Update app.py
Browse files
app.py
CHANGED
@@ -82,18 +82,12 @@ def initialize_llmchain(vector_db, temperature=0.5, max_tokens=4096, top_k=3):
|
|
82 |
def format_citation(source_doc):
|
83 |
content = source_doc.page_content.strip()
|
84 |
page = source_doc.metadata["page"] + 1
|
85 |
-
return
|
86 |
-
|
87 |
-
def format_response_with_citations(answer, sources):
|
88 |
-
citations = [format_citation(source) for source in sources[:3]]
|
89 |
-
formatted_response = f"{answer}\n\nReferences:\n"
|
90 |
-
for idx, citation in enumerate(citations, 1):
|
91 |
-
formatted_response += f"^{idx}^ {citation}\n"
|
92 |
-
return formatted_response
|
93 |
|
94 |
def conversation(qa_chain, message, history):
|
95 |
if not qa_chain:
|
96 |
-
return None, gr.update(value=""), history, "
|
|
|
97 |
|
98 |
formatted_history = []
|
99 |
for user_msg, bot_msg in history:
|
@@ -108,14 +102,22 @@ def conversation(qa_chain, message, history):
|
|
108 |
answer = response["answer"]
|
109 |
if "Helpful Answer:" in answer:
|
110 |
answer = answer.split("Helpful Answer:")[-1]
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
new_history = history + [(message,
|
118 |
-
return qa_chain, gr.update(value=""), new_history
|
|
|
119 |
|
120 |
def demo():
|
121 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="sky")) as demo:
|
@@ -133,13 +135,23 @@ def demo():
|
|
133 |
document = gr.Files(
|
134 |
height=300,
|
135 |
file_count="multiple",
|
136 |
-
file_types=[".pdf"],
|
137 |
label="Upload PDF documents"
|
138 |
)
|
139 |
upload_status = gr.Textbox(label="Upload Status", interactive=False)
|
140 |
|
141 |
with gr.Column(scale=2):
|
142 |
-
chatbot = gr.Chatbot(height=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
with gr.Row():
|
144 |
msg = gr.Textbox(
|
145 |
placeholder="Ask a question about your documents...",
|
@@ -167,16 +179,34 @@ def demo():
|
|
167 |
outputs=[vector_db, qa_chain, upload_status]
|
168 |
)
|
169 |
|
|
|
|
|
|
|
|
|
170 |
# Chatbot events
|
171 |
submit_btn.click(
|
172 |
conversation,
|
173 |
inputs=[qa_chain, msg, chatbot],
|
174 |
-
outputs=[qa_chain, msg, chatbot
|
|
|
|
|
|
|
|
|
175 |
)
|
176 |
msg.submit(
|
177 |
conversation,
|
178 |
inputs=[qa_chain, msg, chatbot],
|
179 |
-
outputs=[qa_chain, msg, chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
)
|
181 |
|
182 |
demo.queue().launch(debug=True)
|
|
|
82 |
def format_citation(source_doc):
|
83 |
content = source_doc.page_content.strip()
|
84 |
page = source_doc.metadata["page"] + 1
|
85 |
+
return content, page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
def conversation(qa_chain, message, history):
|
88 |
if not qa_chain:
|
89 |
+
return (None, gr.update(value=""), history, "", 0, "", 0, "", 0,
|
90 |
+
"Please upload a document first.")
|
91 |
|
92 |
formatted_history = []
|
93 |
for user_msg, bot_msg in history:
|
|
|
102 |
answer = response["answer"]
|
103 |
if "Helpful Answer:" in answer:
|
104 |
answer = answer.split("Helpful Answer:")[-1]
|
105 |
+
|
106 |
+
# Format answer with citation numbers
|
107 |
+
sources = response["source_documents"][:3]
|
108 |
+
modified_answer = answer
|
109 |
+
for i in range(len(sources)):
|
110 |
+
modified_answer = modified_answer + f" [{i+1}]"
|
111 |
+
|
112 |
+
# Get citation contents and page numbers
|
113 |
+
citations = [format_citation(source) for source in sources]
|
114 |
+
source1_content, page1 = citations[0] if len(citations) > 0 else ("", 0)
|
115 |
+
source2_content, page2 = citations[1] if len(citations) > 1 else ("", 0)
|
116 |
+
source3_content, page3 = citations[2] if len(citations) > 2 else ("", 0)
|
117 |
|
118 |
+
new_history = history + [(message, modified_answer)]
|
119 |
+
return (qa_chain, gr.update(value=""), new_history,
|
120 |
+
source1_content, page1, source2_content, page2, source3_content, page3, "")
|
121 |
|
122 |
def demo():
|
123 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="sky")) as demo:
|
|
|
135 |
document = gr.Files(
|
136 |
height=300,
|
137 |
file_count="multiple",
|
138 |
+
file_types=[".pdf"],
|
139 |
label="Upload PDF documents"
|
140 |
)
|
141 |
upload_status = gr.Textbox(label="Upload Status", interactive=False)
|
142 |
|
143 |
with gr.Column(scale=2):
|
144 |
+
chatbot = gr.Chatbot(height=500)
|
145 |
+
with gr.Accordion("Citations", open=False):
|
146 |
+
with gr.Row():
|
147 |
+
doc_source1 = gr.Textbox(label="[1]", lines=2, container=True, scale=20)
|
148 |
+
source1_page = gr.Number(label="Page", scale=1)
|
149 |
+
with gr.Row():
|
150 |
+
doc_source2 = gr.Textbox(label="[2]", lines=2, container=True, scale=20)
|
151 |
+
source2_page = gr.Number(label="Page", scale=1)
|
152 |
+
with gr.Row():
|
153 |
+
doc_source3 = gr.Textbox(label="[3]", lines=2, container=True, scale=20)
|
154 |
+
source3_page = gr.Number(label="Page", scale=1)
|
155 |
with gr.Row():
|
156 |
msg = gr.Textbox(
|
157 |
placeholder="Ask a question about your documents...",
|
|
|
179 |
outputs=[vector_db, qa_chain, upload_status]
|
180 |
)
|
181 |
|
182 |
+
# Clear citations when chat is cleared
|
183 |
+
def clear_all():
|
184 |
+
return ["", 0, "", 0, "", 0]
|
185 |
+
|
186 |
# Chatbot events
|
187 |
submit_btn.click(
|
188 |
conversation,
|
189 |
inputs=[qa_chain, msg, chatbot],
|
190 |
+
outputs=[qa_chain, msg, chatbot,
|
191 |
+
doc_source1, source1_page,
|
192 |
+
doc_source2, source2_page,
|
193 |
+
doc_source3, source3_page,
|
194 |
+
upload_status]
|
195 |
)
|
196 |
msg.submit(
|
197 |
conversation,
|
198 |
inputs=[qa_chain, msg, chatbot],
|
199 |
+
outputs=[qa_chain, msg, chatbot,
|
200 |
+
doc_source1, source1_page,
|
201 |
+
doc_source2, source2_page,
|
202 |
+
doc_source3, source3_page,
|
203 |
+
upload_status]
|
204 |
+
)
|
205 |
+
clear_btn.click(
|
206 |
+
clear_all,
|
207 |
+
outputs=[doc_source1, source1_page,
|
208 |
+
doc_source2, source2_page,
|
209 |
+
doc_source3, source3_page]
|
210 |
)
|
211 |
|
212 |
demo.queue().launch(debug=True)
|