Spaces:
Running
Running
jonathanagustin
commited on
Commit
•
dfb71ed
1
Parent(s):
42966de
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -18,7 +18,7 @@ Note:
|
|
18 |
import gradio as gr
|
19 |
import tempfile
|
20 |
import openai
|
21 |
-
from typing import
|
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.
|
51 |
-
|
52 |
voice=voice,
|
53 |
model=model
|
54 |
)
|
55 |
except openai.error.OpenAIError as e:
|
56 |
raise e
|
57 |
|
58 |
-
if not hasattr(response, '
|
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.
|
63 |
temp_file_path = temp_file.name
|
64 |
|
65 |
return temp_file_path
|
66 |
|
67 |
|
68 |
-
def
|
|
|
|
|
|
|
|
|
|
|
69 |
"""
|
70 |
-
|
71 |
|
72 |
-
:param
|
73 |
-
:type
|
74 |
-
:param model:
|
75 |
:type model: str
|
76 |
-
:param voice:
|
77 |
:type voice: str
|
78 |
-
:param api_key:
|
79 |
:type api_key: str
|
80 |
-
:return:
|
81 |
-
:rtype:
|
82 |
"""
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
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=
|
126 |
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
|
127 |
-
outputs=
|
|
|
128 |
)
|
129 |
|
130 |
# Allow pressing Enter in the input textbox to trigger the conversion
|
131 |
input_textbox.submit(
|
132 |
-
fn=
|
133 |
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
|
134 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
)
|
136 |
|
137 |
-
|
|
|
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()
|