Update app.py
Browse files
app.py
CHANGED
@@ -37,7 +37,6 @@ def prepare_vector_store(filename):
|
|
37 |
# Load data
|
38 |
loader = UnstructuredFileLoader(filename)
|
39 |
raw_documents = loader.load()
|
40 |
-
print(raw_documents[:1000])
|
41 |
|
42 |
# Split the text
|
43 |
text_splitter = CharacterTextSplitter(
|
@@ -48,12 +47,10 @@ def prepare_vector_store(filename):
|
|
48 |
)
|
49 |
|
50 |
documents = text_splitter.split_documents(raw_documents)
|
51 |
-
print(documents[:3])
|
52 |
|
53 |
# Creating a vectorstore
|
54 |
embeddings = HuggingFaceEmbeddings()
|
55 |
vectorstore = FAISS.from_documents(documents, embeddings)
|
56 |
-
print(embeddings, vectorstore)
|
57 |
|
58 |
return vectorstore
|
59 |
|
@@ -76,10 +73,8 @@ def get_retrieval_qa_chain(filename):
|
|
76 |
model = RetrievalQA.from_chain_type(
|
77 |
llm=llm,
|
78 |
retriever=retriever,
|
79 |
-
chain_type_kwargs={"prompt": QA_PROMPT
|
80 |
-
verbose=True,
|
81 |
)
|
82 |
-
print(filename)
|
83 |
return model
|
84 |
|
85 |
# Question Answering Chain
|
@@ -111,7 +106,6 @@ def generate(question, chat_history):
|
|
111 |
# replaces the retreiver in the question answering chain whenever a new file is uploaded
|
112 |
def upload_file(qa_chain):
|
113 |
def uploader(file):
|
114 |
-
print(file)
|
115 |
qa_chain.retriever = VectorStoreRetriever(
|
116 |
vectorstore=prepare_vector_store(file)
|
117 |
)
|
@@ -121,9 +115,12 @@ def upload_file(qa_chain):
|
|
121 |
with gr.Blocks() as demo:
|
122 |
gr.Markdown("""
|
123 |
# RAG-Phi-2 Chatbot demo
|
124 |
-
### This
|
125 |
-
###
|
126 |
-
|
|
|
|
|
|
|
127 |
""")
|
128 |
|
129 |
file_output = gr.File(label="txt file")
|
@@ -139,5 +136,12 @@ with gr.Blocks() as demo:
|
|
139 |
|
140 |
clear = gr.ClearButton([msg, chatbot])
|
141 |
msg.submit(fn=generate, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
demo.launch()
|
|
|
37 |
# Load data
|
38 |
loader = UnstructuredFileLoader(filename)
|
39 |
raw_documents = loader.load()
|
|
|
40 |
|
41 |
# Split the text
|
42 |
text_splitter = CharacterTextSplitter(
|
|
|
47 |
)
|
48 |
|
49 |
documents = text_splitter.split_documents(raw_documents)
|
|
|
50 |
|
51 |
# Creating a vectorstore
|
52 |
embeddings = HuggingFaceEmbeddings()
|
53 |
vectorstore = FAISS.from_documents(documents, embeddings)
|
|
|
54 |
|
55 |
return vectorstore
|
56 |
|
|
|
73 |
model = RetrievalQA.from_chain_type(
|
74 |
llm=llm,
|
75 |
retriever=retriever,
|
76 |
+
chain_type_kwargs={"prompt": QA_PROMPT},
|
|
|
77 |
)
|
|
|
78 |
return model
|
79 |
|
80 |
# Question Answering Chain
|
|
|
106 |
# replaces the retreiver in the question answering chain whenever a new file is uploaded
|
107 |
def upload_file(qa_chain):
|
108 |
def uploader(file):
|
|
|
109 |
qa_chain.retriever = VectorStoreRetriever(
|
110 |
vectorstore=prepare_vector_store(file)
|
111 |
)
|
|
|
115 |
with gr.Blocks() as demo:
|
116 |
gr.Markdown("""
|
117 |
# RAG-Phi-2 Chatbot demo
|
118 |
+
### This demo uses the Phi-2 language model and Retrieval Augmented Generation (RAG) to allow you to add custom knowledge to the chatbot by uploading a txt file. Upload a txt file that contains the text data that you would like to augment the chatbot with.
|
119 |
+
### 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.
|
120 |
+
|
121 |
+
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.
|
122 |
+
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.
|
123 |
+
The chatbot 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.
|
124 |
""")
|
125 |
|
126 |
file_output = gr.File(label="txt file")
|
|
|
136 |
|
137 |
clear = gr.ClearButton([msg, chatbot])
|
138 |
msg.submit(fn=generate, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
139 |
+
examples = gr.Examples(
|
140 |
+
examples=[
|
141 |
+
"Who portrayed J. Robert Oppenheimer in the new Oppenheimer movie?",
|
142 |
+
"In the plot of the movie, why did Lewis Strauss resent Robert Oppenheimer?"
|
143 |
+
],
|
144 |
+
inputs=[msg],
|
145 |
+
)
|
146 |
|
147 |
demo.launch()
|