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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -38
app.py CHANGED
@@ -1,15 +1,10 @@
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
8
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
9
 
10
- # Set the ElevenLabs API key using an environment variable
11
- elevenlabs_api_key = os.getenv("ELEVENLABS_API_KEY")
12
-
13
  # Define the function that generates the speech
14
  def generate_speech(input_text):
15
  # Build completion with OpenAI
@@ -25,39 +20,20 @@ def generate_speech(input_text):
25
  # Extract generated text (response by the assistant) from OpenAI's API response
26
  message_content = completion.choices[0].message.content.strip() # Remove whitespace from the ends
27
 
28
- # Prepare the text for TTS conversion (you may want to process the text further)
29
- text_to_speech = message_content
30
-
31
- # ElevenLabs TTS API settings and request
32
- url = "https://api.elevenlabs.io/v1/text-to-speech/eIH76iW9yjOWnOKIkd6y/stream"
33
- headers = {
34
- "Accept": "audio/mpeg",
35
- "Content-Type": "application/json",
36
- "xi-api-key": elevenlabs_api_key
37
- }
38
- data = {
39
- "text": text_to_speech,
40
- "model_id": "eleven_multilingual_v2",
41
- "voice_settings": {
42
- "stability": 1.0,
43
- "similarity_boost": 1.0,
44
- "excitement": 0.9,
45
- "speed": 1.1,
46
- "volume": 80,
47
- "pitch": 2.0,
48
- "breathiness": 0.8,
49
- "voice_id": "eIH76iW9yjOWnOKIkd6y"
50
- }
51
- }
52
-
53
- response = requests.post(url, json=data, headers=headers)
54
-
55
- # Return the response content directly if successful, otherwise print error details
56
- if response.status_code == 200:
57
- return response.content
58
- else:
59
- print("Error with ElevenLabs API:", response.status_code, response.text)
60
- raise Exception(f"Failed to generate speech, status code: {response.status_code}, response: {response.text}")
61
 
62
  # Define the Gradio interface
63
  iface = gr.Interface(
 
1
  import gradio as gr
2
  import os
 
 
3
  from openai import OpenAI
4
 
5
  # Initialize OpenAI API client with API key
6
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
7
 
 
 
 
8
  # Define the function that generates the speech
9
  def generate_speech(input_text):
10
  # Build completion with OpenAI
 
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(