text2speech / app.py
tregu0458's picture
Update app.py
f8d462b verified
raw
history blame contribute delete
794 Bytes
import gradio as gr
from openai import OpenAI
from pathlib import Path
def generate_audio(text, api_key):
client = OpenAI(api_key=api_key)
speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=text,
)
response.stream_to_file(speech_file_path)
yield speech_file_path
iface = gr.Interface(
fn=generate_audio,
inputs=[
gr.Textbox(label="Enter text to synthesize"),
gr.Textbox(label="Enter OpenAI API Key", type="password")
],
outputs=gr.Audio(streaming=True),
title="OpenAI Text-to-Speech",
description="Enter some text and your OpenAI API key, then click 'Submit' to synthesize speech using OpenAI's API."
)
iface.launch()