File size: 2,029 Bytes
0edab04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.

# %% auto 0
__all__ = ['tts_models', 'tts_voices', 'client', 'create_speech', 'get_input_text_len']

# %% app.ipynb 1
#tts_openai_secrets.py content:
#import os
#os.environ['OPENAI_API_KEY'] = 'sk-XXXXXXXXXXXXXXXXXXXXXX'
try:
    import tts_openai_secrets
except:
    print('secret import from tts_openai_secrets.py failed - env var must be previously set')

# %% app.ipynb 3
import gradio as gr
import openai

# %% app.ipynb 4
tts_models = [o.id for o in openai.models.list().data if 'tts' in o.id]
print(tts_models)

# %% app.ipynb 5
tts_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']

# %% app.ipynb 6
client = openai.OpenAI()

# %% app.ipynb 7
def create_speech(input_text, model='tts-1', voice='alloy'):
    response = client.audio.speech.create(
        model=model,
        voice=voice,
        input=input_text,
        speed=1.0
    )
    return response.content

# %% app.ipynb 8
def get_input_text_len(input_text):
    return len(input_text)

# %% app.ipynb 9
with gr.Blocks(title='OpenAI TTS', head='OpenAI TTS') as app:
    gr.Markdown("# OpenAI TTS")
    gr.Markdown("Start typing below and then click **Go** to create the speech from your text. The current limit is 4,000 characters.")
    with gr.Row():
        input_text = gr.Textbox(max_lines=100, label="Enter text here")
    with gr.Row():
        tts_model_dropdown = gr.Dropdown(value='tts-1',choices=tts_models, label='Model')
        tts_voice_dropdown = gr.Dropdown(value='alloy',choices=tts_voices,label='Voice')
        input_text_length = gr.Label(label="Number of characters")
        output_audio = gr.Audio()
    input_text.input(fn=get_input_text_len, inputs=input_text, outputs=input_text_length)
    go_btn = gr.Button("Go")
    go_btn.click(fn=create_speech, inputs=[input_text, tts_model_dropdown, tts_voice_dropdown], outputs=[output_audio])
    clear_btn = gr.Button('Clear')
    clear_btn.click(fn=lambda: '', outputs=input_text)
    

# %% app.ipynb 10
app.launch()