Pijush2023 commited on
Commit
7702656
·
verified ·
1 Parent(s): b0559af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py CHANGED
@@ -147,14 +147,49 @@ def get_response(question):
147
  def clear_fields():
148
  return "", ""
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  # Create the Gradio Blocks interface
151
  with gr.Blocks() as demo:
152
  question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
153
  response_output = gr.Textbox(label="Response", placeholder="The response will appear here...", interactive=False)
 
154
  get_response_btn = gr.Button("Get Response")
 
155
  clean_btn = gr.Button("Clean")
156
 
157
  get_response_btn.click(fn=get_response, inputs=question_input, outputs=response_output)
 
158
  clean_btn.click(fn=clear_fields, inputs=[], outputs=[question_input, response_output])
159
 
160
  # Launch the Gradio interface
 
147
  def clear_fields():
148
  return "", ""
149
 
150
+ # Function to generate audio with Eleven Labs TTS
151
+ def generate_audio_elevenlabs(text):
152
+ XI_API_KEY = os.environ['ELEVENLABS_API']
153
+ VOICE_ID = 'ehbJzYLQFpwbJmGkqbnW'
154
+ tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
155
+ headers = {
156
+ "Accept": "application/json",
157
+ "xi-api-key": XI_API_KEY
158
+ }
159
+ data = {
160
+ "text": str(text),
161
+ "model_id": "eleven_multilingual_v2",
162
+ "voice_settings": {
163
+ "stability": 1.0,
164
+ "similarity_boost": 0.0,
165
+ "style": 0.60,
166
+ "use_speaker_boost": False
167
+ }
168
+ }
169
+ response = requests.post(tts_url, headers=headers, json=data, stream=True)
170
+ if response.ok:
171
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
172
+ for chunk in response.iter_content(chunk_size=1024):
173
+ if chunk:
174
+ f.write(chunk)
175
+ audio_path = f.name
176
+ logging.debug(f"Audio saved to {audio_path}")
177
+ return audio_path
178
+ else:
179
+ logging.error(f"Error generating audio: {response.text}")
180
+ return None
181
+
182
  # Create the Gradio Blocks interface
183
  with gr.Blocks() as demo:
184
  question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
185
  response_output = gr.Textbox(label="Response", placeholder="The response will appear here...", interactive=False)
186
+ audio_output = gr.Audio(label="Audio", type="filepath", interactive=False)
187
  get_response_btn = gr.Button("Get Response")
188
+ generate_audio_btn = gr.Button("Generate Audio")
189
  clean_btn = gr.Button("Clean")
190
 
191
  get_response_btn.click(fn=get_response, inputs=question_input, outputs=response_output)
192
+ generate_audio_btn.click(fn=generate_audio_elevenlabs, inputs=response_output, outputs=audio_output)
193
  clean_btn.click(fn=clear_fields, inputs=[], outputs=[question_input, response_output])
194
 
195
  # Launch the Gradio interface