Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -190,23 +190,26 @@ def split_text(text, chunk_size=500):
|
|
| 190 |
print("splitting")
|
| 191 |
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 192 |
|
| 193 |
-
def chatbot(user_question):
|
| 194 |
"""Processes the PDF and answers the user's question."""
|
| 195 |
print("chatbot start")
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
| 208 |
else:
|
| 209 |
-
|
| 210 |
|
| 211 |
try:
|
| 212 |
print("asking", prompt)
|
|
@@ -232,7 +235,7 @@ iface = gr.TabbedInterface(
|
|
| 232 |
[
|
| 233 |
gr.Interface(
|
| 234 |
fn=chatbot,
|
| 235 |
-
inputs=gr.Textbox(label="Ask a Question"),
|
| 236 |
outputs=gr.Textbox(label="Answer"),
|
| 237 |
title="PDF Q&A Chatbot (Powered by Together.AI)",
|
| 238 |
),
|
|
|
|
| 190 |
print("splitting")
|
| 191 |
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 192 |
|
| 193 |
+
def chatbot(doc_presence, user_question):
|
| 194 |
"""Processes the PDF and answers the user's question."""
|
| 195 |
print("chatbot start")
|
| 196 |
+
|
| 197 |
+
if doc_presence:
|
| 198 |
+
# retrieve the document relevant to the query
|
| 199 |
+
doc = retrieve_document(user_question)
|
| 200 |
+
|
| 201 |
+
if doc:
|
| 202 |
+
print(f"found doc:\n{doc}\n")
|
| 203 |
+
# Split into smaller chunks
|
| 204 |
+
chunks = split_text(doc)
|
| 205 |
+
|
| 206 |
+
# Use only the first chunk (to optimize token usage)
|
| 207 |
+
prompt = f"Based on this document, answer the question:\n\nDocument:\n{chunks[0]}\n\nQuestion: {user_question}"
|
| 208 |
+
print(f"prompt:\n{prompt}")
|
| 209 |
+
else:
|
| 210 |
+
prompt=user_question
|
| 211 |
else:
|
| 212 |
+
prompt=user_question
|
| 213 |
|
| 214 |
try:
|
| 215 |
print("asking", prompt)
|
|
|
|
| 235 |
[
|
| 236 |
gr.Interface(
|
| 237 |
fn=chatbot,
|
| 238 |
+
inputs=[gr.Textbox(label="Ask a Question"), "text"]
|
| 239 |
outputs=gr.Textbox(label="Answer"),
|
| 240 |
title="PDF Q&A Chatbot (Powered by Together.AI)",
|
| 241 |
),
|