Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
4 |
+
from llama_index.core.embeddings import resolve_embed_model
|
5 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
6 |
+
from llama_index.core import PromptTemplate
|
7 |
+
|
8 |
+
# bge embedding model
|
9 |
+
Settings.embed_model = resolve_embed_model("local:BAAI/bge-small-en-v1.5")
|
10 |
+
query_wrapper_prompt = PromptTemplate(
|
11 |
+
"Below is an instruction that describes a task. "
|
12 |
+
"Write a response that appropriately completes the request.\n\n"
|
13 |
+
"### Instruction:\n{query_str}\n\n### Response:"
|
14 |
+
)
|
15 |
+
|
16 |
+
import torch
|
17 |
+
|
18 |
+
llm = HuggingFaceLLM(
|
19 |
+
context_window=2048,
|
20 |
+
max_new_tokens=256,
|
21 |
+
generate_kwargs={"temperature": 0.25, "do_sample": False},
|
22 |
+
query_wrapper_prompt=query_wrapper_prompt,
|
23 |
+
tokenizer_name="Writer/camel-5b-hf",
|
24 |
+
model_name="Writer/camel-5b-hf",
|
25 |
+
device_map="auto",
|
26 |
+
tokenizer_kwargs={"max_length": 2048},
|
27 |
+
# uncomment this if using CUDA to reduce memory usage
|
28 |
+
# model_kwargs={"torch_dtype": torch.float16}
|
29 |
+
)
|
30 |
+
|
31 |
+
Settings.chunk_size = 512
|
32 |
+
Settings.llm = llm
|
33 |
+
|
34 |
+
info = {}
|
35 |
+
|
36 |
+
def echo(message, history, system_prompt, tokens):
|
37 |
+
index = info['index']
|
38 |
+
query_engine = index.as_query_engine()
|
39 |
+
response = query_engine.query(message)
|
40 |
+
return response.response
|
41 |
+
|
42 |
+
with gr.Blocks() as demo:
|
43 |
+
with gr.Row():
|
44 |
+
folder = gr.File(file_count='directory')
|
45 |
+
docs = gr.Textbox(label="Documents")
|
46 |
+
system_prompt = gr.Textbox("You are helpful AI.", label="System Prompt")
|
47 |
+
slider = gr.Slider(10, 100, render=False)
|
48 |
+
|
49 |
+
gr.ChatInterface(
|
50 |
+
echo, additional_inputs=[system_prompt, slider]
|
51 |
+
)
|
52 |
+
def update_docs(filepath):
|
53 |
+
print(filepath)
|
54 |
+
documents = SimpleDirectoryReader(input_files=filepath).load_data()
|
55 |
+
index = VectorStoreIndex.from_documents(
|
56 |
+
documents,
|
57 |
+
)
|
58 |
+
info['index'] = index
|
59 |
+
return documents
|
60 |
+
folder.upload(update_docs, folder, docs)
|
61 |
+
|
62 |
+
demo.launch()
|