import gradio as gr import os import tempfile from openai import OpenAI def generate_tts(text, model, voice, api_key,response_format, speed): if api_key == '': raise gr.Error('Please enter your OpenAI API key to run this generator') else: try: client = OpenAI(api_key=api_key) response = client.audio.speech.create( model=model, # tts-1, tts-1-hd voice=voice, # alloy, echo, fable, onyx, nova, shimmer response_format=response_format, # mp3, opus, aac, flac speed=speed, # 0.25x - 4x input=text, ) except Exception as error: # Handle any exception that occurs raise gr.Error("An error occurred while generating speech output. Please check your API key and try again.") print(str(error)) # Create a temp file to save the audio from OpenAI api output_suffix = "." + response_format with tempfile.NamedTemporaryFile(suffix=output_suffix, 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 css = """ .header-text p {line-height: 80px !important; text-align: left; font-size: 26px;} .header-logo {text-align: left} """ with gr.Blocks(css=css) as tts_demo: with gr.Row(): with gr.Column(scale=1, min_width=80): gr.Image("tt-logo.jpg", width=80, height=80, show_download_button=False, show_share_button=False, interactive=False, show_label=False, elem_id="thinktecture-logo", elem_classes="header-logo", container=False) with gr.Column(scale=11): gr.Markdown("OpenAI Text-To-Speech Generator (TTS-1 Model)", elem_classes="header-text") with gr.Row(variant='panel'): api_key = gr.Textbox(type='password', label='OpenAI API Key', placeholder='Your OpenAI API key') 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', value='fable') response_format = gr.Dropdown(choices=['mp3', 'opus', 'aac', 'flac'], label='Output format', value='mp3') speed = gr.Slider(0.25, 4, value=1, step=.25, label="Speed", info="Speach speed (0.25x - 4x)") with gr.Row(variant='panel'): text = gr.Textbox(label="Input text", placeholder="Your input for Text-To-Speech - press 'Say it' or hit 'Enter' key when ready.") with gr.Row(variant='panel'): with gr.Column(scale=1): gr.Markdown(" ") with gr.Column(scale=1): btn = gr.Button("Say it") with gr.Column(scale=1): gr.Markdown(" ") output_audio = gr.Audio(label="Text-To-Speech Output", waveform_options={"waveform_progress_color" : "#ff584f"}) text.submit(fn=generate_tts, inputs=[text, model, voice, api_key, response_format, speed], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None) btn.click(fn=generate_tts, inputs=[text, model, voice, api_key, response_format, speed], outputs=output_audio, api_name="tts_button", concurrency_limit=None) tts_demo.launch()