jonathanagustin commited on
Commit
09aa99e
1 Parent(s): beb9866

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +31 -50
app.py CHANGED
@@ -14,56 +14,10 @@ def tts(
14
  speed: float = 1.0,
15
  ) -> str:
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(
37
- "API key is required. Get an API key at: https://platform.openai.com/account/api-keys"
38
- )
39
-
40
- if not input_text.strip():
41
- raise gr.Error("Input text cannot be empty.")
42
-
43
- openai.api_key = api_key
44
-
45
- try:
46
- response = openai.audio.speech.create(
47
- model=model,
48
- voice=voice,
49
- input=input_text,
50
- response_format=response_format,
51
- speed=speed,
52
- )
53
- except openai.error.OpenAIError as e:
54
- # Catch OpenAI exceptions
55
- raise gr.Error(f"An OpenAI error occurred: {e}")
56
- except Exception as e:
57
- # Catch any other exceptions
58
- raise gr.Error(f"An unexpected error occurred: {e}")
59
-
60
- # Save the audio content to a temporary file
61
- file_extension = f".{response_format}"
62
- with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
63
- response.stream_to_file(temp_file.name)
64
- temp_file_path = temp_file.name
65
-
66
- return temp_file_path
67
 
68
  def main():
69
  """
@@ -198,10 +152,37 @@ def main():
198
  show_progress='hidden', # Hide the progress indicator
199
  )
200
 
 
201
  submit_button = gr.Button(
202
- "Convert Text to Speech",
203
  variant="primary",
 
204
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  with gr.Column(scale=1):
206
  output_audio = gr.Audio(label="Output Audio")
207
 
 
14
  speed: float = 1.0,
15
  ) -> str:
16
  """
17
+ [Function remains unchanged]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
+ # [Function body remains unchanged]
20
+ # ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def main():
23
  """
 
152
  show_progress='hidden', # Hide the progress indicator
153
  )
154
 
155
+ # Initialize the submit button as non-interactive
156
  submit_button = gr.Button(
157
+ "Enter OpenAI API Key",
158
  variant="primary",
159
+ interactive=False,
160
  )
161
+
162
+ # Function to update the submit button based on API Key input
163
+ def update_button(api_key):
164
+ """
165
+ Update the submit button's label and interactivity based on the API key input.
166
+
167
+ :param api_key: The current text in the API key input.
168
+ :type api_key: str
169
+ :return: Updated Gradio component for the submit button.
170
+ :rtype: gr.update
171
+ """
172
+ if api_key.strip():
173
+ # There is an API key, enable the submit button
174
+ return gr.update(value="Convert Text to Speech", interactive=True)
175
+ else:
176
+ # No API key, disable the submit button
177
+ return gr.update(value="Enter OpenAI API Key", interactive=False)
178
+
179
+ # Update the submit button whenever the API Key input changes
180
+ api_key_input.input(
181
+ fn=update_button,
182
+ inputs=api_key_input,
183
+ outputs=submit_button,
184
+ )
185
+
186
  with gr.Column(scale=1):
187
  output_audio = gr.Audio(label="Output Audio")
188