NeoPy commited on
Commit
33c6485
Β·
verified Β·
1 Parent(s): 2ee914a

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +31 -30
demo.py CHANGED
@@ -8,13 +8,22 @@ model_library = CachedModels()
8
 
9
  # Helper moved outside to avoid lambda issues in UI definition
10
  def get_audio_paths(path):
 
 
11
  return [os.path.abspath(os.path.join(path, f)) for f in os.listdir(path) if os.path.splitext(f)[1].lower() in ('.mp3', '.wav', '.flac', '.ogg')]
12
 
13
  with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hue="zinc")) as app:
14
  with gr.Tabs():
15
  with gr.Tab("Inference"):
16
  with gr.Row():
17
- voice_model = gr.Dropdown(label="Model Voice", choices=sorted(names), value=lambda:sorted(names)[0] if len(sorted(names)) > 0 else '', interactive=True)
 
 
 
 
 
 
 
18
  refresh_button = gr.Button("Refresh", variant="primary")
19
  spk_item = gr.Slider(
20
  minimum=0,
@@ -33,7 +42,6 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
33
  with gr.Row():
34
  with gr.Column():
35
  with gr.Row():
36
- # Fixed: Use correct parameter name
37
  dropbox = gr.Audio(label="Drop your audio here & hit the Reload button.", type="filepath")
38
  with gr.Row():
39
  record_button = gr.Audio(sources=["microphone"], label="OR Record audio.", type="filepath")
@@ -47,7 +55,6 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
47
  with gr.Row():
48
  audio_player = gr.Audio()
49
 
50
- # Fixed: Proper function to update audio player
51
  def update_audio_player(path):
52
  if path and os.path.exists(path):
53
  return path
@@ -59,7 +66,6 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
59
  outputs=[audio_player]
60
  )
61
 
62
- # Fixed: Handle record button properly
63
  def handle_record(audio):
64
  if audio:
65
  return audio
@@ -71,7 +77,6 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
71
  outputs=[input_audio0]
72
  )
73
 
74
- # Fixed: Handle dropbox upload
75
  def handle_upload(audio):
76
  if audio:
77
  return audio
@@ -150,34 +155,35 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
150
  # Consolidated refresh logic
151
  def refresh_ui():
152
  # Get updated lists
153
- # Assuming change_choices is imported from original or defined elsewhere
154
  try:
155
  model_choices, index_choices = change_choices()
156
- except:
157
- # Fallback if change_choices doesn't return two values
158
  model_choices = []
159
  index_choices = []
160
 
 
 
 
 
 
 
161
  audio_paths = get_audio_paths('audios')
162
-
163
- # Helper to safely get the first item
164
- def safe_first(data):
165
- if not data:
166
- return ""
167
- if isinstance(data, dict):
168
- return next(iter(data.values()))
169
- if isinstance(data, list) and len(data) > 0:
170
- return data[0]
171
- return ""
172
-
173
- default_audio = safe_first(audio_paths)
174
- default_model = safe_first(model_choices)
175
- default_index = safe_first(index_choices)
176
 
177
  return (
178
- gr.update(choices=model_choices, value=default_model), # voice_model
179
- gr.update(choices=index_choices, value=default_index), # file_index2
180
- gr.update(choices=audio_paths, value=default_audio) # input_audio0
181
  )
182
 
183
  refresh_button.click(
@@ -272,12 +278,10 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
272
  dataset_folder = gr.Textbox(
273
  label="dataset folder", value='dataset'
274
  )
275
- # Fixed: Use correct file handling
276
  easy_uploader = gr.File(label="Drop your audio files here", file_count="multiple", file_types=["audio"])
277
  but1 = gr.Button("1. Process", variant="primary")
278
  info1 = gr.Textbox(label="Information", value="", visible=True)
279
 
280
- # Fixed: Proper file upload handling
281
  def handle_file_upload(files, folder):
282
  if not folder or folder.strip() == "":
283
  gr.Warning('Please enter a folder name for your dataset')
@@ -420,7 +424,6 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
420
  interactive=True,
421
  )
422
  with gr.Accordion(label="Change pretrains", open=False):
423
- # Fixed: Get pretrained choices
424
  def get_pretrained_choices(sr, letter):
425
  pretrained_dir = 'assets/pretrained_v2'
426
  if not os.path.exists(pretrained_dir):
@@ -453,14 +456,12 @@ with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hu
453
  gr.update(choices=d_choices, value=d_choices[0] if d_choices else "")
454
  )
455
 
456
- # Bind update function to changes
457
  sr2.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
458
  version19.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
459
 
460
  with gr.Row():
461
  download_model = gr.Button('5.Download Model')
462
  with gr.Row():
463
- # Fixed: File handling for model download
464
  model_files = gr.File(label='Your Model and Index file can be downloaded here:')
465
 
466
  def download_model_files(name):
 
8
 
9
  # Helper moved outside to avoid lambda issues in UI definition
10
  def get_audio_paths(path):
11
+ if not os.path.exists(path):
12
+ return []
13
  return [os.path.abspath(os.path.join(path, f)) for f in os.listdir(path) if os.path.splitext(f)[1].lower() in ('.mp3', '.wav', '.flac', '.ogg')]
14
 
15
  with gr.Blocks(title="πŸ”Š", theme=gr.themes.Base(primary_hue="blue", neutral_hue="zinc")) as app:
16
  with gr.Tabs():
17
  with gr.Tab("Inference"):
18
  with gr.Row():
19
+ # Initialize voice_model with proper choices
20
+ initial_names = sorted(names) if names else []
21
+ voice_model = gr.Dropdown(
22
+ label="Model Voice",
23
+ choices=initial_names,
24
+ value=initial_names[0] if initial_names else None,
25
+ interactive=True
26
+ )
27
  refresh_button = gr.Button("Refresh", variant="primary")
28
  spk_item = gr.Slider(
29
  minimum=0,
 
42
  with gr.Row():
43
  with gr.Column():
44
  with gr.Row():
 
45
  dropbox = gr.Audio(label="Drop your audio here & hit the Reload button.", type="filepath")
46
  with gr.Row():
47
  record_button = gr.Audio(sources=["microphone"], label="OR Record audio.", type="filepath")
 
55
  with gr.Row():
56
  audio_player = gr.Audio()
57
 
 
58
  def update_audio_player(path):
59
  if path and os.path.exists(path):
60
  return path
 
66
  outputs=[audio_player]
67
  )
68
 
 
69
  def handle_record(audio):
70
  if audio:
71
  return audio
 
77
  outputs=[input_audio0]
78
  )
79
 
 
80
  def handle_upload(audio):
81
  if audio:
82
  return audio
 
155
  # Consolidated refresh logic
156
  def refresh_ui():
157
  # Get updated lists
 
158
  try:
159
  model_choices, index_choices = change_choices()
160
+ except Exception as e:
161
+ print(f"Error in change_choices: {e}")
162
  model_choices = []
163
  index_choices = []
164
 
165
+ # Ensure model_choices and index_choices are lists
166
+ if not isinstance(model_choices, list):
167
+ model_choices = []
168
+ if not isinstance(index_choices, list):
169
+ index_choices = []
170
+
171
  audio_paths = get_audio_paths('audios')
172
+
173
+ # Get current values
174
+ current_model = voice_model.value if hasattr(voice_model, 'value') else None
175
+ current_index = file_index2.value if hasattr(file_index2, 'value') else None
176
+ current_audio = input_audio0.value if hasattr(input_audio0, 'value') else None
177
+
178
+ # Set defaults
179
+ default_model = current_model if current_model in model_choices else (model_choices[0] if model_choices else None)
180
+ default_index = current_index if current_index in index_choices else (index_choices[0] if index_choices else None)
181
+ default_audio = current_audio if current_audio in audio_paths else (audio_paths[0] if audio_paths else None)
 
 
 
 
182
 
183
  return (
184
+ gr.update(choices=model_choices, value=default_model), # voice_model
185
+ gr.update(choices=index_choices, value=default_index), # file_index2
186
+ gr.update(choices=audio_paths, value=default_audio) # input_audio0
187
  )
188
 
189
  refresh_button.click(
 
278
  dataset_folder = gr.Textbox(
279
  label="dataset folder", value='dataset'
280
  )
 
281
  easy_uploader = gr.File(label="Drop your audio files here", file_count="multiple", file_types=["audio"])
282
  but1 = gr.Button("1. Process", variant="primary")
283
  info1 = gr.Textbox(label="Information", value="", visible=True)
284
 
 
285
  def handle_file_upload(files, folder):
286
  if not folder or folder.strip() == "":
287
  gr.Warning('Please enter a folder name for your dataset')
 
424
  interactive=True,
425
  )
426
  with gr.Accordion(label="Change pretrains", open=False):
 
427
  def get_pretrained_choices(sr, letter):
428
  pretrained_dir = 'assets/pretrained_v2'
429
  if not os.path.exists(pretrained_dir):
 
456
  gr.update(choices=d_choices, value=d_choices[0] if d_choices else "")
457
  )
458
 
 
459
  sr2.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
460
  version19.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
461
 
462
  with gr.Row():
463
  download_model = gr.Button('5.Download Model')
464
  with gr.Row():
 
465
  model_files = gr.File(label='Your Model and Index file can be downloaded here:')
466
 
467
  def download_model_files(name):