File size: 997 Bytes
7c45000
1d8366d
 
 
7c45000
 
1d8366d
 
a88694e
7c45000
 
 
 
 
a88694e
7c45000
 
 
 
 
a88694e
7c45000
 
1d8366d
 
 
7c45000
1d8366d
 
a88694e
1d8366d
 
a88694e
 
7c45000
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
30
31
32
33
34
35
36
import os
import openai
import gradio as gr

# read key from env
openai.api_key = os.getenv("OPENAI_API_KEY")

def text_to_speech(text):
    try:
        # call the TTS endpoint
        response = openai.audio.speech.create(
            model="tts-1",      # or "tts-1-hd"
            voice="nova",       # nova | shimmer | echo | fable | onyx | alloy
            input=text
        )
        # response.content holds the raw mp3 bytes
        out_path = "output.mp3"
        with open(out_path, "wb") as f:
            f.write(response.content)
        return out_path
    except Exception as e:
        # return the error message so you see it in the UI
        return f"Error: {e}"

iface = gr.Interface(
    fn=text_to_speech,
    inputs=gr.Textbox(lines=2, placeholder="Enter text to convert to speech…"),
    outputs=gr.Audio(type="filepath"),
    title="Text to Speech with OpenAI",
    description="Enter text and get the spoken audio!"
)

if __name__ == "__main__":
    iface.launch()