import gradio as gr import os import tempfile from openai import OpenAI def tts(text, model, voice, api_key): if api_key == '': raise gr.Error('Please enter your OpenAI API Key') else: try: client = openai.OpenAI(api_key=api_key) response = client.audio.speech.create( model='tts-1', # "tts-1","tts-1-hd" voice='alloy', # 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer' input='input text', ) except Exception as error: # Handle any exception that occurs raise gr.Error("An error occurred while generating speech. Please check your API key and try again.") print(str(error)) response = client.audio.speech.create( model=model, # expected values: "tts-1","tts-1-hd" voice=voice, # expected values: '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("#