Shreyas094 commited on
Commit
5ef07bd
·
verified ·
1 Parent(s): ba786cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -20
app.py CHANGED
@@ -412,6 +412,8 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
412
  logging.info(f"Selected Documents: {selected_docs}")
413
  logging.info(f"Use Web Search: {use_web_search}")
414
 
 
 
415
  if use_web_search:
416
  original_query = message
417
  rephrased_query = rephrase_query(message, conversation_manager)
@@ -475,7 +477,15 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
475
  fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
476
  yield from respond(message, history, fallback_model, temperature, num_calls, selected_docs)
477
  else:
478
- yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
 
 
 
 
 
 
 
 
479
 
480
  logging.basicConfig(level=logging.DEBUG)
481
 
@@ -730,8 +740,16 @@ demo = gr.Interface(
730
 
731
  # Add file upload functionality
732
  # Add file upload functionality
733
- with demo:
734
- gr.Markdown("## Chat Interface")
 
 
 
 
 
 
 
 
735
  with gr.Row():
736
  text_input = gr.Textbox(
737
  placeholder=custom_placeholder,
@@ -739,16 +757,32 @@ with demo:
739
  scale=7
740
  )
741
  audio_input = gr.Audio(source="microphone", type="filepath")
742
- submit_button = gr.Button("Submit")
743
 
744
- chat_output = gr.Chatbot(
745
- show_copy_button=True,
746
- likeable=True,
747
- layout="bubble",
748
- height=400,
749
- value=initial_conversation()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
750
  )
751
- with demo:
 
752
  gr.Markdown("## Upload and Manage PDF Documents")
753
  with gr.Row():
754
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
@@ -759,25 +793,22 @@ with demo:
759
  update_output = gr.Textbox(label="Update Status")
760
  delete_button = gr.Button("Delete Selected Documents")
761
 
762
- # Update both the output text and the document selector
763
  update_button.click(
764
  update_vectors,
765
  inputs=[file_input, parser_dropdown],
766
- outputs=[update_output, demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs
767
  )
768
 
769
- # Add the refresh button functionality
770
  refresh_button.click(
771
  refresh_documents,
772
  inputs=[],
773
- outputs=[demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs
774
  )
775
 
776
- # Add the delete button functionality
777
  delete_button.click(
778
  delete_documents,
779
- inputs=[demo.additional_inputs[-1]], # Use the CheckboxGroup from additional_inputs
780
- outputs=[update_output, demo.additional_inputs[-1]]
781
  )
782
 
783
  gr.Markdown(
@@ -786,12 +817,11 @@ with demo:
786
  1. Upload PDF documents using the file input at the top.
787
  2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
788
  3. Select the documents you want to query using the checkboxes.
789
- 4. Ask questions in the chat interface.
790
  5. Toggle "Use Web Search" to switch between PDF chat and web search.
791
  6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
792
  7. Use the provided examples or ask your own questions.
793
  """
794
  )
795
-
796
  if __name__ == "__main__":
797
  demo.launch(share=True)
 
412
  logging.info(f"Selected Documents: {selected_docs}")
413
  logging.info(f"Use Web Search: {use_web_search}")
414
 
415
+ history = history + [(message, "")]
416
+
417
  if use_web_search:
418
  original_query = message
419
  rephrased_query = rephrase_query(message, conversation_manager)
 
477
  fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
478
  yield from respond(message, history, fallback_model, temperature, num_calls, selected_docs)
479
  else:
480
+ yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model
481
+
482
+ history[-1] = (message, response)
483
+ return response, history
484
+ except Exception as e:
485
+ logging.error(f"Error in respond: {str(e)}")
486
+ error_message = f"An error occurred: {str(e)}"
487
+ history[-1] = (message, error_message)
488
+ return error_message, history
489
 
490
  logging.basicConfig(level=logging.DEBUG)
491
 
 
740
 
741
  # Add file upload functionality
742
  # Add file upload functionality
743
+ with gr.Blocks() as demo:
744
+ chatbot = gr.Chatbot(
745
+ show_copy_button=True,
746
+ likeable=True,
747
+ layout="bubble",
748
+ height=400,
749
+ value=initial_conversation()
750
+ )
751
+ state = gr.State([])
752
+
753
  with gr.Row():
754
  text_input = gr.Textbox(
755
  placeholder=custom_placeholder,
 
757
  scale=7
758
  )
759
  audio_input = gr.Audio(source="microphone", type="filepath")
 
760
 
761
+ with gr.Accordion("⚙️ Parameters", open=False):
762
+ model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3])
763
+ temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature")
764
+ num_calls = gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")
765
+ use_web_search = gr.Checkbox(label="Use Web Search", value=True)
766
+ selected_docs = gr.CheckboxGroup(label="Select documents to query")
767
+
768
+ submit_button = gr.Button("Submit")
769
+
770
+ submit_button.click(
771
+ fn=respond,
772
+ inputs=[
773
+ text_input,
774
+ state,
775
+ model,
776
+ temperature,
777
+ num_calls,
778
+ use_web_search,
779
+ selected_docs,
780
+ audio_input
781
+ ],
782
+ outputs=[chatbot, state]
783
  )
784
+
785
+ # Add file upload functionality
786
  gr.Markdown("## Upload and Manage PDF Documents")
787
  with gr.Row():
788
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
 
793
  update_output = gr.Textbox(label="Update Status")
794
  delete_button = gr.Button("Delete Selected Documents")
795
 
 
796
  update_button.click(
797
  update_vectors,
798
  inputs=[file_input, parser_dropdown],
799
+ outputs=[update_output, selected_docs]
800
  )
801
 
 
802
  refresh_button.click(
803
  refresh_documents,
804
  inputs=[],
805
+ outputs=[selected_docs]
806
  )
807
 
 
808
  delete_button.click(
809
  delete_documents,
810
+ inputs=[selected_docs],
811
+ outputs=[update_output, selected_docs]
812
  )
813
 
814
  gr.Markdown(
 
817
  1. Upload PDF documents using the file input at the top.
818
  2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
819
  3. Select the documents you want to query using the checkboxes.
820
+ 4. Ask questions by typing in the text box or using the microphone for speech input.
821
  5. Toggle "Use Web Search" to switch between PDF chat and web search.
822
  6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
823
  7. Use the provided examples or ask your own questions.
824
  """
825
  )
 
826
  if __name__ == "__main__":
827
  demo.launch(share=True)