Spaces:
Paused
Paused
Shreyas094
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -476,16 +476,42 @@ def display_documents():
|
|
476 |
label="Select documents to query"
|
477 |
)
|
478 |
|
479 |
-
def
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
489 |
primary_hue="orange",
|
490 |
secondary_hue="amber",
|
491 |
neutral_hue="gray",
|
@@ -502,69 +528,45 @@ def create_interface():
|
|
502 |
background_fill_secondary_dark="#0c0505",
|
503 |
color_accent_soft_dark="transparent",
|
504 |
code_background_fill_dark="#140b0b"
|
505 |
-
)
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
with gr.Column(scale=1):
|
516 |
-
model_dropdown
|
517 |
-
temperature_slider
|
518 |
-
api_calls_slider
|
519 |
-
use_web_search
|
520 |
-
document_selector
|
521 |
-
|
522 |
-
gr.Markdown("## Upload PDF Documents")
|
523 |
-
with gr.Row():
|
524 |
-
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
525 |
-
parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
|
526 |
-
update_button = gr.Button("Upload Document")
|
527 |
-
|
528 |
-
update_output = gr.Textbox(label="Update Status")
|
529 |
-
|
530 |
-
# Update both the output text and the document selector
|
531 |
-
update_button.click(update_vectors,
|
532 |
-
inputs=[file_input, parser_dropdown],
|
533 |
-
outputs=[update_output, document_selector])
|
534 |
-
|
535 |
-
# Set up the chat functionality
|
536 |
-
msg.submit(chatbot_interface,
|
537 |
-
inputs=[msg, chatbot, use_web_search, model_dropdown, temperature_slider, api_calls_slider, document_selector],
|
538 |
-
outputs=[chatbot])
|
539 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
540 |
-
|
541 |
-
gr.Markdown(
|
542 |
-
"""
|
543 |
-
## How to use
|
544 |
-
1. Upload PDF documents using the file input at the top.
|
545 |
-
2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
|
546 |
-
3. Select the documents you want to query using the checkboxes.
|
547 |
-
4. Ask questions in the chat interface.
|
548 |
-
5. Toggle "Use Web Search" to switch between PDF chat and web search.
|
549 |
-
6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
|
550 |
-
7. Use the provided examples or ask your own questions.
|
551 |
-
"""
|
552 |
-
)
|
553 |
-
|
554 |
-
# Add examples
|
555 |
-
gr.Examples(
|
556 |
-
examples=[
|
557 |
-
["Tell me about the contents of the uploaded PDFs."],
|
558 |
-
["What are the main topics discussed in the documents?"],
|
559 |
-
["Can you summarize the key points from the PDFs?"]
|
560 |
-
],
|
561 |
-
inputs=msg,
|
562 |
-
)
|
563 |
|
564 |
-
|
|
|
|
|
565 |
|
566 |
-
|
567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
568 |
|
569 |
if __name__ == "__main__":
|
570 |
demo.launch(share=True)
|
|
|
476 |
label="Select documents to query"
|
477 |
)
|
478 |
|
479 |
+
def chat_with_parameters(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|
480 |
+
"""
|
481 |
+
This function wraps the chatbot functionality with all parameters.
|
482 |
+
It will be used as the main function for gr.Interface.
|
483 |
+
"""
|
484 |
+
try:
|
485 |
+
for response in respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
|
486 |
+
history.append((message, response))
|
487 |
+
yield history
|
488 |
+
except Exception as e:
|
489 |
+
history.append((message, f"An error occurred: {str(e)}"))
|
490 |
+
yield history
|
491 |
+
|
492 |
+
# Define the components that were previously in additional_inputs
|
493 |
+
model_dropdown = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3])
|
494 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")
|
495 |
+
api_calls_slider = gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")
|
496 |
+
use_web_search = gr.Checkbox(label="Use Web Search", value=True)
|
497 |
+
document_selector = gr.CheckboxGroup(label="Select documents to query")
|
498 |
+
|
499 |
+
# Create the main interface
|
500 |
+
demo = gr.Interface(
|
501 |
+
fn=chat_with_parameters,
|
502 |
+
inputs=[
|
503 |
+
gr.Textbox(lines=2, label="Chat Input"),
|
504 |
+
"state",
|
505 |
+
model_dropdown,
|
506 |
+
temperature_slider,
|
507 |
+
api_calls_slider,
|
508 |
+
use_web_search,
|
509 |
+
document_selector
|
510 |
+
],
|
511 |
+
outputs=gr.Chatbot(),
|
512 |
+
title="AI-powered Web Search and PDF Chat Assistant",
|
513 |
+
description="Chat with your PDFs or use web search to answer questions",
|
514 |
+
theme=gr.themes.Soft(
|
515 |
primary_hue="orange",
|
516 |
secondary_hue="amber",
|
517 |
neutral_hue="gray",
|
|
|
528 |
background_fill_secondary_dark="#0c0505",
|
529 |
color_accent_soft_dark="transparent",
|
530 |
code_background_fill_dark="#140b0b"
|
531 |
+
),
|
532 |
+
css=css,
|
533 |
+
examples=[
|
534 |
+
["Tell me about the contents of the uploaded PDFs."],
|
535 |
+
["What are the main topics discussed in the documents?"],
|
536 |
+
["Can you summarize the key points from the PDFs?"]
|
537 |
+
],
|
538 |
+
cache_examples=False,
|
539 |
+
analytics_enabled=False,
|
540 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
541 |
|
542 |
+
# Add file upload functionality
|
543 |
+
with demo:
|
544 |
+
gr.Markdown("## Upload PDF Documents")
|
545 |
|
546 |
+
with gr.Row():
|
547 |
+
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
548 |
+
parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
|
549 |
+
update_button = gr.Button("Upload Document")
|
550 |
+
|
551 |
+
update_output = gr.Textbox(label="Update Status")
|
552 |
+
|
553 |
+
# Update both the output text and the document selector
|
554 |
+
update_button.click(update_vectors,
|
555 |
+
inputs=[file_input, parser_dropdown],
|
556 |
+
outputs=[update_output, document_selector])
|
557 |
+
|
558 |
+
gr.Markdown(
|
559 |
+
"""
|
560 |
+
## How to use
|
561 |
+
1. Upload PDF documents using the file input at the top.
|
562 |
+
2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
|
563 |
+
3. Select the documents you want to query using the checkboxes.
|
564 |
+
4. Ask questions in the chat interface.
|
565 |
+
5. Toggle "Use Web Search" to switch between PDF chat and web search.
|
566 |
+
6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
|
567 |
+
7. Use the provided examples or ask your own questions.
|
568 |
+
"""
|
569 |
+
)
|
570 |
|
571 |
if __name__ == "__main__":
|
572 |
demo.launch(share=True)
|