Shreyas094 commited on
Commit
812267e
·
verified ·
1 Parent(s): 52a67c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -54
app.py CHANGED
@@ -476,23 +476,16 @@ def display_documents():
476
  label="Select documents to query"
477
  )
478
 
479
- # Define the checkbox outside the demo block
480
- document_selector = gr.CheckboxGroup(label="Select documents to query")
481
-
482
- use_web_search = gr.Checkbox(label="Use Web Search", value=True)
483
-
484
- demo = gr.ChatInterface(
485
- respond,
486
- additional_inputs=[
487
- gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
488
- gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
489
- gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
490
- use_web_search,
491
- document_selector # Add the document selector to the chat interface
492
- ],
493
- title="AI-powered Web Search and PDF Chat Assistant",
494
- description="Chat with your PDFs or use web search to answer questions (Please use toggle under Additional Inputs to swithc between PDF and Web Search, Default Value Web Search)",
495
- theme=gr.themes.Soft(
496
  primary_hue="orange",
497
  secondary_hue="amber",
498
  neutral_hue="gray",
@@ -509,45 +502,69 @@ demo = gr.ChatInterface(
509
  background_fill_secondary_dark="#0c0505",
510
  color_accent_soft_dark="transparent",
511
  code_background_fill_dark="#140b0b"
512
- ),
513
- css=css,
514
- examples=[
515
- ["Tell me about the contents of the uploaded PDFs."],
516
- ["What are the main topics discussed in the documents?"],
517
- ["Can you summarize the key points from the PDFs?"]
518
- ],
519
- cache_examples=False,
520
- analytics_enabled=False,
521
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522
 
523
- # Add file upload functionality
524
- with demo:
525
- gr.Markdown("## Upload PDF Documents")
 
 
 
 
 
 
526
 
527
- with gr.Row():
528
- file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
529
- parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
530
- update_button = gr.Button("Upload Document")
531
-
532
- update_output = gr.Textbox(label="Update Status")
533
-
534
- # Update both the output text and the document selector
535
- update_button.click(update_vectors,
536
- inputs=[file_input, parser_dropdown],
537
- outputs=[update_output, document_selector])
538
-
539
- gr.Markdown(
540
- """
541
- ## How to use
542
- 1. Upload PDF documents using the file input at the top.
543
- 2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
544
- 3. Select the documents you want to query using the checkboxes.
545
- 4. Ask questions in the chat interface.
546
- 5. Toggle "Use Web Search" to switch between PDF chat and web search.
547
- 6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
548
- 7. Use the provided examples or ask your own questions.
549
- """
550
- )
551
 
552
  if __name__ == "__main__":
553
  demo.launch(share=True)
 
476
  label="Select documents to query"
477
  )
478
 
479
+ def create_interface():
480
+ # Define components outside the main interface
481
+ model_dropdown = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3])
482
+ temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")
483
+ api_calls_slider = gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")
484
+ use_web_search = gr.Checkbox(label="Use Web Search", value=True)
485
+ document_selector = gr.CheckboxGroup(label="Select documents to query")
486
+
487
+ # Create the main demo layout
488
+ with gr.Blocks(css=css, theme=gr.themes.Soft(
 
 
 
 
 
 
 
489
  primary_hue="orange",
490
  secondary_hue="amber",
491
  neutral_hue="gray",
 
502
  background_fill_secondary_dark="#0c0505",
503
  color_accent_soft_dark="transparent",
504
  code_background_fill_dark="#140b0b"
505
+ )) as demo:
506
+ gr.Markdown("# AI-powered Web Search and PDF Chat Assistant")
507
+ gr.Markdown("Chat with your PDFs or use web search to answer questions")
508
+
509
+ with gr.Row():
510
+ with gr.Column(scale=3):
511
+ chatbot = gr.Chatbot(height=600)
512
+ msg = gr.Textbox(label="Enter your message")
513
+ clear = gr.Button("Clear")
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
+ return demo
565
+
566
+ # Use the function to create the interface
567
+ demo = create_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
 
569
  if __name__ == "__main__":
570
  demo.launch(share=True)