jonathanagustin commited on
Commit
3a80faa
1 Parent(s): 775839f

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +55 -23
app.py CHANGED
@@ -16,19 +16,21 @@ def tts(
16
  """
17
  Convert input text to speech using OpenAI's Text-to-Speech API.
18
 
19
- Parameters:
20
- input_text (str): The text to be converted to speech.
21
- model (str): The model to use for synthesis (e.g., 'tts-1', 'tts-1-hd').
22
- voice (str): The voice profile to use (e.g., 'alloy', 'echo', 'fable', etc.).
23
- api_key (str): OpenAI API key.
24
- response_format (str): The audio format of the output file (default is 'mp3').
25
- speed (float): The speed of the synthesized speech (0.25 to 4.0).
26
-
27
- Returns:
28
- str: File path to the generated audio file.
29
-
30
- Raises:
31
- gr.Error: If input parameters are invalid or API call fails.
 
 
32
  """
33
  if not api_key.strip():
34
  raise gr.Error(
@@ -100,8 +102,15 @@ def main():
100
  with gr.Blocks(title="OpenAI - Text to Speech") as demo:
101
  with gr.Row():
102
  with gr.Column(scale=1):
103
- # Function to play the selected voice sample
104
- def play_voice_sample(voice):
 
 
 
 
 
 
 
105
  return gr.update(
106
  value=VOICE_PREVIEW_FILES[voice],
107
  label=f"Preview Voice: {voice.capitalize()}",
@@ -109,7 +118,7 @@ def main():
109
 
110
  preview_audio = gr.Audio(
111
  interactive=False,
112
- label="Echo",
113
  value=VOICE_PREVIEW_FILES['echo'],
114
  visible=True,
115
  show_download_button=False,
@@ -162,15 +171,21 @@ def main():
162
  )
163
 
164
  with gr.Column(scale=2):
165
- # Initialize the input textbox with the desired label
166
  input_textbox = gr.Textbox(
167
  label="Input Text (0000 / 4096 chars)",
168
  lines=10,
169
  placeholder="Type your text here...",
170
  )
171
 
172
- # Function to update the label with the character count
173
- def update_label(input_text):
 
 
 
 
 
 
 
174
  char_count = len(input_text)
175
  new_label = f"Input Text ({char_count:04d} / 4096 chars)"
176
  return gr.update(label=new_label)
@@ -190,10 +205,27 @@ def main():
190
  with gr.Column(scale=1):
191
  output_audio = gr.Audio(label="Output Audio")
192
 
193
- # Define the event handler for the submit button with error handling
194
  def on_submit(
195
- input_text, model, voice, api_key, response_format, speed
196
- ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  audio_file = tts(
198
  input_text, model, voice, api_key, response_format, speed
199
  )
@@ -217,4 +249,4 @@ def main():
217
  demo.launch(show_error=True)
218
 
219
  if __name__ == "__main__":
220
- main()
 
16
  """
17
  Convert input text to speech using OpenAI's Text-to-Speech API.
18
 
19
+ :param input_text: The text to be converted to speech.
20
+ :type input_text: str
21
+ :param model: The model to use for synthesis (e.g., 'tts-1', 'tts-1-hd').
22
+ :type model: str
23
+ :param voice: The voice profile to use (e.g., 'alloy', 'echo', 'fable', etc.).
24
+ :type voice: str
25
+ :param api_key: OpenAI API key.
26
+ :type api_key: str
27
+ :param response_format: The audio format of the output file, defaults to 'mp3'.
28
+ :type response_format: str, optional
29
+ :param speed: The speed of the synthesized speech (0.25 to 4.0), defaults to 1.0.
30
+ :type speed: float, optional
31
+ :return: File path to the generated audio file.
32
+ :rtype: str
33
+ :raises gr.Error: If input parameters are invalid or API call fails.
34
  """
35
  if not api_key.strip():
36
  raise gr.Error(
 
102
  with gr.Blocks(title="OpenAI - Text to Speech") as demo:
103
  with gr.Row():
104
  with gr.Column(scale=1):
105
+ def play_voice_sample(voice: str):
106
+ """
107
+ Play the preview audio sample for the selected voice.
108
+
109
+ :param voice: The name of the voice to preview.
110
+ :type voice: str
111
+ :return: Updated Gradio Audio component with the selected voice sample.
112
+ :rtype: gr.Audio
113
+ """
114
  return gr.update(
115
  value=VOICE_PREVIEW_FILES[voice],
116
  label=f"Preview Voice: {voice.capitalize()}",
 
118
 
119
  preview_audio = gr.Audio(
120
  interactive=False,
121
+ label="Preview Voice: Echo",
122
  value=VOICE_PREVIEW_FILES['echo'],
123
  visible=True,
124
  show_download_button=False,
 
171
  )
172
 
173
  with gr.Column(scale=2):
 
174
  input_textbox = gr.Textbox(
175
  label="Input Text (0000 / 4096 chars)",
176
  lines=10,
177
  placeholder="Type your text here...",
178
  )
179
 
180
+ def update_label(input_text: str):
181
+ """
182
+ Update the label of the input textbox with the current character count.
183
+
184
+ :param input_text: The current text in the input textbox.
185
+ :type input_text: str
186
+ :return: Updated Gradio component with new label.
187
+ :rtype: gr.update
188
+ """
189
  char_count = len(input_text)
190
  new_label = f"Input Text ({char_count:04d} / 4096 chars)"
191
  return gr.update(label=new_label)
 
205
  with gr.Column(scale=1):
206
  output_audio = gr.Audio(label="Output Audio")
207
 
 
208
  def on_submit(
209
+ input_text: str, model: str, voice: str, api_key: str, response_format: str, speed: float
210
+ ) -> str:
211
+ """
212
+ Event handler for the submit button; converts text to speech using the tts function.
213
+
214
+ :param input_text: The text to convert to speech.
215
+ :type input_text: str
216
+ :param model: The TTS model to use (e.g., 'tts-1', 'tts-1-hd').
217
+ :type model: str
218
+ :param voice: The voice profile to use (e.g., 'alloy', 'echo', etc.).
219
+ :type voice: str
220
+ :param api_key: OpenAI API key.
221
+ :type api_key: str
222
+ :param response_format: The audio format of the output file.
223
+ :type response_format: str
224
+ :param speed: The speed of the synthesized speech.
225
+ :type speed: float
226
+ :return: File path to the generated audio file.
227
+ :rtype: str
228
+ """
229
  audio_file = tts(
230
  input_text, model, voice, api_key, response_format, speed
231
  )
 
249
  demo.launch(show_error=True)
250
 
251
  if __name__ == "__main__":
252
+ main()