Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -143,8 +143,10 @@ def update_vectors(files, parser):
|
|
| 143 |
# Save the updated list of documents
|
| 144 |
save_documents(uploaded_documents)
|
| 145 |
|
|
|
|
| 146 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", display_documents()
|
| 147 |
|
|
|
|
| 148 |
def delete_documents(selected_docs):
|
| 149 |
global uploaded_documents
|
| 150 |
|
|
@@ -381,8 +383,7 @@ def summarize_web_results(query: str, search_results: List[Dict[str, str]], conv
|
|
| 381 |
You have to create a comprehensive news summary FOCUSING on the context provided to you.
|
| 382 |
Include key facts, relevant statistics, and expert opinions if available.
|
| 383 |
Ensure the article is well-structured with an introduction, main body, and conclusion, IF NECESSARY.
|
| 384 |
-
|
| 385 |
-
If it's a new topic, than address the query as a new conversation.
|
| 386 |
Cite sources directly within the generated text and not at the end of the generated text, integrating URLs where appropriate to support the information provided:
|
| 387 |
|
| 388 |
{search_context}
|
|
@@ -395,7 +396,7 @@ def summarize_web_results(query: str, search_results: List[Dict[str, str]], conv
|
|
| 395 |
return f"An error occurred during summarization: {str(e)}"
|
| 396 |
|
| 397 |
# Modify the existing respond function to handle both PDF and web search
|
| 398 |
-
def respond(message, history, model, temperature, num_calls,
|
| 399 |
logging.info(f"User Query: {message}")
|
| 400 |
logging.info(f"Model Used: {model}")
|
| 401 |
logging.info(f"Selected Documents: {selected_docs}")
|
|
@@ -437,7 +438,7 @@ def respond(message, history, model, temperature, num_calls, selected_docs, use_
|
|
| 437 |
if not relevant_docs:
|
| 438 |
yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
| 439 |
return
|
| 440 |
-
|
| 441 |
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
| 442 |
logging.info(f"Context length: {len(context_str)}")
|
| 443 |
else:
|
|
@@ -445,23 +446,27 @@ def respond(message, history, model, temperature, num_calls, selected_docs, use_
|
|
| 445 |
yield "No documents available. Please upload PDF documents to answer questions."
|
| 446 |
return
|
| 447 |
|
| 448 |
-
if model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
# Use Cloudflare API
|
| 450 |
-
for
|
| 451 |
-
yield
|
| 452 |
else:
|
| 453 |
# Use Hugging Face API
|
| 454 |
-
for
|
| 455 |
-
yield
|
| 456 |
except Exception as e:
|
| 457 |
logging.error(f"Error with {model}: {str(e)}")
|
| 458 |
if "microsoft/Phi-3-mini-4k-instruct" in model:
|
| 459 |
logging.info("Falling back to Mistral model due to Phi-3 error")
|
| 460 |
fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 461 |
-
yield from respond(message, history, fallback_model, temperature, num_calls, selected_docs
|
| 462 |
else:
|
| 463 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
| 464 |
-
|
| 465 |
logging.basicConfig(level=logging.DEBUG)
|
| 466 |
|
| 467 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
|
@@ -645,7 +650,7 @@ def refresh_documents():
|
|
| 645 |
# Define the checkbox outside the demo block
|
| 646 |
document_selector = gr.CheckboxGroup(label="Select documents to query")
|
| 647 |
|
| 648 |
-
use_web_search = gr.Checkbox(label="Use Web Search", value=
|
| 649 |
|
| 650 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
| 651 |
|
|
@@ -658,8 +663,8 @@ demo = gr.ChatInterface(
|
|
| 658 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
| 659 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 660 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
| 661 |
-
gr.
|
| 662 |
-
gr.
|
| 663 |
],
|
| 664 |
title="AI-powered PDF Chat and Web Search Assistant",
|
| 665 |
description="Chat with your PDFs or use web search to answer questions.",
|
|
@@ -700,10 +705,10 @@ demo = gr.ChatInterface(
|
|
| 700 |
)
|
| 701 |
)
|
| 702 |
|
|
|
|
| 703 |
# Add file upload functionality
|
| 704 |
with demo:
|
| 705 |
gr.Markdown("## Upload and Manage PDF Documents")
|
| 706 |
-
|
| 707 |
with gr.Row():
|
| 708 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
| 709 |
parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
|
|
@@ -714,19 +719,25 @@ with demo:
|
|
| 714 |
delete_button = gr.Button("Delete Selected Documents")
|
| 715 |
|
| 716 |
# Update both the output text and the document selector
|
| 717 |
-
update_button.click(
|
| 718 |
-
|
| 719 |
-
|
|
|
|
|
|
|
| 720 |
|
| 721 |
# Add the refresh button functionality
|
| 722 |
-
refresh_button.click(
|
| 723 |
-
|
| 724 |
-
|
|
|
|
|
|
|
| 725 |
|
| 726 |
# Add the delete button functionality
|
| 727 |
-
delete_button.click(
|
| 728 |
-
|
| 729 |
-
|
|
|
|
|
|
|
| 730 |
|
| 731 |
gr.Markdown(
|
| 732 |
"""
|
|
|
|
| 143 |
# Save the updated list of documents
|
| 144 |
save_documents(uploaded_documents)
|
| 145 |
|
| 146 |
+
# Return a tuple with the status message and the updated document list
|
| 147 |
return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", display_documents()
|
| 148 |
|
| 149 |
+
|
| 150 |
def delete_documents(selected_docs):
|
| 151 |
global uploaded_documents
|
| 152 |
|
|
|
|
| 383 |
You have to create a comprehensive news summary FOCUSING on the context provided to you.
|
| 384 |
Include key facts, relevant statistics, and expert opinions if available.
|
| 385 |
Ensure the article is well-structured with an introduction, main body, and conclusion, IF NECESSARY.
|
| 386 |
+
Address the query in the context of the ongoing conversation IF APPLICABLE.
|
|
|
|
| 387 |
Cite sources directly within the generated text and not at the end of the generated text, integrating URLs where appropriate to support the information provided:
|
| 388 |
|
| 389 |
{search_context}
|
|
|
|
| 396 |
return f"An error occurred during summarization: {str(e)}"
|
| 397 |
|
| 398 |
# Modify the existing respond function to handle both PDF and web search
|
| 399 |
+
def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|
| 400 |
logging.info(f"User Query: {message}")
|
| 401 |
logging.info(f"Model Used: {model}")
|
| 402 |
logging.info(f"Selected Documents: {selected_docs}")
|
|
|
|
| 438 |
if not relevant_docs:
|
| 439 |
yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
| 440 |
return
|
| 441 |
+
|
| 442 |
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
| 443 |
logging.info(f"Context length: {len(context_str)}")
|
| 444 |
else:
|
|
|
|
| 446 |
yield "No documents available. Please upload PDF documents to answer questions."
|
| 447 |
return
|
| 448 |
|
| 449 |
+
if model.startswith("duckduckgo/"):
|
| 450 |
+
# Use DuckDuckGo chat with context
|
| 451 |
+
for partial_response in get_response_from_duckduckgo(message, model, context_str, num_calls, temperature):
|
| 452 |
+
yield partial_response
|
| 453 |
+
elif model == "@cf/meta/llama-3.1-8b-instruct":
|
| 454 |
# Use Cloudflare API
|
| 455 |
+
for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
|
| 456 |
+
yield partial_response
|
| 457 |
else:
|
| 458 |
# Use Hugging Face API
|
| 459 |
+
for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
| 460 |
+
yield partial_response
|
| 461 |
except Exception as e:
|
| 462 |
logging.error(f"Error with {model}: {str(e)}")
|
| 463 |
if "microsoft/Phi-3-mini-4k-instruct" in model:
|
| 464 |
logging.info("Falling back to Mistral model due to Phi-3 error")
|
| 465 |
fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 466 |
+
yield from respond(message, history, fallback_model, temperature, num_calls, selected_docs)
|
| 467 |
else:
|
| 468 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
| 469 |
+
|
| 470 |
logging.basicConfig(level=logging.DEBUG)
|
| 471 |
|
| 472 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
|
|
|
| 650 |
# Define the checkbox outside the demo block
|
| 651 |
document_selector = gr.CheckboxGroup(label="Select documents to query")
|
| 652 |
|
| 653 |
+
use_web_search = gr.Checkbox(label="Use Web Search", value=False)
|
| 654 |
|
| 655 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
| 656 |
|
|
|
|
| 663 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
| 664 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 665 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
| 666 |
+
gr.Checkbox(label="Use Web Search", value=True),
|
| 667 |
+
gr.CheckboxGroup(label="Select documents to query")
|
| 668 |
],
|
| 669 |
title="AI-powered PDF Chat and Web Search Assistant",
|
| 670 |
description="Chat with your PDFs or use web search to answer questions.",
|
|
|
|
| 705 |
)
|
| 706 |
)
|
| 707 |
|
| 708 |
+
# Add file upload functionality
|
| 709 |
# Add file upload functionality
|
| 710 |
with demo:
|
| 711 |
gr.Markdown("## Upload and Manage PDF Documents")
|
|
|
|
| 712 |
with gr.Row():
|
| 713 |
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
| 714 |
parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
|
|
|
|
| 719 |
delete_button = gr.Button("Delete Selected Documents")
|
| 720 |
|
| 721 |
# Update both the output text and the document selector
|
| 722 |
+
update_button.click(
|
| 723 |
+
update_vectors,
|
| 724 |
+
inputs=[file_input, parser_dropdown],
|
| 725 |
+
outputs=[update_output, demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs
|
| 726 |
+
)
|
| 727 |
|
| 728 |
# Add the refresh button functionality
|
| 729 |
+
refresh_button.click(
|
| 730 |
+
refresh_documents,
|
| 731 |
+
inputs=[],
|
| 732 |
+
outputs=[demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs
|
| 733 |
+
)
|
| 734 |
|
| 735 |
# Add the delete button functionality
|
| 736 |
+
delete_button.click(
|
| 737 |
+
delete_documents,
|
| 738 |
+
inputs=[demo.additional_inputs[-1]], # Use the CheckboxGroup from additional_inputs
|
| 739 |
+
outputs=[update_output, demo.additional_inputs[-1]]
|
| 740 |
+
)
|
| 741 |
|
| 742 |
gr.Markdown(
|
| 743 |
"""
|