ariankhalfani commited on
Commit
a75c3a6
·
verified ·
1 Parent(s): a976a31

Rename LLMwithvoice.py to app.py

Browse files
Files changed (1) hide show
  1. LLMwithvoice.py → app.py +26 -6
LLMwithvoice.py → app.py RENAMED
@@ -2,6 +2,7 @@ import requests
2
  from pydub import AudioSegment
3
  from io import BytesIO
4
  import gradio as gr
 
5
 
6
  # Hugging Face API URLs
7
  API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
@@ -38,10 +39,9 @@ def generate_speech(api_token, answer):
38
  response.raise_for_status() # Raise an error for bad responses
39
  audio = response.content
40
  audio_segment = AudioSegment.from_file(BytesIO(audio), format="wav")
41
- audio_file = BytesIO()
42
- audio_segment.export(audio_file, format="wav")
43
- audio_file.seek(0)
44
- return audio_file
45
  except requests.exceptions.HTTPError as e:
46
  print(f"HTTP error occurred: {e}")
47
  return None
@@ -55,5 +55,25 @@ def gradio_interface(api_token, context, prompt):
55
  if 'error' in answer:
56
  return answer['error'], None
57
  answer_text = answer.get('answer', 'No answer found')
58
- audio_file = generate_speech(api_token, answer_text)
59
- return answer_text, audio_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from pydub import AudioSegment
3
  from io import BytesIO
4
  import gradio as gr
5
+ import tempfile
6
 
7
  # Hugging Face API URLs
8
  API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
 
39
  response.raise_for_status() # Raise an error for bad responses
40
  audio = response.content
41
  audio_segment = AudioSegment.from_file(BytesIO(audio), format="wav")
42
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
43
+ audio_segment.export(temp_file.name, format="wav")
44
+ return temp_file.name
 
45
  except requests.exceptions.HTTPError as e:
46
  print(f"HTTP error occurred: {e}")
47
  return None
 
55
  if 'error' in answer:
56
  return answer['error'], None
57
  answer_text = answer.get('answer', 'No answer found')
58
+ audio_file_path = generate_speech(api_token, answer_text)
59
+ return answer_text, audio_file_path
60
+
61
+ # Define the Gradio interface
62
+ iface = gr.Interface(
63
+ fn=gradio_interface,
64
+ inputs=[
65
+ gr.Textbox(type="password", lines=1, label="Hugging Face API Token", placeholder="Enter your Hugging Face API token here..."),
66
+ gr.Textbox(lines=2, label="Context", placeholder="Enter the context here..."),
67
+ gr.Textbox(lines=1, label="Question", placeholder="Enter your question here...")
68
+ ],
69
+ outputs=[
70
+ gr.Textbox(label="Answer"),
71
+ gr.Audio(label="Answer as Speech", type="filepath") # Changed to filepath type
72
+ ],
73
+ title="Chat with Roberta with Voice",
74
+ description="Ask questions based on a provided context using the Roberta model and hear the response via text-to-speech."
75
+ )
76
+
77
+ # Launch the Gradio app
78
+ if __name__ == "__main__":
79
+ iface.launch()