jonathanagustin commited on
Commit
d72598f
1 Parent(s): 4ac8df6

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +8 -13
app.py CHANGED
@@ -22,7 +22,7 @@ def tts(
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.
26
 
27
  Returns:
28
  str: File path to the generated audio file.
@@ -42,29 +42,23 @@ def tts(
42
 
43
  try:
44
  response = openai.audio.speech.create(
45
- input=input_text,
46
- voice=voice,
47
  model=model,
 
 
48
  response_format=response_format,
49
  speed=speed,
50
  )
51
- except openai.OpenAIError as e:
52
- # Catch-all for OpenAI exceptions
53
  raise gr.Error(f"An OpenAI error occurred: {e}")
54
  except Exception as e:
55
  # Catch any other exceptions
56
  raise gr.Error(f"An unexpected error occurred: {e}")
57
 
58
- if not hasattr(response, "audio"):
59
- raise gr.Error(
60
- "Invalid response from OpenAI API. The response does not contain audio content."
61
- )
62
-
63
  # Save the audio content to a temporary file
64
- audio_content = response.audio
65
  file_extension = f".{response_format}"
66
  with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
67
- temp_file.write(audio_content)
68
  temp_file_path = temp_file.name
69
 
70
  return temp_file_path
@@ -171,6 +165,7 @@ def main():
171
  step=0.05,
172
  label="Voice Speed",
173
  value=1.0,
 
174
  )
175
 
176
  with gr.Column(scale=2):
@@ -229,4 +224,4 @@ def main():
229
  demo.launch(show_error=True)
230
 
231
  if __name__ == "__main__":
232
- main()
 
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.
 
42
 
43
  try:
44
  response = openai.audio.speech.create(
 
 
45
  model=model,
46
+ voice=voice,
47
+ input=input_text,
48
  response_format=response_format,
49
  speed=speed,
50
  )
51
+ except openai.error.OpenAIError as e:
52
+ # Catch OpenAI exceptions
53
  raise gr.Error(f"An OpenAI error occurred: {e}")
54
  except Exception as e:
55
  # Catch any other exceptions
56
  raise gr.Error(f"An unexpected error occurred: {e}")
57
 
 
 
 
 
 
58
  # Save the audio content to a temporary file
 
59
  file_extension = f".{response_format}"
60
  with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
61
+ response.stream_to_file(temp_file.name)
62
  temp_file_path = temp_file.name
63
 
64
  return temp_file_path
 
165
  step=0.05,
166
  label="Voice Speed",
167
  value=1.0,
168
+ info="Adjust the speed of the generated speech.",
169
  )
170
 
171
  with gr.Column(scale=2):
 
224
  demo.launch(show_error=True)
225
 
226
  if __name__ == "__main__":
227
+ main()