Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -152,8 +152,6 @@ def load_model_configuration(model_name):
|
|
152 |
return None
|
153 |
|
154 |
|
155 |
-
#@app.post("/synthesize")
|
156 |
-
#@app.post("/", response_class=FileResponse)
|
157 |
@app.post("/", response_class=HTMLResponse)
|
158 |
async def main(
|
159 |
request: Request,
|
@@ -166,82 +164,62 @@ async def main(
|
|
166 |
noise_scale_w_slider: float = Form(...),
|
167 |
play: bool = Form(True)
|
168 |
):
|
169 |
-
|
170 |
-
# Handle loading the selected model and speaker ID here
|
171 |
if selected_model in onnx_models:
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
response_html = """
|
179 |
-
<div id="
|
180 |
<script>
|
181 |
document.getElementById("synthesize_button").disabled = true;
|
182 |
</script>
|
183 |
"""
|
184 |
-
if selected_model in onnx_models:
|
185 |
-
# The selected_model is found in the list of model file paths
|
186 |
-
model_name = selected_model
|
187 |
-
config = load_model_configuration(model_name)
|
188 |
-
onnx_model = model_name # Replace with the actual key for your ONNX model file
|
189 |
-
if config:
|
190 |
-
model, _ = load_onnx(onnx_model, sess_options, providers)
|
191 |
-
speaker_id_map = config.get("speaker_id_map", {})
|
192 |
-
else:
|
193 |
-
# The selected_model is not found in the list; handle this case as needed
|
194 |
-
# You can show an error message or handle it differently
|
195 |
-
response_html = """
|
196 |
-
<div id="error-message">Selected model not found.</div>
|
197 |
-
<script>
|
198 |
-
document.getElementById("synthesize_button").disabled = true;
|
199 |
-
</script>
|
200 |
-
"""
|
201 |
-
auto_play = play
|
202 |
-
audio = inferencing(model, config, 1, text_input, speed_slider, noise_scale_slider, noise_scale_w_slider, auto_play)
|
203 |
-
temp_dir = tempfile.mkdtemp()
|
204 |
-
|
205 |
-
|
206 |
-
renamed_audio_file = os.path.join(temp_dir, "download.mp3")
|
207 |
-
|
208 |
-
audio.export(renamed_audio_file, format="mp3")
|
209 |
-
# Save the generated audio as a temporary file
|
210 |
-
filepath = renamed_audio_file
|
211 |
-
|
212 |
-
# Generate a unique file ID
|
213 |
-
file_id = str(uuid.uuid4())
|
214 |
-
|
215 |
-
# Store the file path with the generated file ID
|
216 |
-
files[file_id] = filepath
|
217 |
-
|
218 |
-
# Create a URL to download the file
|
219 |
-
file_url = f'/download?fileId={file_id}'
|
220 |
-
# Restore the form and return the response
|
221 |
-
response_html += """
|
222 |
-
<script>
|
223 |
-
document.getElementById("loading-message").innerText = "Audio generated successfully!";
|
224 |
-
document.getElementById("synthesize_button").disabled = false;
|
225 |
-
</script>
|
226 |
-
"""
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
# Save the audio as a temporary WAV file
|
233 |
-
return templates.TemplateResponse("interface.html", {"request": request, "file_url": file_url, "text_input": text_input, "data": data, "model_names": model_configurations.keys(), # Pass the list of available model names
|
234 |
-
"selected_model": selected_model, # Pass the selected model
|
235 |
-
"speaker_id_map": speaker_id_map, # Pass the speaker ID map for the selected model
|
236 |
-
"selected_speaker_id": selected_speaker_id, # Pass the selected speaker ID
|
237 |
-
# "model": model, # Pass the loaded model (if any)
|
238 |
-
"voices": voices, "dynamic_content": response_html})
|
239 |
-
|
240 |
-
# Serve the audio file with the correct media type
|
241 |
-
# return FileResponse(renamed_audio_file)
|
242 |
-
|
243 |
|
244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
|
246 |
@app.get("/download")
|
247 |
async def download_file(fileId: str):
|
|
|
152 |
return None
|
153 |
|
154 |
|
|
|
|
|
155 |
@app.post("/", response_class=HTMLResponse)
|
156 |
async def main(
|
157 |
request: Request,
|
|
|
164 |
noise_scale_w_slider: float = Form(...),
|
165 |
play: bool = Form(True)
|
166 |
):
|
167 |
+
# Handle loading the selected model and speaker ID here
|
|
|
168 |
if selected_model in onnx_models:
|
169 |
+
model_name = selected_model
|
170 |
+
config = load_model_configuration(model_name)
|
171 |
+
onnx_model = config.get("onnx_model") # Replace with the actual key for your ONNX model file
|
172 |
+
if config:
|
173 |
+
model, _ = load_onnx(onnx_model, sess_options, providers)
|
174 |
+
speaker_id_map = config.get("speaker_id_map", {})
|
175 |
+
|
176 |
+
auto_play = play
|
177 |
+
audio = inferencing(model, config, 1, text_input, speed_slider, noise_scale_slider, noise_scale_w_slider, auto_play)
|
178 |
+
temp_dir = tempfile.mkdtemp()
|
179 |
+
renamed_audio_file = os.path.join(temp_dir, "download.mp3")
|
180 |
+
audio.export(renamed_audio_file, format="mp3")
|
181 |
+
|
182 |
+
# Generate a unique file ID
|
183 |
+
file_id = str(uuid.uuid4())
|
184 |
+
|
185 |
+
# Store the file path with the generated file ID
|
186 |
+
files[file_id] = renamed_audio_file
|
187 |
+
|
188 |
+
# Create a URL to download the file
|
189 |
+
file_url = f'/download?fileId={file_id}'
|
190 |
+
|
191 |
+
# Cleanup code for temp_dir here
|
192 |
+
|
193 |
+
# Restore the form and return the response
|
194 |
+
response_html = """
|
195 |
+
<script>
|
196 |
+
document.getElementById("loading-message").innerText = "Audio generated successfully!";
|
197 |
+
document.getElementById("synthesize_button").disabled = false;
|
198 |
+
</script>
|
199 |
+
"""
|
200 |
+
else:
|
201 |
+
# The selected_model is not found in the list; handle this case as needed
|
202 |
+
# You can show an error message or handle it differently
|
203 |
response_html = """
|
204 |
+
<div id="error-message">Selected model not found.</div>
|
205 |
<script>
|
206 |
document.getElementById("synthesize_button").disabled = true;
|
207 |
</script>
|
208 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
+
# Pass the necessary data to the HTML template
|
211 |
+
return templates.TemplateResponse("interface.html", {
|
212 |
+
"request": request,
|
213 |
+
"file_url": file_url,
|
214 |
+
"text_input": text_input,
|
215 |
+
"data": data,
|
216 |
+
"model_names": model_configurations.keys(),
|
217 |
+
"selected_model": selected_model,
|
218 |
+
"speaker_id_map": speaker_id_map,
|
219 |
+
"selected_speaker_id": selected_speaker_id,
|
220 |
+
"voices": voices, # Make sure to populate this variable
|
221 |
+
"dynamic_content": response_html
|
222 |
+
})
|
223 |
|
224 |
@app.get("/download")
|
225 |
async def download_file(fileId: str):
|