jonathanagustin commited on
Commit
dfb71ed
1 Parent(s): 42966de

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +55 -27
app.py CHANGED
@@ -18,7 +18,7 @@ Note:
18
  import gradio as gr
19
  import tempfile
20
  import openai
21
- from typing import Tuple
22
 
23
 
24
  def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
@@ -47,49 +47,57 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
47
  openai.api_key = api_key
48
 
49
  try:
50
- response = openai.audio.speech.create(
51
- input=input_text,
52
  voice=voice,
53
  model=model
54
  )
55
  except openai.error.OpenAIError as e:
56
  raise e
57
 
58
- if not hasattr(response, 'content'):
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.content)
63
  temp_file_path = temp_file.name
64
 
65
  return temp_file_path
66
 
67
 
68
- def on_convert_click(input_text: str, model: str, voice: str, api_key: str) -> Tuple[str, str]:
 
 
 
 
 
69
  """
70
- Callback function to handle the click event for text-to-speech conversion.
71
 
72
- :param input_text: Text input from the user.
73
- :type input_text: str
74
- :param model: Selected model.
75
  :type model: str
76
- :param voice: Selected voice.
77
  :type voice: str
78
- :param api_key: User's OpenAI API key.
79
  :type api_key: str
80
- :return: Tuple containing the file path to the generated audio file and an error message.
81
- :rtype: Tuple[str, str]
82
  """
83
- try:
84
- file_path = tts(input_text, model, voice, api_key)
85
- return file_path, ""
86
- except Exception as e:
87
- return None, str(e)
 
 
 
88
 
89
 
90
  def main():
91
  """
92
- Main function to create and launch the Gradio interface.
93
  """
94
  # Define model and voice options
95
  MODEL_OPTIONS = ["tts-1", "tts-1-hd"]
@@ -120,22 +128,42 @@ def main():
120
  label="Error Message", interactive=False, visible=False
121
  )
122
 
123
- # Define the event handler for the submit button
124
  submit_button.click(
125
- fn=on_convert_click,
126
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
127
- outputs=[output_audio, error_output]
 
128
  )
129
 
130
  # Allow pressing Enter in the input textbox to trigger the conversion
131
  input_textbox.submit(
132
- fn=on_convert_click,
133
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
134
- outputs=[output_audio, error_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  )
136
 
137
- demo.launch()
 
138
 
139
 
140
  if __name__ == "__main__":
141
- main()
 
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:
 
47
  openai.api_key = api_key
48
 
49
  try:
50
+ response = openai.Audio.create(
51
+ text=input_text,
52
  voice=voice,
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)
63
  temp_file_path = temp_file.name
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"]
 
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",
137
  )
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()