Spaces:
Running
Running
Update app.py
Browse filesupdated the code to handle no pdf file provided
app.py
CHANGED
@@ -73,20 +73,27 @@ def split_text(text, chunk_size=500):
|
|
73 |
def chatbot(pdf_file, user_question):
|
74 |
"""Processes the PDF and answers the user's question."""
|
75 |
print("chatbot start")
|
76 |
-
|
77 |
-
# Extract text from the PDF
|
78 |
-
text = extract_text_from_pdf(pdf_file)
|
79 |
-
if not text:
|
80 |
-
return "Could not extract any text from the PDF."
|
81 |
-
|
82 |
-
# retrieve the document relevant to the query
|
83 |
-
doc = retrieve_document(user_question)
|
84 |
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
try:
|
92 |
response = together.Completion.create(
|
|
|
73 |
def chatbot(pdf_file, user_question):
|
74 |
"""Processes the PDF and answers the user's question."""
|
75 |
print("chatbot start")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
if pdf_file:
|
78 |
+
# Extract text from the PDF
|
79 |
+
text = extract_text_from_pdf(pdf_file)
|
80 |
+
if not text:
|
81 |
+
return "Could not extract any text from the PDF."
|
82 |
|
83 |
+
try:
|
84 |
+
# retrieve the document relevant to the query
|
85 |
+
doc = retrieve_document(user_question)
|
86 |
+
except Exception as e:
|
87 |
+
return f"Error retrieving document relevant to the query: {user_question} \n{e}"
|
88 |
+
|
89 |
+
if doc:
|
90 |
+
# Split into smaller chunks
|
91 |
+
chunks = split_text(doc)
|
92 |
+
|
93 |
+
# Use only the first chunk (to optimize token usage)
|
94 |
+
prompt = f"Based on this document, answer the question:\n\nDocument:\n{chunks[0]}\n\nQuestion: {user_question}"
|
95 |
+
else:
|
96 |
+
prompt=user_question
|
97 |
|
98 |
try:
|
99 |
response = together.Completion.create(
|