PhilSpiel commited on
Commit
b65c921
·
1 Parent(s): 836d62c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
  import os
 
 
3
  from openai import OpenAI
4
 
5
  # Initialize OpenAI API client with API key
@@ -20,20 +22,21 @@ def generate_speech(input_text):
20
  # Extract generated text (response by the assistant) from OpenAI's API response
21
  message_content = completion.choices[0].message.content.strip() # Remove whitespace from the ends
22
 
23
- # OpenAI TTS synthesis
24
- try:
25
- response = client.voice.create(
26
- model="tts-1",
27
- voice="alloy",
28
- text=message_content
29
- )
30
-
31
- # Return the audio file URL if successful
32
- audio_url = response['url']
33
- return audio_url
34
- except Exception as e:
35
- print("Error with OpenAI TTS API:", e)
36
- raise Exception(f"Failed to generate speech: {e}")
 
37
 
38
  # Define the Gradio interface
39
  iface = gr.Interface(
 
1
  import gradio as gr
2
  import os
3
+ import requests
4
+ import re
5
  from openai import OpenAI
6
 
7
  # Initialize OpenAI API client with API key
 
22
  # Extract generated text (response by the assistant) from OpenAI's API response
23
  message_content = completion.choices[0].message.content.strip() # Remove whitespace from the ends
24
 
25
+ # Prepare the text for TTS conversion (you may want to process the text further)
26
+ #text_to_speech = message_content
27
+
28
+ response = client.audio.speech.create(
29
+ model="tts-1",
30
+ voice="alloy",
31
+ input=str(message_content)
32
+ )
33
+
34
+ # Return the response content directly if successful, otherwise print error details
35
+ if response.status_code == 200:
36
+ return response.content
37
+ else:
38
+ print("Error with ElevenLabs API:", response.status_code, response.text)
39
+ raise Exception(f"Failed to generate speech, status code: {response.status_code}, response: {response.text}")
40
 
41
  # Define the Gradio interface
42
  iface = gr.Interface(