Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
def text_to_speech(text):
|
| 9 |
-
# Using OpenAI's text-to-speech API
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
voice="nova"
|
|
|
|
| 15 |
)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
return
|
|
|
|
| 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 |
+
|