Spaces:
Runtime error
Runtime error
added format selector
Browse files
app.py
CHANGED
@@ -9,15 +9,21 @@ os.environ['OPENAI_API_KEY'] = api_key
|
|
9 |
|
10 |
openai_client = OpenAI()
|
11 |
|
12 |
-
def synthesize_speech(input_text, selected_model, selected_voice):
|
|
|
|
|
13 |
audio_response = openai_client.audio.speech.create(
|
14 |
model=selected_model,
|
15 |
voice=selected_voice,
|
16 |
-
input=input_text
|
|
|
17 |
)
|
18 |
|
|
|
|
|
|
|
19 |
# Save the synthesized speech to a temporary audio file
|
20 |
-
with tempfile.NamedTemporaryFile(suffix=
|
21 |
audio_temp.write(audio_response.content)
|
22 |
audio_file_path = audio_temp.name
|
23 |
|
@@ -29,13 +35,14 @@ with grd.Blocks() as speech_synthesizer_interface:
|
|
29 |
with grd.Row():
|
30 |
model_selector = grd.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Choose Model', value='tts-1')
|
31 |
voice_selector = grd.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Select Voice', value='alloy')
|
|
|
32 |
|
33 |
input_field = grd.Textbox(label="Enter your text here", placeholder="Type here and convert to speech.")
|
34 |
synthesis_button = grd.Button("Convert to Speech")
|
35 |
audio_result = grd.Audio(label="Generated Speech")
|
36 |
|
37 |
-
input_field.submit(fn=synthesize_speech, inputs=[input_field, model_selector, voice_selector], outputs=audio_result)
|
38 |
-
synthesis_button.click(fn=synthesize_speech, inputs=[input_field, model_selector, voice_selector], outputs=audio_result)
|
39 |
|
40 |
# Launch the interface
|
41 |
speech_synthesizer_interface.launch()
|
|
|
9 |
|
10 |
openai_client = OpenAI()
|
11 |
|
12 |
+
def synthesize_speech(input_text, selected_model, selected_voice, audio_format):
|
13 |
+
# You would need to check the OpenAI documentation for the correct parameter to set the audio format
|
14 |
+
# and update the following line accordingly.
|
15 |
audio_response = openai_client.audio.speech.create(
|
16 |
model=selected_model,
|
17 |
voice=selected_voice,
|
18 |
+
input=input_text
|
19 |
+
# Add the correct parameter for audio format here, if available
|
20 |
)
|
21 |
|
22 |
+
# Determine the file extension based on the selected audio format
|
23 |
+
file_extension = f".{audio_format}" if audio_format in ['mp3', 'aac', 'flac'] else ".opus"
|
24 |
+
|
25 |
# Save the synthesized speech to a temporary audio file
|
26 |
+
with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as audio_temp:
|
27 |
audio_temp.write(audio_response.content)
|
28 |
audio_file_path = audio_temp.name
|
29 |
|
|
|
35 |
with grd.Row():
|
36 |
model_selector = grd.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Choose Model', value='tts-1')
|
37 |
voice_selector = grd.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Select Voice', value='alloy')
|
38 |
+
format_selector = grd.Dropdown(choices=['mp3', 'opus', 'aac', 'flac'], label='Select Format', value='mp3')
|
39 |
|
40 |
input_field = grd.Textbox(label="Enter your text here", placeholder="Type here and convert to speech.")
|
41 |
synthesis_button = grd.Button("Convert to Speech")
|
42 |
audio_result = grd.Audio(label="Generated Speech")
|
43 |
|
44 |
+
input_field.submit(fn=synthesize_speech, inputs=[input_field, model_selector, voice_selector, format_selector], outputs=audio_result)
|
45 |
+
synthesis_button.click(fn=synthesize_speech, inputs=[input_field, model_selector, voice_selector, format_selector], outputs=audio_result)
|
46 |
|
47 |
# Launch the interface
|
48 |
speech_synthesizer_interface.launch()
|