jonathanagustin commited on
Commit
3020c69
1 Parent(s): dfb71ed

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +23 -66
app.py CHANGED
@@ -18,8 +18,7 @@ Note:
18
  import gradio as gr
19
  import tempfile
20
  import openai
21
- from typing import List
22
-
23
 
24
  def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
25
  """
@@ -35,14 +34,13 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
35
  :type api_key: str
36
  :return: File path to the generated audio file.
37
  :rtype: str
38
- :raises ValueError: If input parameters are invalid.
39
- :raises openai.error.OpenAIError: If API call fails.
40
  """
41
  if not input_text.strip():
42
- raise ValueError("Input text cannot be empty.")
43
 
44
  if not api_key.strip():
45
- raise ValueError("API key is required.")
46
 
47
  openai.api_key = api_key
48
 
@@ -53,10 +51,10 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
53
  model=model
54
  )
55
  except openai.error.OpenAIError as e:
56
- raise e
57
 
58
  if not hasattr(response, 'audio'):
59
- raise Exception("Invalid response from OpenAI API. The response does not contain audio content.")
60
 
61
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
62
  temp_file.write(response.audio)
@@ -64,40 +62,9 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
64
 
65
  return temp_file_path
66
 
67
-
68
- def tts_batch(
69
- input_texts: List[str],
70
- model: str,
71
- voice: str,
72
- api_key: str,
73
- ) -> List[str]:
74
- """
75
- Convert a batch of input texts to speech using OpenAI's Text-to-Speech API.
76
-
77
- :param input_texts: The texts to be converted to speech.
78
- :type input_texts: List[str]
79
- :param model: The model to use for synthesis.
80
- :type model: str
81
- :param voice: The voice profile to use.
82
- :type voice: str
83
- :param api_key: OpenAI API key.
84
- :type api_key: str
85
- :return: List of file paths to the generated audio files.
86
- :rtype: List[str]
87
- """
88
- outputs = []
89
- for input_text in input_texts:
90
- try:
91
- output = tts(input_text, model, voice, api_key)
92
- outputs.append(output)
93
- except Exception as e:
94
- outputs.append(None)
95
- return outputs
96
-
97
-
98
  def main():
99
  """
100
- Main function to create and launch the Gradio interface with API capabilities and enhancements.
101
  """
102
  # Define model and voice options
103
  MODEL_OPTIONS = ["tts-1", "tts-1-hd"]
@@ -107,7 +74,10 @@ def main():
107
  with gr.Row():
108
  with gr.Column(scale=1):
109
  api_key_input = gr.Textbox(
110
- label="API Key", type="password", placeholder="Enter your OpenAI API Key"
 
 
 
111
  )
112
  model_dropdown = gr.Dropdown(
113
  choices=MODEL_OPTIONS, label="Model", value="tts-1"
@@ -121,16 +91,21 @@ def main():
121
  lines=10,
122
  placeholder="Type your text here..."
123
  )
124
- submit_button = gr.Button("Convert Text to Speech", variant="primary")
 
 
 
 
125
  with gr.Column(scale=1):
126
  output_audio = gr.Audio(label="Output Audio")
127
- error_output = gr.Textbox(
128
- label="Error Message", interactive=False, visible=False
129
- )
130
 
131
- # Define the event handler for the submit button with API endpoint and naming
 
 
 
 
132
  submit_button.click(
133
- fn=tts,
134
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
135
  outputs=output_audio,
136
  api_name="convert_text_to_speech",
@@ -138,32 +113,14 @@ def main():
138
 
139
  # Allow pressing Enter in the input textbox to trigger the conversion
140
  input_textbox.submit(
141
- fn=tts,
142
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
143
  outputs=output_audio,
144
  api_name="convert_text_to_speech",
145
  )
146
 
147
- # Expose the `demo` app as a callable function
148
- def process_text_to_speech(
149
- input_text: str,
150
- model: str = "tts-1",
151
- voice: str = "echo",
152
- api_key: str = ""
153
- ) -> str:
154
- """
155
- Allows calling the Gradio app as a function.
156
- """
157
- return demo(
158
- input_text,
159
- model,
160
- voice,
161
- api_key
162
- )
163
-
164
  # Launch the Gradio app with API documentation enabled
165
  demo.launch(share=True)
166
 
167
-
168
  if __name__ == "__main__":
169
  main()
 
18
  import gradio as gr
19
  import tempfile
20
  import openai
21
+ from typing import Tuple
 
22
 
23
  def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
24
  """
 
34
  :type api_key: str
35
  :return: File path to the generated audio file.
36
  :rtype: str
37
+ :raises gr.Error: If input parameters are invalid or API call fails.
 
38
  """
39
  if not input_text.strip():
40
+ raise gr.Error("Input text cannot be empty.")
41
 
42
  if not api_key.strip():
43
+ raise gr.Error("API key is required.")
44
 
45
  openai.api_key = api_key
46
 
 
51
  model=model
52
  )
53
  except openai.error.OpenAIError as e:
54
+ raise gr.Error(f"OpenAI API Error: {str(e)}")
55
 
56
  if not hasattr(response, 'audio'):
57
+ raise gr.Error("Invalid response from OpenAI API. The response does not contain audio content.")
58
 
59
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
60
  temp_file.write(response.audio)
 
62
 
63
  return temp_file_path
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def main():
66
  """
67
+ Main function to create and launch the Gradio interface with input validation and error handling.
68
  """
69
  # Define model and voice options
70
  MODEL_OPTIONS = ["tts-1", "tts-1-hd"]
 
74
  with gr.Row():
75
  with gr.Column(scale=1):
76
  api_key_input = gr.Textbox(
77
+ label="API Key",
78
+ type="password",
79
+ placeholder="Enter your OpenAI API Key",
80
+ value="",
81
  )
82
  model_dropdown = gr.Dropdown(
83
  choices=MODEL_OPTIONS, label="Model", value="tts-1"
 
91
  lines=10,
92
  placeholder="Type your text here..."
93
  )
94
+ submit_button = gr.Button(
95
+ "Convert Text to Speech",
96
+ variant="primary",
97
+ interactive=True
98
+ )
99
  with gr.Column(scale=1):
100
  output_audio = gr.Audio(label="Output Audio")
 
 
 
101
 
102
+ # Define the event handler for the submit button with error handling
103
+ def on_submit(input_text, model, voice, api_key):
104
+ audio_file = tts(input_text, model, voice, api_key)
105
+ return audio_file
106
+
107
  submit_button.click(
108
+ fn=on_submit,
109
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
110
  outputs=output_audio,
111
  api_name="convert_text_to_speech",
 
113
 
114
  # Allow pressing Enter in the input textbox to trigger the conversion
115
  input_textbox.submit(
116
+ fn=on_submit,
117
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
118
  outputs=output_audio,
119
  api_name="convert_text_to_speech",
120
  )
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  # Launch the Gradio app with API documentation enabled
123
  demo.launch(share=True)
124
 
 
125
  if __name__ == "__main__":
126
  main()