File size: 794 Bytes
7eec909
 
49c5291
7eec909
 
5218b8f
49c5291
 
7eec909
 
 
 
 
 
49c5291
 
7eec909
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import gradio as gr
from openai import OpenAI
from pathlib import Path

def generate_audio(text, api_key):
    client = OpenAI(api_key=api_key)
    speech_file_path = Path(__file__).parent / "speech.mp3"
    
    response = client.audio.speech.create(
        model="tts-1",
        voice="alloy",
        input=text,
    )

    response.stream_to_file(speech_file_path)
    yield speech_file_path

iface = gr.Interface(
    fn=generate_audio,
    inputs=[
        gr.Textbox(label="Enter text to synthesize"),
        gr.Textbox(label="Enter OpenAI API Key", type="password")
    ],
    outputs=gr.Audio(streaming=True),
    title="OpenAI Text-to-Speech",
    description="Enter some text and your OpenAI API key, then click 'Submit' to synthesize speech using OpenAI's API."
)

iface.launch()