|
import gradio as gr |
|
import os |
|
import tempfile |
|
from openai import OpenAI |
|
|
|
|
|
os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY') |
|
|
|
client = OpenAI() |
|
|
|
def tts(text): |
|
response = client.audio.speech.create( |
|
model="tts-1", |
|
voice="alloy", |
|
input=text, |
|
) |
|
|
|
|
|
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: |
|
temp_file.write(response.content) |
|
|
|
|
|
temp_file_path = temp_file.name |
|
|
|
return temp_file_path |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
dd1 = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model') |
|
dd2 = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options') |
|
|
|
text = gr.Textbox(label="Input text") |
|
btn = gr.Button("Greet") |
|
output_audio = gr.Audio(label="Speech Output") |
|
|
|
btn.click(fn=tts, inputs=text, outputs=output_audio, api_name="tts") |
|
|
|
demo.launch() |
|
|