quick-tts / app.py
vMyth's picture
Update app.py
57765ce verified
import tempfile
import gradio as gr
from openai import OpenAI
def text_to_audio(api_key, text, voice_name):
"""
Convert text to speech using OpenAI's TTS API
"""
# Input validation
if not api_key or not api_key.strip():
raise gr.Error("Please provide an OpenAI API key")
if not text or not text.strip():
raise gr.Error("Please provide some text to convert to speech")
try:
# Initialize OpenAI client with the provided API key
client = OpenAI(api_key=api_key.strip())
# Create a temporary file to store the audio
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio_file:
speech_file_path = temp_audio_file.name
# Generate speech using OpenAI's API
response = client.audio.speech.create(
model="tts-1",
voice=voice_name.strip(), # Ensure voice name is properly formatted
input=text.strip()
)
# Save the audio to the temporary file
response.stream_to_file(speech_file_path)
return speech_file_path
except Exception as e:
raise gr.Error(f"Error: {str(e)}")
# Create the Gradio interface
demo = gr.Interface(
fn=text_to_audio,
inputs=[
gr.Textbox(
label="OpenAI API Key",
placeholder="Enter your OpenAI API key",
type="password"
),
gr.Textbox(
label="Enter Text",
placeholder="Enter the text you want to convert to speech",
lines=6
),
gr.Dropdown(
choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
label="Select Voice",
value="nova"
)
],
outputs=gr.Audio(
label="Generated Audio",
type="filepath"
),
title="Text to Speech with OpenAI",
description="Convert text to speech using OpenAI's TTS API. Enter your API key (not stored), type your text, and select a voice.",
)
# Launch the app
if __name__ == "__main__":
demo.launch()
else:
demo.launch(show_error=True)