Update app.py new preview with chosen language
Browse filesThe new TTS text to speech model with the choice of the desired language. It can transcribe in any desired language because AI can! Day by day it exceeds expectations in transcribing from text to speech much more realistic in all languages.
You can add your desired language if it is not in the code!
Best!
LG
app.py
CHANGED
@@ -3,48 +3,47 @@ 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
|
10 |
else:
|
11 |
try:
|
12 |
client = OpenAI(api_key=api_key)
|
13 |
|
14 |
response = client.audio.speech.create(
|
15 |
-
model=model,
|
16 |
-
voice=voice,
|
|
|
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>
|
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 to access the TTS demo')
|
40 |
-
model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model', value='tts-1')
|
41 |
-
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
3 |
import tempfile
|
4 |
from openai import OpenAI
|
5 |
|
6 |
+
def tts(text, model, voice, language, api_key):
|
|
|
7 |
if api_key == '':
|
8 |
+
raise gr.Error('Please enter your API Key')
|
9 |
else:
|
10 |
try:
|
11 |
client = OpenAI(api_key=api_key)
|
12 |
|
13 |
response = client.audio.speech.create(
|
14 |
+
model=model,
|
15 |
+
voice=voice,
|
16 |
+
language=language, # Parametrul adăugat pentru limbă
|
17 |
input=text,
|
18 |
)
|
19 |
|
20 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
21 |
+
temp_file.write(response.content)
|
22 |
+
temp_file_path = temp_file.name
|
23 |
+
|
24 |
+
return temp_file_path
|
25 |
except Exception as error:
|
|
|
|
|
26 |
print(str(error))
|
27 |
+
raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("# <center> Text-To-Speech API Key </center>")
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
with gr.Row(variant='panel'):
|
33 |
+
api_key = gr.Textbox(type='password', label='OpenAI API Key', placeholder='Enter your API key to access the TTS')
|
34 |
+
model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1-hd')
|
35 |
+
voice = gr.Dropdown(choices=['alloy', 'ash', 'coral', 'echo', 'fable', 'onyx', 'nova', 'sage', 'shimmer'], label='Voice Options', value='echo')
|
36 |
+
language = gr.Dropdown(
|
37 |
+
choices=['en', 'fr', 'es', 'de', 'it', 'ro', 'tr', 'br', 'ru'],
|
38 |
+
label='Choose a language',
|
39 |
+
value='en'
|
40 |
+
)
|
41 |
+
|
42 |
+
text = gr.Textbox(label="Input text", placeholder="Enter your text and then click the 'Text-To-Speech' button or press Enter.")
|
43 |
btn = gr.Button("Text-To-Speech")
|
44 |
output_audio = gr.Audio(label="Speech Output")
|
45 |
+
|
46 |
+
text.submit(fn=tts, inputs=[text, model, voice, language, api_key], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None)
|
47 |
+
btn.click(fn=tts, inputs=[text, model, voice, language, api_key], outputs=output_audio, api_name="tts_button", concurrency_limit=None)
|
48 |
|
49 |
demo.launch()
|