Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -32,6 +32,25 @@ QA_PROMPT = PromptTemplate(
|
|
32 |
input_variables=["question", "context"]
|
33 |
)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
# Returns a faiss vector store given a txt file
|
36 |
def prepare_vector_store(filename):
|
37 |
# Load data
|
@@ -54,42 +73,19 @@ def prepare_vector_store(filename):
|
|
54 |
|
55 |
return vectorstore
|
56 |
|
57 |
-
# Load Phi-2 model from hugging face hub
|
58 |
-
model_id = "microsoft/phi-2"
|
59 |
-
|
60 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
61 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, device_map="auto", trust_remote_code=True)
|
62 |
-
|
63 |
-
streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True)
|
64 |
-
phi2 = pipeline(
|
65 |
-
"text-generation",
|
66 |
-
tokenizer=tokenizer,
|
67 |
-
model=model,
|
68 |
-
max_new_tokens=256,
|
69 |
-
eos_token_id=tokenizer.eos_token_id,
|
70 |
-
device_map="auto",
|
71 |
-
streamer=streamer
|
72 |
-
) # GPU
|
73 |
-
hf_model = HuggingFacePipeline(pipeline=phi2)
|
74 |
-
|
75 |
# Retrieveal QA chian
|
76 |
-
def get_retrieval_qa_chain(
|
77 |
-
|
78 |
-
|
79 |
-
vectorstore=prepare_vector_store(filename)
|
80 |
-
)
|
81 |
-
model = RetrievalQA.from_chain_type(
|
82 |
-
llm=llm,
|
83 |
retriever=retriever,
|
84 |
chain_type_kwargs={"prompt": QA_PROMPT},
|
85 |
)
|
86 |
-
return
|
87 |
-
|
88 |
-
# Question Answering Chain
|
89 |
-
qa_chain = get_retrieval_qa_chain(filename="Oppenheimer-movie-wiki.txt")
|
90 |
|
91 |
# Generates response using the question answering chain defined earlier
|
92 |
-
def generate(question, answer):
|
|
|
|
|
93 |
query = f"{question}"
|
94 |
|
95 |
thread = Thread(target=qa_chain.invoke, kwargs={"input": {"query": query}})
|
@@ -98,46 +94,56 @@ def generate(question, answer):
|
|
98 |
response = ""
|
99 |
for token in streamer:
|
100 |
response += token
|
101 |
-
yield response
|
102 |
|
103 |
# replaces the retreiver in the question answering chain whenever a new file is uploaded
|
104 |
-
def upload_file(
|
105 |
-
|
106 |
-
qa_chain.retriever = VectorStoreRetriever(
|
107 |
vectorstore=prepare_vector_store(file)
|
108 |
)
|
109 |
-
|
110 |
-
return uploader
|
111 |
|
112 |
with gr.Blocks() as demo:
|
113 |
gr.Markdown("""
|
114 |
-
#
|
115 |
-
### This demo uses the Phi-2 language model and Retrieval Augmented Generation (RAG)
|
116 |
### If you don't have one, there is a txt file already loaded, the new Oppenheimer movie's entire wikipedia page. The movie came out very recently in July, 2023, so the Phi-2 model is not aware of it.
|
117 |
The context size of the Phi-2 model is 2048 tokens, so even this medium size wikipedia page (11.5k tokens) does not fit in the context window.
|
118 |
Retrieval Augmented Generation (RAG) enables us to retrieve just the few small chunks of the document that are relevant to the our query and inject it into our prompt.
|
119 |
The model is then able to answer questions by incorporating knowledge from the newly provided document. RAG can be used with thousands of documents, but this demo is limited to just one txt file.
|
120 |
""")
|
121 |
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
upload_button = gr.UploadButton(
|
124 |
label="Click to upload a text file",
|
125 |
file_types=["text"],
|
126 |
file_count="single"
|
127 |
)
|
128 |
-
upload_button.upload(upload_file
|
129 |
-
|
|
|
|
|
130 |
with gr.Row():
|
131 |
with gr.Column():
|
132 |
ques = gr.Textbox(label="Question", placeholder="Enter text here", lines=3)
|
133 |
with gr.Column():
|
134 |
-
ans = gr.Textbox(label="Answer", lines=4)
|
135 |
with gr.Row():
|
136 |
with gr.Column():
|
137 |
btn = gr.Button("Submit")
|
138 |
with gr.Column():
|
139 |
clear = gr.ClearButton([ques, ans])
|
140 |
-
|
|
|
141 |
examples = gr.Examples(
|
142 |
examples=[
|
143 |
"Who portrayed J. Robert Oppenheimer in the new Oppenheimer movie?",
|
|
|
32 |
input_variables=["question", "context"]
|
33 |
)
|
34 |
|
35 |
+
# Load Phi-2 model from hugging face hub
|
36 |
+
model_id = "microsoft/phi-2"
|
37 |
+
|
38 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
39 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, device_map="auto", trust_remote_code=True)
|
40 |
+
|
41 |
+
streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True)
|
42 |
+
phi2 = pipeline(
|
43 |
+
"text-generation",
|
44 |
+
tokenizer=tokenizer,
|
45 |
+
model=model,
|
46 |
+
max_new_tokens=128,
|
47 |
+
pad_token_id=tokenizer.eos_token_id,
|
48 |
+
eos_token_id=tokenizer.eos_token_id,
|
49 |
+
device_map="auto",
|
50 |
+
streamer=streamer
|
51 |
+
) # GPU
|
52 |
+
hf_model = HuggingFacePipeline(pipeline=phi2)
|
53 |
+
|
54 |
# Returns a faiss vector store given a txt file
|
55 |
def prepare_vector_store(filename):
|
56 |
# Load data
|
|
|
73 |
|
74 |
return vectorstore
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# Retrieveal QA chian
|
77 |
+
def get_retrieval_qa_chain(retriever):
|
78 |
+
chain = RetrievalQA.from_chain_type(
|
79 |
+
llm=hf_model,
|
|
|
|
|
|
|
|
|
80 |
retriever=retriever,
|
81 |
chain_type_kwargs={"prompt": QA_PROMPT},
|
82 |
)
|
83 |
+
return chain
|
|
|
|
|
|
|
84 |
|
85 |
# Generates response using the question answering chain defined earlier
|
86 |
+
def generate(question, answer, retriever):
|
87 |
+
qa_chain = get_retrieval_qa_chain(retriever)
|
88 |
+
|
89 |
query = f"{question}"
|
90 |
|
91 |
thread = Thread(target=qa_chain.invoke, kwargs={"input": {"query": query}})
|
|
|
94 |
response = ""
|
95 |
for token in streamer:
|
96 |
response += token
|
97 |
+
yield response.strip()
|
98 |
|
99 |
# replaces the retreiver in the question answering chain whenever a new file is uploaded
|
100 |
+
def upload_file(file):
|
101 |
+
new_retriever = VectorStoreRetriever(
|
|
|
102 |
vectorstore=prepare_vector_store(file)
|
103 |
)
|
104 |
+
return file, new_retriever
|
|
|
105 |
|
106 |
with gr.Blocks() as demo:
|
107 |
gr.Markdown("""
|
108 |
+
# Retrieval Augmented Generation with Phi-2: Question Answering demo
|
109 |
+
### This demo uses the Phi-2 language model and Retrieval Augmented Generation (RAG). It allows you to upload a txt file and ask the model questions related to the content of that file.
|
110 |
### If you don't have one, there is a txt file already loaded, the new Oppenheimer movie's entire wikipedia page. The movie came out very recently in July, 2023, so the Phi-2 model is not aware of it.
|
111 |
The context size of the Phi-2 model is 2048 tokens, so even this medium size wikipedia page (11.5k tokens) does not fit in the context window.
|
112 |
Retrieval Augmented Generation (RAG) enables us to retrieve just the few small chunks of the document that are relevant to the our query and inject it into our prompt.
|
113 |
The model is then able to answer questions by incorporating knowledge from the newly provided document. RAG can be used with thousands of documents, but this demo is limited to just one txt file.
|
114 |
""")
|
115 |
|
116 |
+
default_text_file = "Oppenheimer-movie-wiki.txt"
|
117 |
+
retriever = gr.State(
|
118 |
+
VectorStoreRetriever(
|
119 |
+
vectorstore=prepare_vector_store(default_text_file)
|
120 |
+
)
|
121 |
+
)
|
122 |
+
|
123 |
+
gr.Markdown("## Upload a txt file or Use the Default 'Oppenheimer-movie-wiki.txt' that has already been loaded")
|
124 |
+
|
125 |
+
file_name = gr.Textbox(label="Loaded text file", value=default_text_file, lines=1, interactive=False)
|
126 |
upload_button = gr.UploadButton(
|
127 |
label="Click to upload a text file",
|
128 |
file_types=["text"],
|
129 |
file_count="single"
|
130 |
)
|
131 |
+
upload_button.upload(upload_file, upload_button, [file_name, retriever])
|
132 |
+
|
133 |
+
gr.Markdown("## Enter your question")
|
134 |
+
|
135 |
with gr.Row():
|
136 |
with gr.Column():
|
137 |
ques = gr.Textbox(label="Question", placeholder="Enter text here", lines=3)
|
138 |
with gr.Column():
|
139 |
+
ans = gr.Textbox(label="Answer", lines=4, interactive=False)
|
140 |
with gr.Row():
|
141 |
with gr.Column():
|
142 |
btn = gr.Button("Submit")
|
143 |
with gr.Column():
|
144 |
clear = gr.ClearButton([ques, ans])
|
145 |
+
|
146 |
+
btn.click(fn=generate, inputs=[ques, ans, retriever], outputs=[ans])
|
147 |
examples = gr.Examples(
|
148 |
examples=[
|
149 |
"Who portrayed J. Robert Oppenheimer in the new Oppenheimer movie?",
|