Shreyas094 commited on
Commit
13ed9c1
·
verified ·
1 Parent(s): bbb5684

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -58
app.py CHANGED
@@ -402,13 +402,28 @@ def summarize_web_results(query: str, search_results: List[Dict[str, str]], conv
402
  except Exception as e:
403
  return f"An error occurred during summarization: {str(e)}"
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  # Modify the existing respond function to handle both PDF and web search
406
- def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs, audio_input):
407
- if audio_input is not None:
408
  # If audio input is provided, transcribe it
409
  try:
410
- message = transcribe(audio_input)
411
- print(f"Transcribed audio: {message}")
 
412
  except Exception as e:
413
  print(f"Error transcribing audio: {str(e)}")
414
  return f"Error transcribing audio: {str(e)}", history
@@ -621,11 +636,12 @@ Write a detailed and complete response that answers the following user question:
621
 
622
  logging.info("Finished generating response")
623
 
624
- def transcribe(audio_path):
 
 
 
625
  headers = {"Authorization": f"Bearer {huggingface_token}"}
626
- with open(audio_path, "rb") as f:
627
- data = f.read()
628
- response = requests.post(WHISPER_API_URL, headers=headers, data=data)
629
  return response.json().get("text", "")
630
 
631
  def vote(data: gr.LikeData):
@@ -677,57 +693,60 @@ use_web_search = gr.Checkbox(label="Use Web Search", value=False)
677
 
678
  custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
679
 
680
- # Update the demo interface
681
- # Update the Gradio interface
682
- demo = gr.ChatInterface(
683
- respond,
684
- additional_inputs=[
685
- gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
686
- gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
687
- gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
688
- gr.Checkbox(label="Use Web Search", value=True),
689
- gr.CheckboxGroup(label="Select documents to query"),
690
- gr.Audio(sources=["microphone"], streaming=True)
691
- ],
692
- title="AI-powered PDF Chat and Web Search Assistant",
693
- description="Chat with your PDFs, use web search to answer questions, or speak your query.",
694
- theme=gr.themes.Soft(
695
- primary_hue="orange",
696
- secondary_hue="amber",
697
- neutral_hue="gray",
698
- font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
699
- ).set(
700
- body_background_fill_dark="#0c0505",
701
- block_background_fill_dark="#0c0505",
702
- block_border_width="1px",
703
- block_title_background_fill_dark="#1b0f0f",
704
- input_background_fill_dark="#140b0b",
705
- button_secondary_background_fill_dark="#140b0b",
706
- border_color_accent_dark="#1b0f0f",
707
- border_color_primary_dark="#1b0f0f",
708
- background_fill_secondary_dark="#0c0505",
709
- color_accent_soft_dark="transparent",
710
- code_background_fill_dark="#140b0b"
711
- ),
712
- css=css,
713
- examples=[
714
- ["Tell me about the contents of the uploaded PDFs."],
715
- ["What are the main topics discussed in the documents?"],
716
- ["Can you summarize the key points from the PDFs?"],
717
- ["What's the latest news about artificial intelligence?"]
718
- ],
719
- cache_examples=False,
720
- analytics_enabled=False,
721
- textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDFs or any topic", container=False, scale=7),
722
- chatbot = gr.Chatbot(
723
- show_copy_button=True,
724
- likeable=True,
725
- layout="bubble",
726
- height=400,
727
- value=initial_conversation()
 
 
 
 
 
728
  )
729
- )
730
-
731
  # Add file upload functionality
732
  # Add file upload functionality
733
  with demo:
 
402
  except Exception as e:
403
  return f"An error occurred during summarization: {str(e)}"
404
 
405
+ def process_input(message, history, audio, model, temperature, num_calls, use_web_search, selected_docs):
406
+ if audio is not None:
407
+ # If audio input is provided, transcribe it
408
+ try:
409
+ transcribed_text = transcribe(audio)
410
+ print(f"Transcribed audio: {transcribed_text}")
411
+ message = transcribed_text
412
+ except Exception as e:
413
+ print(f"Error transcribing audio: {str(e)}")
414
+ return f"Error transcribing audio: {str(e)}", history
415
+
416
+ # Now call the existing respond function with the message (either text or transcribed audio)
417
+ return respond(message, history, model, temperature, num_calls, use_web_search, selected_docs)
418
+
419
  # Modify the existing respond function to handle both PDF and web search
420
+ def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
421
+ if audio is not None:
422
  # If audio input is provided, transcribe it
423
  try:
424
+ transcribed_text = transcribe(audio)
425
+ print(f"Transcribed audio: {transcribed_text}")
426
+ message = transcribed_text
427
  except Exception as e:
428
  print(f"Error transcribing audio: {str(e)}")
429
  return f"Error transcribing audio: {str(e)}", history
 
636
 
637
  logging.info("Finished generating response")
638
 
639
+ def transcribe(audio):
640
+ if audio is None:
641
+ return ""
642
+
643
  headers = {"Authorization": f"Bearer {huggingface_token}"}
644
+ response = requests.post(WHISPER_API_URL, headers=headers, data=audio)
 
 
645
  return response.json().get("text", "")
646
 
647
  def vote(data: gr.LikeData):
 
693
 
694
  custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
695
 
696
+ # Create the Gradio interface
697
+ with gr.Blocks() as demo:
698
+ gr.Markdown("# AI-powered PDF Chat and Web Search Assistant")
699
+
700
+ audio_input = gr.Audio(sources=["microphone"], type="numpy", label="Speak your query")
701
+
702
+ chat_interface = gr.ChatInterface(
703
+ fn=process_input,
704
+ additional_inputs=[
705
+ gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
706
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
707
+ gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
708
+ gr.Checkbox(label="Use Web Search", value=True),
709
+ gr.CheckboxGroup(label="Select documents to query"),
710
+ audio_input
711
+ ],
712
+ title="AI-powered PDF Chat and Web Search Assistant",
713
+ description="Chat with your PDFs, use web search to answer questions, or speak your query.",
714
+ theme=gr.themes.Soft(
715
+ primary_hue="orange",
716
+ secondary_hue="amber",
717
+ neutral_hue="gray",
718
+ font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
719
+ ).set(
720
+ body_background_fill_dark="#0c0505",
721
+ block_background_fill_dark="#0c0505",
722
+ block_border_width="1px",
723
+ block_title_background_fill_dark="#1b0f0f",
724
+ input_background_fill_dark="#140b0b",
725
+ button_secondary_background_fill_dark="#140b0b",
726
+ border_color_accent_dark="#1b0f0f",
727
+ border_color_primary_dark="#1b0f0f",
728
+ background_fill_secondary_dark="#0c0505",
729
+ color_accent_soft_dark="transparent",
730
+ code_background_fill_dark="#140b0b"
731
+ ),
732
+ css=css,
733
+ examples=[
734
+ ["Tell me about the contents of the uploaded PDFs."],
735
+ ["What are the main topics discussed in the documents?"],
736
+ ["Can you summarize the key points from the PDFs?"],
737
+ ["What's the latest news about artificial intelligence?"]
738
+ ],
739
+ cache_examples=False,
740
+ analytics_enabled=False,
741
+ textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDFs or any topic", container=False, scale=7),
742
+ chatbot = gr.Chatbot(
743
+ show_copy_button=True,
744
+ likeable=True,
745
+ layout="bubble",
746
+ height=400,
747
+ value=initial_conversation()
748
+ )
749
  )
 
 
750
  # Add file upload functionality
751
  # Add file upload functionality
752
  with demo: