Gregniuki commited on
Commit
3e91c45
·
1 Parent(s): caf7c50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -108
app.py CHANGED
@@ -132,7 +132,24 @@ async def load_model_data():
132
  sess_options = onnxruntime.SessionOptions()
133
 
134
  # Collect data for all models in the directory and populate model_configurations
135
- onnx_models = detect_onnx_models(models_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
 
138
  #@app.post("/synthesize")
@@ -149,27 +166,7 @@ async def main(
149
  noise_scale_w_slider: float = Form(...),
150
  play: bool = Form(True)
151
  ):
152
- """Main entry point"""
153
- # sys.path.append('./content/piper/src/python')
154
- # models_path = "./content/piper/src/python"
155
- # logging.basicConfig(level=logging.DEBUG)
156
- # providers = [
157
- # "CPUExecutionProvider"
158
- # if use_gpu is False
159
- # else ("CUDAExecutionProvider", {"cudnn_conv_algo_search": "DEFAULT"})
160
- # ]
161
- # sess_options = onnxruntime.SessionOptions()
162
- # model = None
163
- # lang = speaker
164
- # onnx_models = detect_onnx_models(models_path)
165
-
166
- # if len(text_input) == 0:
167
- # text_input = "1, 2, 3. This is a test. Enter some text to generate."
168
- # speaker_selection = widgets.Dropdown(
169
- # options=[],
170
- # description=f'{lan.translate(lang, "Select speaker")}:',
171
- # layout={'visibility': 'hidden'}
172
- # )
173
  # Handle loading the selected model and speaker ID here
174
  if selected_model in onnx_models:
175
  config = model_configurations[selected_model]
@@ -184,43 +181,28 @@ async def main(
184
  document.getElementById("synthesize_button").disabled = true;
185
  </script>
186
  """
187
- onnx_model = selected_model
188
- model, config = load_onnx(onnx_model, sess_options, providers)
189
-
190
-
191
- config["espeak"]["voice"] = speaker
192
-
193
- config["inference"]["noise_scale"] = noise_scale_slider
194
- config["inference"]["length_scale"] = speed_slider
195
- config["inference"]["noise_w"] = noise_scale_w_slider
196
-
197
-
198
- # lan.load_language(speaker)
199
-
200
- print("speed: ", speed_slider)
201
- print("nuber of speakers = ", config["num_speakers"])
202
- print("speaker", speaker)
203
- print("language", config["espeak"]["voice"])
204
- # rate = speed_slider.value
205
- # noise_scale = noise_scale_slider.value
206
- # noise_scale_w = noise_scale_w_slider.value
207
  auto_play = play
208
  audio = inferencing(model, config, 1, text_input, speed_slider, noise_scale_slider, noise_scale_w_slider, auto_play)
209
  temp_dir = tempfile.mkdtemp()
210
 
211
- # Create a temporary directory to store the audio files
212
- #temp_dir = tempfile.mkdtemp()
213
-
214
- # Export the audio to an MP3 file in the temporary directory
215
- # temp_audio_file = os.path.join(temp_dir, "generated_audio.mp3")
216
-
217
- # Check if text_input is more than 200 characters
218
- # if len(text_input) > 100:
219
- # Truncate text_input to 200 characters
220
- # text_input = text_input[:100]
221
 
222
- # Rename the audio file based on the text input
223
- # renamed_audio_file = os.path.join(temp_dir, f"{text_input}.mp3")
224
  renamed_audio_file = os.path.join(temp_dir, "download.mp3")
225
 
226
  audio.export(renamed_audio_file, format="mp3")
@@ -242,60 +224,6 @@ async def main(
242
  document.getElementById("synthesize_button").disabled = false;
243
  </script>
244
  """
245
- # os.rename(temp_audio_file, renamed_audio_file)
246
-
247
- # Specify the path to your MP3 audio file
248
- # audio_file_path = "path/to/your/audio.mp3"
249
-
250
- # Check if the file exists
251
- # if not os.path.exists(audio_file_path):
252
- # return {"detail": "Audio file not found"}
253
-
254
-
255
- # temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
256
- # audio.export(temp_audio_file.name, format="mp3")
257
-
258
- # Rename the temporary audio file based on the text input
259
- # global renamed_audio_file
260
- # renamed_audio_file = os.path.join(tempfile.gettempdir(), f"{text_input}.mp3")
261
- # os.rename(temp_audio_file.name, renamed_audio_file)
262
-
263
- else:
264
- voice_model_names = []
265
- for current in onnx_models:
266
- voice_struct = current.split("/")[5]
267
- voice_model_names.append(voice_struct)
268
- # if enhanced_accessibility:
269
- # playaudio("selectmodel")
270
- # selection = widgets.Dropdown(
271
- # options=voice_model_names,
272
- # description=f'{lan.translate(lang, "Select voice package")}:',
273
- # )
274
- # load_btn = widgets.Button(
275
- # description=lan.translate(lang, "Load it!")
276
- # )
277
- # config = None
278
- # def load_model(button):
279
- # nonlocal config
280
- # global onnx_model
281
- # nonlocal model
282
- # nonlocal models_path
283
- # selected_voice = selection.value
284
- # onnx_model = f"{models_path}/{selected_voice}"
285
- # model, config = load_onnx(onnx_model, sess_options, providers)
286
- # if enhanced_accessibility:
287
- # playaudio("loaded")
288
- # if config["num_speakers"] > 1:
289
- # speaker_selection.options = config["speaker_id_map"].values()
290
- # speaker_selection.layout.visibility = 'visible'
291
- # if enhanced_accessibility:
292
- # playaudio("multispeaker")
293
- # else:
294
- # speaker_selection.layout.visibility = 'hidden'
295
-
296
- # load_btn.on_click(load_model)
297
- # display(selection, load_btn)
298
- # display(speaker_selection)
299
 
300
 
301
 
 
132
  sess_options = onnxruntime.SessionOptions()
133
 
134
  # Collect data for all models in the directory and populate model_configurations
135
+ model_names = detect_onnx_models(models_path)
136
+ for model_name in model_names:
137
+ # Load the configuration data for each model (including speaker_id_map)
138
+ config = load_model_configuration(model_name)
139
+ model_configurations[model_name] = config
140
+
141
+
142
+ def load_model_configuration(model_name):
143
+ # Assuming model_name is the path to the ONNX model file, e.g., 'model.onnx'
144
+ config_file_path = model_name.replace('.onnx', '.json')
145
+
146
+ try:
147
+ with open(config_file_path, 'r') as config_file:
148
+ config_data = json.load(config_file)
149
+ return config_data
150
+ except FileNotFoundError:
151
+ # Handle the case where the configuration file does not exist
152
+ return None
153
 
154
 
155
  #@app.post("/synthesize")
 
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
  config = model_configurations[selected_model]
 
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")
 
224
  document.getElementById("synthesize_button").disabled = false;
225
  </script>
226
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
 
229