tts-openai / app.py
matdmiller's picture
added auth
990159e
raw
history blame
No virus
2.87 kB
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
# %% auto 0
__all__ = ['secret_import_failed', 'tts_voices', 'launch_kwargs', 'create_speech', 'get_input_text_len']
# %% app.ipynb 1
#tts_openai_secrets.py content:
#import os
#os.environ['OPENAI_API_KEY'] = 'sk-XXXXXXXXXXXXXXXXXXXXXX'
import os
secret_import_failed = False
try:
_ = os.environ['OPENAI_API_KEY']
print('OPENAI_API_KEY environment variable was found.')
except:
print('OPENAI_API_KEY environment variable was not found.')
secret_import_failed = True
try:
GRADIO_PASSWORD = os.environ['GRADIO_PASSWORD']
print('GRADIO_PASSWORD environment variable was found.')
except:
print('GRADIO_PASSWORD environment variable was not found.')
secret_import_failed = True
if secret_import_failed == True:
import tts_openai_secrets
GRADIO_PASSWORD = os.environ['GRADIO_PASSWORD']
print('import tts_openai_secrets succeeded')
# %% app.ipynb 3
import gradio as gr
import openai
# %% app.ipynb 4
try:
tts_models = [o.id for o in openai.models.list().data if 'tts' in o.id]
print('successfully got tts model list:', tts_models)
except:
tts_models = ['tts-1']
# %% app.ipynb 5
tts_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']
# %% app.ipynb 6
def create_speech(input_text, model='tts-1', voice='alloy'):
client = openai.OpenAI()
response = client.audio.speech.create(
model=model,
voice=voice,
input=input_text,
speed=1.0
)
client.close()
return response.content
# %% app.ipynb 7
def get_input_text_len(input_text):
return len(input_text)
# %% app.ipynb 8
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 9
launch_kwargs = {'auth':('username',GRADIO_PASSWORD),
'auth_message':'Please log in to Mat\'s TTS App with username: username and password.'}
# %% app.ipynb 11
#.py launch
if __name__ == "__main__":
app.launch(**launch_kwargs)