Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -300,12 +300,9 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
300 |
max_attempts = 3
|
301 |
context_reduction_factor = 0.7
|
302 |
|
303 |
-
contextualized_question, topics, entity_tracker = chatbot.process_question(question)
|
304 |
-
|
305 |
-
# Convert sets to lists in entity_tracker
|
306 |
-
serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}
|
307 |
-
|
308 |
if web_search:
|
|
|
|
|
309 |
search_results = google_search(contextualized_question)
|
310 |
all_answers = []
|
311 |
|
@@ -345,23 +342,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
345 |
)
|
346 |
|
347 |
full_response = generate_chunked_response(model, formatted_prompt)
|
348 |
-
|
349 |
-
answer_patterns = [
|
350 |
-
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
351 |
-
r"Provide a concise and direct answer to the question:",
|
352 |
-
r"Answer:",
|
353 |
-
r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",
|
354 |
-
r"Do not include any source information in your answer."
|
355 |
-
]
|
356 |
-
|
357 |
-
for pattern in answer_patterns:
|
358 |
-
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
359 |
-
if len(match) > 1:
|
360 |
-
answer = match[-1].strip()
|
361 |
-
break
|
362 |
-
else:
|
363 |
-
answer = full_response.strip()
|
364 |
-
|
365 |
all_answers.append(answer)
|
366 |
break
|
367 |
|
@@ -377,14 +358,14 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
377 |
|
378 |
return answer
|
379 |
|
380 |
-
else:
|
381 |
for attempt in range(max_attempts):
|
382 |
try:
|
383 |
if database is None:
|
384 |
-
return "No documents available. Please upload documents
|
385 |
|
386 |
retriever = database.as_retriever()
|
387 |
-
relevant_docs = retriever.get_relevant_documents(
|
388 |
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
389 |
|
390 |
if attempt > 0:
|
@@ -392,50 +373,47 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
392 |
context_str = " ".join(words[:int(len(words) * context_reduction_factor)])
|
393 |
|
394 |
prompt_template = """
|
395 |
-
Answer the question based on the following context:
|
396 |
Context:
|
397 |
{context}
|
398 |
-
|
399 |
-
If the context doesn't contain relevant information, state that the information is not available.
|
400 |
Provide a summarized and direct answer to the question.
|
401 |
-
Do not include any source information in your answer.
|
402 |
"""
|
403 |
|
404 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
405 |
-
formatted_prompt = prompt_val.format(context=context_str, question=
|
406 |
|
407 |
full_response = generate_chunked_response(model, formatted_prompt)
|
408 |
-
|
409 |
-
answer_patterns = [
|
410 |
-
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
411 |
-
r"Provide a concise and direct answer to the question:",
|
412 |
-
r"Answer:",
|
413 |
-
r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",
|
414 |
-
r"Do not include any source information in your answer."
|
415 |
-
]
|
416 |
-
|
417 |
-
for pattern in answer_patterns:
|
418 |
-
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
419 |
-
if len(match) > 1:
|
420 |
-
answer = match[-1].strip()
|
421 |
-
break
|
422 |
-
else:
|
423 |
-
answer = full_response.strip()
|
424 |
|
425 |
return answer
|
426 |
|
427 |
except Exception as e:
|
428 |
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
429 |
-
if
|
430 |
-
|
431 |
-
elif attempt == max_attempts - 1:
|
432 |
-
return f"I apologize, but I'm having trouble processing your question due to its length or complexity. Could you please try rephrasing it more concisely?"
|
433 |
|
434 |
return "An unexpected error occurred. Please try again later."
|
435 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
436 |
# Gradio interface
|
437 |
with gr.Blocks() as demo:
|
438 |
-
gr.Markdown("# Enhanced
|
439 |
|
440 |
with gr.Row():
|
441 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
|
|
300 |
max_attempts = 3
|
301 |
context_reduction_factor = 0.7
|
302 |
|
|
|
|
|
|
|
|
|
|
|
303 |
if web_search:
|
304 |
+
contextualized_question, topics, entity_tracker = chatbot.process_question(question)
|
305 |
+
serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}
|
306 |
search_results = google_search(contextualized_question)
|
307 |
all_answers = []
|
308 |
|
|
|
342 |
)
|
343 |
|
344 |
full_response = generate_chunked_response(model, formatted_prompt)
|
345 |
+
answer = extract_answer(full_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
all_answers.append(answer)
|
347 |
break
|
348 |
|
|
|
358 |
|
359 |
return answer
|
360 |
|
361 |
+
else: # PDF document chat
|
362 |
for attempt in range(max_attempts):
|
363 |
try:
|
364 |
if database is None:
|
365 |
+
return "No documents available. Please upload PDF documents to answer questions."
|
366 |
|
367 |
retriever = database.as_retriever()
|
368 |
+
relevant_docs = retriever.get_relevant_documents(question)
|
369 |
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
370 |
|
371 |
if attempt > 0:
|
|
|
373 |
context_str = " ".join(words[:int(len(words) * context_reduction_factor)])
|
374 |
|
375 |
prompt_template = """
|
376 |
+
Answer the question based on the following context from the PDF document:
|
377 |
Context:
|
378 |
{context}
|
379 |
+
Question: {question}
|
380 |
+
If the context doesn't contain relevant information, state that the information is not available in the document.
|
381 |
Provide a summarized and direct answer to the question.
|
|
|
382 |
"""
|
383 |
|
384 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
385 |
+
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
386 |
|
387 |
full_response = generate_chunked_response(model, formatted_prompt)
|
388 |
+
answer = extract_answer(full_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
|
390 |
return answer
|
391 |
|
392 |
except Exception as e:
|
393 |
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
394 |
+
if attempt == max_attempts - 1:
|
395 |
+
return f"I apologize, but I'm having trouble processing your question. Could you please try rephrasing it more concisely?"
|
|
|
|
|
396 |
|
397 |
return "An unexpected error occurred. Please try again later."
|
398 |
|
399 |
+
def extract_answer(full_response):
|
400 |
+
answer_patterns = [
|
401 |
+
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
402 |
+
r"Provide a concise and direct answer to the question:",
|
403 |
+
r"Answer:",
|
404 |
+
r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",
|
405 |
+
r"Do not include any source information in your answer."
|
406 |
+
]
|
407 |
+
|
408 |
+
for pattern in answer_patterns:
|
409 |
+
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
410 |
+
if len(match) > 1:
|
411 |
+
return match[-1].strip()
|
412 |
+
return full_response.strip()
|
413 |
+
|
414 |
# Gradio interface
|
415 |
with gr.Blocks() as demo:
|
416 |
+
gr.Markdown("# Enhanced PDF Document Chat and Web Search")
|
417 |
|
418 |
with gr.Row():
|
419 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|