openaitts / app.py
ysharma's picture
ysharma HF staff
Update app.py
7ebcd98
raw
history blame
No virus
1.3 kB
import gradio as gr
import os
import tempfile
from openai import OpenAI
# Set an environment variable for key
os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')
client = OpenAI() # add api_key
def tts(text, model, voice):
response = client.audio.speech.create(
model=model, #"tts-1","tts-1-hd"
voice=voice, #'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
input=text,
)
# Create a temp file to save the audio
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
temp_file.write(response.content)
# Get the file path of the temp file
temp_file_path = temp_file.name
return temp_file_path
with gr.Blocks() as demo:
gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
with gr.Row():
model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model', value='tts-1')
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')
text = gr.Textbox(label="Input text")
btn = gr.Button("Text-To-Speech")
output_audio = gr.Audio(label="Speech Output")
btn.click(fn=tts, inputs=[text, model, voice], outputs=output_audio, api_name="tts", concurrency_limit=None)
demo.launch()