File size: 1,761 Bytes
aa95dd5
 
 
530748c
aa95dd5
 
 
 
5f780d4
aa95dd5
7ebcd98
aa95dd5
fa14696
7ebcd98
aa95dd5
 
 
 
 
 
 
 
 
 
 
 
 
 
3d8b154
1c5bd40
 
aa95dd5
fa14696
7ebcd98
aa95dd5
dcabdfd
ae677e4
aa95dd5
 
dcabdfd
7ebcd98
aa95dd5
 
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
import gradio as gr
import os
import tempfile
from openai import OpenAI

# Set an environment variable for key
os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')

client = OpenAI() # add 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

def tts(text, model, voice):
    response = client.audio.speech.create(
        model=model, #"tts-1-1106","tts-1-hd-1106"
        voice=voice, #'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
        input=text,
    )

    # Create a temp file to save the audio
    with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
        temp_file.write(response.content)

    # Get the file path of the temp file
    temp_file_path = temp_file.name

    return temp_file_path


with gr.Blocks() as demo:
    gr.Markdown("# <center> OpenAI Text-To-Speech API </center>")
    gr.Markdown("<center>⭐️Brought to you by <a href='https://note.com/sangmin/n/n9813f2064a6a'>Chiomirai School</a>⭐️</center>")
    gr.Markdown("<center>🚨Enter fewer than 4096 characters🚨</center>")
    with gr.Row():
      model = gr.Dropdown(choices=['tts-1-1106','tts-1-hd-1106'], label='Model', value='tts-1-1106')
      voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')

    text = gr.Textbox(label="Input text", placeholder="Input text and press the Text-To-Speech button or press Enter.")
    btn = gr.Button("Text-To-Speech")
    output_audio = gr.Audio(label="Speech Output")
    
    text.submit(fn=tts, inputs=[text, model, voice], outputs=output_audio, api_name="tts", concurrency_limit=None)
    btn.click(fn=tts, inputs=[text, model, voice], outputs=output_audio, api_name="tts", concurrency_limit=None)

demo.launch()