Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
# Set an environment variable for key
|
| 7 |
+
os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')
|
| 8 |
+
|
| 9 |
+
client = OpenAI() # add api_key
|
| 10 |
+
|
| 11 |
+
def tts(text):
|
| 12 |
+
response = client.audio.speech.create(
|
| 13 |
+
model="tts-1",
|
| 14 |
+
voice="alloy",
|
| 15 |
+
input=text,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Create a temp file to save the audio
|
| 19 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
| 20 |
+
temp_file.write(response.content)
|
| 21 |
+
|
| 22 |
+
# Get the file path of the temp file
|
| 23 |
+
temp_file_path = temp_file.name
|
| 24 |
+
|
| 25 |
+
return temp_file_path
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
with gr.Blocks() as demo:
|
| 29 |
+
with gr.Row():
|
| 30 |
+
dd1 = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model')
|
| 31 |
+
dd2 = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options')
|
| 32 |
+
|
| 33 |
+
text = gr.Textbox(label="Input text")
|
| 34 |
+
btn = gr.Button("Greet")
|
| 35 |
+
output_audio = gr.Audio(label="Speech Output")
|
| 36 |
+
|
| 37 |
+
btn.click(fn=tts, inputs=text, outputs=output_audio, api_name="tts")
|
| 38 |
+
|
| 39 |
+
demo.launch()
|