smease commited on
Commit
43aeb21
·
1 Parent(s): c68c897

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import tempfile
4
+ from openai import OpenAI
5
+
6
+ # Set an environment variable for key
7
+ os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')
8
+
9
+ client = OpenAI() # add api_key
10
+
11
+ def tts(text, model, voice):
12
+ response = client.audio.speech.create(
13
+ model=model, #"tts-1","tts-1-hd"
14
+ voice=voice, #'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
15
+ input=text,
16
+ )
17
+
18
+ # Create a temp file to save the audio
19
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
20
+ temp_file.write(response.content)
21
+
22
+ # Get the file path of the temp file
23
+ temp_file_path = temp_file.name
24
+
25
+ return temp_file_path
26
+
27
+
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
30
+ gr.HTML("You can also access the Streaming demo for OpenAI TTS by clicking this <a href='https://huggingface.co/spaces/ysharma/OpenAI_TTS_Streaming'>Gradio demo link</a>")
31
+ with gr.Row():
32
+ model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model', value='tts-1')
33
+ voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')
34
+
35
+ text = gr.Textbox(label="Input text", placeholder="Input text and press the Text-To-Speech button or press Enter.")
36
+ btn = gr.Button("Text-To-Speech")
37
+ output_audio = gr.Audio(label="Speech Output")
38
+
39
+ text.submit(fn=tts, inputs=[text, model, voice], outputs=output_audio, api_name="tts", concurrency_limit=None)
40
+ btn.click(fn=tts, inputs=[text, model, voice], outputs=output_audio, api_name="tts", concurrency_limit=None)
41
+
42
+ demo.launch(auth=("cutie", "softpaws"))