Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,50 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from
|
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 |
-
all_voices = voices()
|
47 |
-
input_voice = gr.Dropdown(
|
48 |
-
[ voice.name for voice in all_voices ],
|
49 |
-
value="Rachel",
|
50 |
-
label="Voice",
|
51 |
-
elem_id="input_voice"
|
52 |
-
)
|
53 |
-
|
54 |
-
input_text = gr.Textbox(
|
55 |
-
label="Input Text (4000 characters max)",
|
56 |
-
lines=1,
|
57 |
-
value="Hello! 你好! Hola! नमस्ते! Bonjour! こんにちは! مرحبا! 안녕하세요! Ciao! Cześć! Привіт! Γειά σας! Здравей! வணக்கம்!",
|
58 |
-
elem_id="input_text"
|
59 |
-
)
|
60 |
-
|
61 |
-
run_button = gr.Button(
|
62 |
-
text="Generate Voice",
|
63 |
-
type="button"
|
64 |
-
)
|
65 |
-
|
66 |
-
out_audio = gr.Audio(
|
67 |
-
label="Generated Voice",
|
68 |
-
type="numpy",
|
69 |
-
elem_id="out_audio",
|
70 |
-
format="mp3"
|
71 |
-
)
|
72 |
-
|
73 |
-
inputs = [input_text, input_voice, input_api_key]
|
74 |
-
outputs = [out_audio]
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
inputs=inputs,
|
79 |
-
outputs=outputs,
|
80 |
-
queue=True
|
81 |
-
)
|
82 |
|
83 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
from openai import OpenAI
|
5 |
+
|
6 |
+
|
7 |
+
def tts(text, model, voice, api_key):
|
8 |
+
if api_key == '':
|
9 |
+
raise gr.Error('Please enter your OpenAI API Key')
|
10 |
+
else:
|
11 |
+
try:
|
12 |
+
client = OpenAI(api_key=api_key)
|
13 |
+
|
14 |
+
response = client.audio.speech.create(
|
15 |
+
model=model, # "tts-1","tts-1-hd"
|
16 |
+
voice=voice, # 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
|
17 |
+
input=text,
|
18 |
+
)
|
19 |
+
|
20 |
+
except Exception as error:
|
21 |
+
# Handle any exception that occurs
|
22 |
+
raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")
|
23 |
+
print(str(error))
|
24 |
+
|
25 |
+
# Create a temp file to save the audio
|
26 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
27 |
+
temp_file.write(response.content)
|
28 |
+
|
29 |
+
# Get the file path of the temp file
|
30 |
+
temp_file_path = temp_file.name
|
31 |
+
|
32 |
+
return temp_file_path
|
33 |
+
|
34 |
+
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# <center> Alyxsissy TTS Preview </center>")
|
37 |
+
#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>")
|
38 |
+
with gr.Row(variant='panel'):
|
39 |
+
api_key = gr.Textbox(type='password', label='OpenAI API Key', placeholder='Enter your API key')
|
40 |
+
model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model', value='tts-1-hd')
|
41 |
+
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='nova')
|
42 |
+
|
43 |
+
text = gr.Textbox(label="Input text", placeholder="Enter your text and then click on the button below.")
|
44 |
+
btn = gr.Button("Text-To-Speech")
|
45 |
+
output_audio = gr.Audio(label="Speech Output")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
text.submit(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None)
|
48 |
+
btn.click(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="tts_button", concurrency_limit=None)
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
demo.launch()
|