deekshitha9876 commited on
Commit
7c45000
·
verified ·
1 Parent(s): a88694e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -1,28 +1,30 @@
 
1
  import openai
2
  import gradio as gr
3
- import os
4
 
5
- # Make sure the OpenAI API key is set properly
6
- openai.api_key = os.getenv("OPENAI_API_KEY") # Ensure this key is set in Hugging Face secrets
7
 
8
  def text_to_speech(text):
9
- # Using OpenAI's text-to-speech API
10
  try:
11
- response = openai.Audio.create(
12
- model="text-to-speech", # Check that this is correct for text-to-speech
13
- input=text,
14
- voice="nova" # Options could be "nova", "shimmer", etc.
 
15
  )
16
- filename = "output.mp3"
17
- with open(filename, "wb") as f:
18
- f.write(response['data'])
19
- return filename
 
20
  except Exception as e:
21
- return f"Error: {str(e)}"
 
22
 
23
  iface = gr.Interface(
24
  fn=text_to_speech,
25
- inputs=gr.Textbox(lines=2, placeholder="Enter text to convert to speech..."),
26
  outputs=gr.Audio(type="filepath"),
27
  title="Text to Speech with OpenAI",
28
  description="Enter text and get the spoken audio!"
@@ -30,3 +32,4 @@ iface = gr.Interface(
30
 
31
  if __name__ == "__main__":
32
  iface.launch()
 
 
1
+ import os
2
  import openai
3
  import gradio as gr
 
4
 
5
+ # read key from env
6
+ openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
  def text_to_speech(text):
 
9
  try:
10
+ # call the TTS endpoint
11
+ response = openai.audio.speech.create(
12
+ model="tts-1", # or "tts-1-hd"
13
+ voice="nova", # nova | shimmer | echo | fable | onyx | alloy
14
+ input=text
15
  )
16
+ # response.content holds the raw mp3 bytes
17
+ out_path = "output.mp3"
18
+ with open(out_path, "wb") as f:
19
+ f.write(response.content)
20
+ return out_path
21
  except Exception as e:
22
+ # return the error message so you see it in the UI
23
+ return f"Error: {e}"
24
 
25
  iface = gr.Interface(
26
  fn=text_to_speech,
27
+ inputs=gr.Textbox(lines=2, placeholder="Enter text to convert to speech"),
28
  outputs=gr.Audio(type="filepath"),
29
  title="Text to Speech with OpenAI",
30
  description="Enter text and get the spoken audio!"
 
32
 
33
  if __name__ == "__main__":
34
  iface.launch()
35
+