Shreyas094 commited on
Commit
1dec773
·
verified ·
1 Parent(s): e2c3e17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -17,6 +17,11 @@ from huggingface_hub import InferenceClient
17
  import inspect
18
  import logging
19
  import shutil
 
 
 
 
 
20
 
21
 
22
  # Set up basic configuration for logging
@@ -28,6 +33,7 @@ llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
28
  ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
29
  API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
30
  API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"
 
31
 
32
  print(f"ACCOUNT_ID: {ACCOUNT_ID}")
33
  print(f"CLOUDFLARE_AUTH_TOKEN: {API_TOKEN[:5]}..." if API_TOKEN else "Not set")
@@ -397,7 +403,16 @@ def summarize_web_results(query: str, search_results: List[Dict[str, str]], conv
397
  return f"An error occurred during summarization: {str(e)}"
398
 
399
  # Modify the existing respond function to handle both PDF and web search
400
- def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
 
 
 
 
 
 
 
 
 
401
  logging.info(f"User Query: {message}")
402
  logging.info(f"Model Used: {model}")
403
  logging.info(f"Selected Documents: {selected_docs}")
@@ -606,6 +621,13 @@ Write a detailed and complete response that answers the following user question:
606
 
607
  logging.info("Finished generating response")
608
 
 
 
 
 
 
 
 
609
  def vote(data: gr.LikeData):
610
  if data.liked:
611
  print(f"You upvoted this response: {data.value}")
@@ -659,16 +681,16 @@ custom_placeholder = "Ask a question (Note: You can toggle between Web Search an
659
  # Update the Gradio interface
660
  demo = gr.ChatInterface(
661
  respond,
662
- additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),
663
  additional_inputs=[
664
  gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
665
  gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
666
  gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
667
  gr.Checkbox(label="Use Web Search", value=True),
668
- gr.CheckboxGroup(label="Select documents to query")
 
669
  ],
670
  title="AI-powered PDF Chat and Web Search Assistant",
671
- description="Chat with your PDFs or use web search to answer questions.",
672
  theme=gr.themes.Soft(
673
  primary_hue="orange",
674
  secondary_hue="amber",
 
17
  import inspect
18
  import logging
19
  import shutil
20
+ import soundfile as sf
21
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
22
+ from transformers import pipeline
23
+ import numpy as np
24
+
25
 
26
 
27
  # Set up basic configuration for logging
 
33
  ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
34
  API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
35
  API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"
36
+ WHISPER_API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"
37
 
38
  print(f"ACCOUNT_ID: {ACCOUNT_ID}")
39
  print(f"CLOUDFLARE_AUTH_TOKEN: {API_TOKEN[:5]}..." if API_TOKEN else "Not set")
 
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
415
+
416
  logging.info(f"User Query: {message}")
417
  logging.info(f"Model Used: {model}")
418
  logging.info(f"Selected Documents: {selected_docs}")
 
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):
632
  if data.liked:
633
  print(f"You upvoted this response: {data.value}")
 
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(source="microphone", type="filepath", label="Voice Input")
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",