File size: 1,528 Bytes
e9aaed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import gradio as gr
import tempfile
from openai import OpenAI
first_alert = "1mp3.mp3"
next_alert = "3mp3.mp3"

def Txt_To_Speech(api_key, model, voice,speed,text):
    if api_key == '':
        output= first_alert
    else:
        try:
           client = OpenAI(api_key=api_key)
           response = client.audio.speech.create(
                model=model,
                voice=voice,
                speed=speed,
                input=text
            )
           with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
            temp_file.write(response.content)

            temp_file_path = temp_file.name
           output = temp_file_path 
        except :
           output = next_alert
    return output


with gr.Blocks() as demo:
    gr.Markdown("# TTS Text To Speech using OpenAI ")

    text1 = gr.Textbox(type = 'password',label="Enter your API-Key", placeholder="API-Key", lines=1)
    model = gr.Dropdown(choices=["tts-1", "tts-1-hd"], label="Model", value="tts-1")
    voice = gr.Dropdown(choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Voice Options",
                            value="alloy")
    speed = gr.Slider(minimum=0.25, maximum=4.0, value=1.0, step=0.01, label="Speed")

    text = gr.Textbox(label="Please Enter text",placeholder="Enter your Input")


    
        
    
    gr.Interface(
        Txt_To_Speech,
        [
            text1,model,voice,speed,text
        ],
        outputs=gr.Audio(label="Speech Output")
    )

    

demo.launch()