Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
-
from
|
| 5 |
from langchain.vectorstores.faiss import FAISS
|
| 6 |
-
from
|
| 7 |
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
from langchain.prompts.prompt import PromptTemplate
|
|
@@ -74,7 +74,11 @@ def prepare_vector_store(filename):
|
|
| 74 |
return vectorstore
|
| 75 |
|
| 76 |
# Retrieveal QA chian
|
| 77 |
-
def get_retrieval_qa_chain(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
chain = RetrievalQA.from_chain_type(
|
| 79 |
llm=hf_model,
|
| 80 |
retriever=retriever,
|
|
@@ -98,10 +102,7 @@ def generate(question, answer, retriever):
|
|
| 98 |
|
| 99 |
# replaces the retreiver in the question answering chain whenever a new file is uploaded
|
| 100 |
def upload_file(file):
|
| 101 |
-
|
| 102 |
-
vectorstore=prepare_vector_store(file)
|
| 103 |
-
)
|
| 104 |
-
return file, new_retriever
|
| 105 |
|
| 106 |
with gr.Blocks() as demo:
|
| 107 |
gr.Markdown("""
|
|
@@ -114,11 +115,7 @@ with gr.Blocks() as demo:
|
|
| 114 |
""")
|
| 115 |
|
| 116 |
default_text_file = "Oppenheimer-movie-wiki.txt"
|
| 117 |
-
|
| 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 |
|
|
@@ -128,7 +125,7 @@ with gr.Blocks() as demo:
|
|
| 128 |
file_types=["text"],
|
| 129 |
file_count="single"
|
| 130 |
)
|
| 131 |
-
upload_button.upload(upload_file, upload_button, [file_name,
|
| 132 |
|
| 133 |
gr.Markdown("## Enter your question")
|
| 134 |
|
|
@@ -143,7 +140,7 @@ with gr.Blocks() as demo:
|
|
| 143 |
with gr.Column():
|
| 144 |
clear = gr.ClearButton([ques, ans])
|
| 145 |
|
| 146 |
-
btn.click(fn=generate, inputs=[ques, ans,
|
| 147 |
examples = gr.Examples(
|
| 148 |
examples=[
|
| 149 |
"Who portrayed J. Robert Oppenheimer in the new Oppenheimer movie?",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain_community.document_loaders import UnstructuredFileLoader
|
| 5 |
from langchain.vectorstores.faiss import FAISS
|
| 6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
from langchain.prompts.prompt import PromptTemplate
|
|
|
|
| 74 |
return vectorstore
|
| 75 |
|
| 76 |
# Retrieveal QA chian
|
| 77 |
+
def get_retrieval_qa_chain(text_file):
|
| 78 |
+
retriever = VectorStoreRetriever(
|
| 79 |
+
vectorstore=prepare_vector_store(text_file)
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
chain = RetrievalQA.from_chain_type(
|
| 83 |
llm=hf_model,
|
| 84 |
retriever=retriever,
|
|
|
|
| 102 |
|
| 103 |
# replaces the retreiver in the question answering chain whenever a new file is uploaded
|
| 104 |
def upload_file(file):
|
| 105 |
+
return file, file
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
with gr.Blocks() as demo:
|
| 108 |
gr.Markdown("""
|
|
|
|
| 115 |
""")
|
| 116 |
|
| 117 |
default_text_file = "Oppenheimer-movie-wiki.txt"
|
| 118 |
+
text_file = gr.State(default_text_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
gr.Markdown("## Upload a txt file or Use the Default 'Oppenheimer-movie-wiki.txt' that has already been loaded")
|
| 121 |
|
|
|
|
| 125 |
file_types=["text"],
|
| 126 |
file_count="single"
|
| 127 |
)
|
| 128 |
+
upload_button.upload(upload_file, upload_button, [file_name, text_file])
|
| 129 |
|
| 130 |
gr.Markdown("## Enter your question")
|
| 131 |
|
|
|
|
| 140 |
with gr.Column():
|
| 141 |
clear = gr.ClearButton([ques, ans])
|
| 142 |
|
| 143 |
+
btn.click(fn=generate, inputs=[ques, ans, text_file], outputs=[ans])
|
| 144 |
examples = gr.Examples(
|
| 145 |
examples=[
|
| 146 |
"Who portrayed J. Robert Oppenheimer in the new Oppenheimer movie?",
|