CoolFace
Apppublic

UltraMarkoBR/OpenAI_TTS_Streaming

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py49 linesDownload Raw Back to root
1import gradio as gr2import numpy as np3import io4import os5from openai import OpenAI6from pydub import AudioSegment7from pydub.playback import play8 9 10# Set an environment variable for key11os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')12 13client = OpenAI() # add api_key14 15def stream_and_yield_audio(text, model, voice):16    response = client.audio.speech.create(17        model=model, #"tts-1", for example18        voice=voice , #"alloy", for example19        input=text,20    )21 22    # Convert the binary response content to a byte stream23    byte_stream = io.BytesIO(response.content)24 25    # Read the audio data from the byte stream26    audio = AudioSegment.from_file(byte_stream, format="mp3")27 28    # Export the audio as WAV format29    sample_width = audio.sample_width30    sample_rate = audio.frame_rate31    audio_data = np.array(audio.get_array_of_samples(), dtype=np.int16)32 33    # Yield the audio data34    yield sample_rate, audio_data #audio_data.tobytes(), sample_width35    36 37# demo using older gradio version (3.50.2) 38with gr.Blocks() as demo:39    with gr.Row():40      model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model', value='tts-1')41      voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')42 43    text = gr.Textbox(label="Input text")44    btn = gr.Button("Greet")45    output_audio = gr.Audio(label="Speech Output", streaming=True, autoplay=True)46    47    btn.click(fn=stream_and_yield_audio, inputs=[text,model, voice], outputs=output_audio, api_name="tts-stream")48 49demo.queue().launch()