import gradio as gr import tempfile import openai def tts(input_text: str, model: str, voice: str, api_key: str) -> str: """ Convert input text to speech using OpenAI's Text-to-Speech API. :param input_text: The text to be converted to speech. :type input_text: str :param model: The model to use for synthesis (e.g., 'tts-1', 'tts-1-hd'). :type model: str :param voice: The voice profile to use (e.g., 'alloy', 'echo', 'fable', etc.). :type voice: str :param api_key: OpenAI API key. :type api_key: str :return: File path to the generated audio file. :rtype: str :raises gr.Error: If input parameters are invalid or API call fails. """ if not input_text.strip(): raise gr.Error("Input text cannot be empty.") if not api_key.strip(): raise gr.Error("API key is required.") openai.api_key = api_key try: response = openai.Audio.create( text=input_text, voice=voice, model=model ) except openai.error.Timeout as e: raise gr.Error(f"OpenAI API request timed out: {e}") except openai.error.APIError as e: raise gr.Error(f"OpenAI API returned an API Error: {e}") except openai.error.APIConnectionError as e: raise gr.Error(f"OpenAI API request failed to connect: {e}") except openai.error.InvalidRequestError as e: raise gr.Error(f"OpenAI API request was invalid: {e}") except openai.error.AuthenticationError as e: raise gr.Error(f"OpenAI API request was not authorized: {e}") except openai.error.PermissionError as e: raise gr.Error(f"OpenAI API request was not permitted: {e}") except openai.error.RateLimitError as e: raise gr.Error(f"OpenAI API request exceeded rate limit: {e}") except openai.error.OpenAIError as e: raise gr.Error(f"OpenAI API Error: {e}") except Exception as e: raise gr.Error(f"An unexpected error occurred: {e}") if not hasattr(response, 'audio'): raise gr.Error("Invalid response from OpenAI API. The response does not contain audio content.") with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: temp_file.write(response.audio) temp_file_path = temp_file.name return temp_file_path def main(): """ Main function to create and launch the Gradio interface with input validation and error handling. """ MODEL_OPTIONS = ["tts-1", "tts-1-hd"] VOICE_OPTIONS = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"] with gr.Blocks() as demo: with gr.Row(): with gr.Column(scale=1): api_key_input = gr.Textbox( label="API Key", info="Get API key at: [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)", type="password", placeholder="Enter your OpenAI API Key", value="", ) model_dropdown = gr.Dropdown( choices=MODEL_OPTIONS, label="Model", value="tts-1" ) voice_dropdown = gr.Dropdown( choices=VOICE_OPTIONS, label="Voice Options", value="echo" ) with gr.Column(scale=2): input_textbox = gr.Textbox( label="Input Text", lines=10, placeholder="Type your text here..." ) submit_button = gr.Button( "Convert Text to Speech", variant="primary", interactive=False # Initially disabled ) with gr.Column(scale=1): output_audio = gr.Audio(label="Output Audio") # Define the event handler for the submit button with error handling def on_submit(input_text, model, voice, api_key): try: audio_file = tts(input_text, model, voice, api_key) return audio_file except gr.Error as err: # Re-raise gr.Error exceptions to display message without traceback raise err except Exception as e: # Handle any other exceptions and display error message raise gr.Error(f"An unexpected error occurred: {e}") # Function to update the submit button state def update_submit_button_state(api_key, input_text): if api_key.strip() and input_text.strip(): return gr.update(interactive=True) else: return gr.update(interactive=False) # Update the submit button state when the API key or input text changes api_key_input.change( fn=update_submit_button_state, inputs=[api_key_input, input_textbox], outputs=submit_button ) input_textbox.change( fn=update_submit_button_state, inputs=[api_key_input, input_textbox], outputs=submit_button ) # Allow pressing Enter in the input textbox to trigger the conversion input_textbox.submit( fn=on_submit, inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input], outputs=output_audio, api_name="tts", ) # Trigger the conversion when the submit button is clicked submit_button.click( fn=on_submit, inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input], outputs=output_audio, api_name="tts", ) # Launch the Gradio app with error display enabled demo.launch(share=True, show_error=True) if __name__ == "__main__": main()