jonathanagustin's picture
Upload folder using huggingface_hub
dfb71ed verified
raw
history blame
5.31 kB
"""
This script implements a Gradio interface for text-to-speech conversion using OpenAI's API.
Users can input text, select a model and voice, and receive an audio output of the synthesized speech.
Dependencies:
- gradio
- openai
Usage:
Run the script to launch a web interface for text-to-speech conversion.
Note:
- Ensure that you have installed the required packages:
pip install gradio openai
- Obtain a valid OpenAI API key with access to the necessary services.
"""
import gradio as gr
import tempfile
import openai
from typing import List
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 ValueError: If input parameters are invalid.
:raises openai.error.OpenAIError: If API call fails.
"""
if not input_text.strip():
raise ValueError("Input text cannot be empty.")
if not api_key.strip():
raise ValueError("API key is required.")
openai.api_key = api_key
try:
response = openai.Audio.create(
text=input_text,
voice=voice,
model=model
)
except openai.error.OpenAIError as e:
raise e
if not hasattr(response, 'audio'):
raise Exception("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 tts_batch(
input_texts: List[str],
model: str,
voice: str,
api_key: str,
) -> List[str]:
"""
Convert a batch of input texts to speech using OpenAI's Text-to-Speech API.
:param input_texts: The texts to be converted to speech.
:type input_texts: List[str]
:param model: The model to use for synthesis.
:type model: str
:param voice: The voice profile to use.
:type voice: str
:param api_key: OpenAI API key.
:type api_key: str
:return: List of file paths to the generated audio files.
:rtype: List[str]
"""
outputs = []
for input_text in input_texts:
try:
output = tts(input_text, model, voice, api_key)
outputs.append(output)
except Exception as e:
outputs.append(None)
return outputs
def main():
"""
Main function to create and launch the Gradio interface with API capabilities and enhancements.
"""
# Define model and voice options
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", type="password", placeholder="Enter your OpenAI API Key"
)
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")
with gr.Column(scale=1):
output_audio = gr.Audio(label="Output Audio")
error_output = gr.Textbox(
label="Error Message", interactive=False, visible=False
)
# Define the event handler for the submit button with API endpoint and naming
submit_button.click(
fn=tts,
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
outputs=output_audio,
api_name="convert_text_to_speech",
)
# Allow pressing Enter in the input textbox to trigger the conversion
input_textbox.submit(
fn=tts,
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
outputs=output_audio,
api_name="convert_text_to_speech",
)
# Expose the `demo` app as a callable function
def process_text_to_speech(
input_text: str,
model: str = "tts-1",
voice: str = "echo",
api_key: str = ""
) -> str:
"""
Allows calling the Gradio app as a function.
"""
return demo(
input_text,
model,
voice,
api_key
)
# Launch the Gradio app with API documentation enabled
demo.launch(share=True)
if __name__ == "__main__":
main()