Penality commited on
Commit
8708594
·
verified ·
1 Parent(s): 3be5a4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
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
- # retrieve the document relevant to the query
198
- doc = retrieve_document(user_question)
199
-
200
- if doc:
201
- print(f"found doc:\n{doc}\n")
202
- # Split into smaller chunks
203
- chunks = split_text(doc)
204
-
205
- # Use only the first chunk (to optimize token usage)
206
- prompt = f"Based on this document, answer the question:\n\nDocument:\n{chunks[0]}\n\nQuestion: {user_question}"
207
- print(f"prompt:\n{prompt}")
 
 
 
208
  else:
209
- prompt=user_question
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
  ),