Update app.py
Browse files
app.py
CHANGED
|
@@ -61,9 +61,7 @@ def load_model():
|
|
| 61 |
template = """
|
| 62 |
You are an intelligent assistant specialized in document analysis.
|
| 63 |
Use the following information from PDF files to answer the question:
|
| 64 |
-
|
| 65 |
{context}
|
| 66 |
-
|
| 67 |
Question: {question}
|
| 68 |
Answer (in detail and in clear language):
|
| 69 |
"""
|
|
@@ -91,25 +89,28 @@ def respond(message, chat_history):
|
|
| 91 |
except Exception as e:
|
| 92 |
return chat_history + [(message, f"An error occurred: {str(e)}")]
|
| 93 |
|
| 94 |
-
#
|
| 95 |
def handle_upload(files):
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# UI
|
| 115 |
with gr.Blocks(title="Smart Document Assistant", theme=gr.themes.Default()) as demo:
|
|
@@ -139,28 +140,7 @@ with gr.Blocks(title="Smart Document Assistant", theme=gr.themes.Default()) as d
|
|
| 139 |
|
| 140 |
clear_btn = gr.Button("Clear Chat")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
def handle_upload(files):
|
| 144 |
-
global qa_chain
|
| 145 |
-
try:
|
| 146 |
-
retriever = process_pdfs(files)
|
| 147 |
-
llm = load_model()
|
| 148 |
-
|
| 149 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 150 |
-
llm=llm,
|
| 151 |
-
retriever=retriever,
|
| 152 |
-
chain_type="stuff",
|
| 153 |
-
chain_type_kwargs={
|
| 154 |
-
"prompt": PromptTemplate(
|
| 155 |
-
template=template,
|
| 156 |
-
input_variables=["context", "question"]
|
| 157 |
-
)
|
| 158 |
-
},
|
| 159 |
-
return_source_documents=False
|
| 160 |
-
)
|
| 161 |
-
return "Files uploaded successfully!"
|
| 162 |
-
except Exception as e:
|
| 163 |
-
return f"Error uploading files: {str(e)}"
|
| 164 |
|
| 165 |
file_upload.change(
|
| 166 |
handle_upload,
|
|
|
|
| 61 |
template = """
|
| 62 |
You are an intelligent assistant specialized in document analysis.
|
| 63 |
Use the following information from PDF files to answer the question:
|
|
|
|
| 64 |
{context}
|
|
|
|
| 65 |
Question: {question}
|
| 66 |
Answer (in detail and in clear language):
|
| 67 |
"""
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
return chat_history + [(message, f"An error occurred: {str(e)}")]
|
| 91 |
|
| 92 |
+
# Event handling
|
| 93 |
def handle_upload(files):
|
| 94 |
+
global qa_chain
|
| 95 |
+
try:
|
| 96 |
+
retriever = process_pdfs(files)
|
| 97 |
+
llm = load_model()
|
| 98 |
+
|
| 99 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 100 |
+
llm=llm,
|
| 101 |
+
retriever=retriever,
|
| 102 |
+
chain_type="stuff",
|
| 103 |
+
chain_type_kwargs={
|
| 104 |
+
"prompt": PromptTemplate(
|
| 105 |
+
template=template,
|
| 106 |
+
input_variables=["context", "question"]
|
| 107 |
+
)
|
| 108 |
+
},
|
| 109 |
+
return_source_documents=False
|
| 110 |
+
)
|
| 111 |
+
return "Files uploaded successfully!"
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return f"Error uploading files: {str(e)}"
|
| 114 |
|
| 115 |
# UI
|
| 116 |
with gr.Blocks(title="Smart Document Assistant", theme=gr.themes.Default()) as demo:
|
|
|
|
| 140 |
|
| 141 |
clear_btn = gr.Button("Clear Chat")
|
| 142 |
|
| 143 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
file_upload.change(
|
| 146 |
handle_upload,
|