Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,10 @@ os.environ['OPENAI_API_KEY'] = api_key
|
|
| 9 |
|
| 10 |
openai_client = OpenAI()
|
| 11 |
|
|
|
|
| 12 |
def synthesize_speech(input_text, selected_model, selected_voice, audio_format):
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
audio_response = openai_client.audio.speech.create(
|
| 16 |
model=selected_model,
|
| 17 |
voice=selected_voice,
|
|
@@ -20,7 +21,8 @@ def synthesize_speech(input_text, selected_model, selected_voice, audio_format):
|
|
| 20 |
)
|
| 21 |
|
| 22 |
# Determine the file extension based on the selected audio format
|
| 23 |
-
file_extension = f".{audio_format}" if audio_format in [
|
|
|
|
| 24 |
|
| 25 |
# Save the synthesized speech to a temporary audio file
|
| 26 |
with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as audio_temp:
|
|
@@ -29,20 +31,27 @@ def synthesize_speech(input_text, selected_model, selected_voice, audio_format):
|
|
| 29 |
|
| 30 |
return audio_file_path
|
| 31 |
|
|
|
|
| 32 |
# Define the Gradio interface
|
| 33 |
with grd.Blocks() as speech_synthesizer_interface:
|
| 34 |
grd.Markdown("# <center> Text-to-Speech Synthesizer </center>")
|
| 35 |
with grd.Row():
|
| 36 |
-
model_selector = grd.Dropdown(
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=[
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Launch the interface
|
| 48 |
-
speech_synthesizer_interface.launch()
|
|
|
|
| 9 |
|
| 10 |
openai_client = OpenAI()
|
| 11 |
|
| 12 |
+
|
| 13 |
def synthesize_speech(input_text, selected_model, selected_voice, audio_format):
|
| 14 |
+
# This is a new feature from OpenAI, so please check the documentation for the correct parameter to set the audio format.
|
| 15 |
+
# See: https://platform.openai.com/docs/guides/text-to-speech
|
| 16 |
audio_response = openai_client.audio.speech.create(
|
| 17 |
model=selected_model,
|
| 18 |
voice=selected_voice,
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
# Determine the file extension based on the selected audio format
|
| 24 |
+
file_extension = f".{audio_format}" if audio_format in [
|
| 25 |
+
'mp3', 'aac', 'flac'] else ".opus"
|
| 26 |
|
| 27 |
# Save the synthesized speech to a temporary audio file
|
| 28 |
with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as audio_temp:
|
|
|
|
| 31 |
|
| 32 |
return audio_file_path
|
| 33 |
|
| 34 |
+
|
| 35 |
# Define the Gradio interface
|
| 36 |
with grd.Blocks() as speech_synthesizer_interface:
|
| 37 |
grd.Markdown("# <center> Text-to-Speech Synthesizer </center>")
|
| 38 |
with grd.Row():
|
| 39 |
+
model_selector = grd.Dropdown(
|
| 40 |
+
choices=['tts-1', 'tts-1-hd'], label='Choose Model', value='tts-1')
|
| 41 |
+
voice_selector = grd.Dropdown(choices=[
|
| 42 |
+
'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Select Voice', value='alloy')
|
| 43 |
+
format_selector = grd.Dropdown(
|
| 44 |
+
choices=['mp3', 'opus', 'aac', 'flac'], label='Select Format', value='mp3')
|
| 45 |
+
|
| 46 |
+
input_field = grd.Textbox(
|
| 47 |
+
label="Enter your text here", placeholder="Type here and convert to speech.")
|
| 48 |
synthesis_button = grd.Button("Convert to Speech")
|
| 49 |
audio_result = grd.Audio(label="Generated Speech")
|
| 50 |
|
| 51 |
+
input_field.submit(fn=synthesize_speech, inputs=[
|
| 52 |
+
input_field, model_selector, voice_selector, format_selector], outputs=audio_result)
|
| 53 |
+
synthesis_button.click(fn=synthesize_speech, inputs=[
|
| 54 |
+
input_field, model_selector, voice_selector, format_selector], outputs=audio_result)
|
| 55 |
|
| 56 |
# Launch the interface
|
| 57 |
+
speech_synthesizer_interface.launch()
|