jhauret commited on
Commit
6f26b02
·
verified ·
1 Parent(s): ebf4fcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -32
app.py CHANGED
@@ -6,7 +6,6 @@ DATASET_NAME = "Cnam-LMSSC/vibravox-test"
6
  SUBSETS = ["speech_clean", "speech_noisy", "speechless_clean", "speechless_noisy"]
7
  SPLITS = ["train", "validation", "test"]
8
  TEXT_COLUMN = "raw_text"
9
- # Add new column names to the configuration
10
  PHONEMIZED_TEXT_COLUMN = "phonemized_text"
11
  GENDER_COLUMN = "gender"
12
  AUDIO_COLUMNS = [
@@ -28,23 +27,30 @@ def load_and_update_all(subset, split):
28
  dataset = load_dataset(DATASET_NAME, name=subset, split=split)
29
  has_text_fields = TEXT_COLUMN in dataset.features
30
 
31
- # Get the first row to display immediately
32
  sample = dataset[0]
33
- sentence = sample[TEXT_COLUMN] if has_text_fields else None
34
- # Fetch the new fields
35
- phonemized_text = sample[PHONEMIZED_TEXT_COLUMN] if has_text_fields else None
36
- gender = sample[GENDER_COLUMN] if has_text_fields else None
37
 
38
  raw_audio_data = [
39
  (sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
40
  ]
41
 
42
- # Return updates for all UI components, including the new ones
 
 
 
 
 
 
 
 
 
 
43
  return (
44
  dataset,
45
- gr.update(maximum=len(dataset) - 1, value=0, visible=True, interactive=True),
46
  gr.update(value=sentence, visible=has_text_fields),
47
- # Add updates for the new text boxes
48
  gr.update(value=phonemized_text, visible=has_text_fields),
49
  gr.update(value=gender, visible=has_text_fields),
50
  *raw_audio_data,
@@ -53,7 +59,6 @@ def load_and_update_all(subset, split):
53
  except Exception as e:
54
  error_message = f"Failed to load {subset}/{split}. Error: {e}"
55
  empty_audio = (None, None)
56
- # Return empty/hidden updates for all components on error
57
  return (
58
  None,
59
  gr.update(visible=False),
@@ -67,16 +72,15 @@ def get_audio_row(dataset, index):
67
  Fetches a new row from the currently loaded dataset when the slider moves.
68
  """
69
  if dataset is None:
70
- return [None] * (3 + len(AUDIO_COLUMNS)) # 3 text fields now
71
 
72
  index = int(index)
73
  sample = dataset[index]
74
 
75
  has_text_fields = TEXT_COLUMN in dataset.features
76
- sentence = sample[TEXT_COLUMN] if has_text_fields else None
77
- # Fetch the new fields for the selected row
78
- phonemized_text = sample[PHONEMIZED_TEXT_COLUMN] if has_text_fields else None
79
- gender = sample[GENDER_COLUMN] if has_text_fields else None
80
 
81
  raw_audio_data = [
82
  (sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
@@ -84,26 +88,19 @@ def get_audio_row(dataset, index):
84
 
85
  return [sentence, phonemized_text, gender] + raw_audio_data
86
 
 
87
  with gr.Blocks(css="footer {display: none !important}") as demo:
88
- # Change the app title
89
  gr.Markdown("# Vibravox Viewer")
90
-
91
  loaded_dataset_state = gr.State(None)
92
-
93
  with gr.Row():
94
  subset_dropdown = gr.Dropdown(SUBSETS, value="speech_clean", label="Select Subset")
95
  split_dropdown = gr.Dropdown(SPLITS, value="train", label="Select Split")
96
-
97
  error_box = gr.Textbox(visible=False, interactive=False, container=False)
98
-
99
- # Group the text outputs together
100
  with gr.Row():
101
  sentence_output = gr.Textbox(label="Raw Text", interactive=False)
102
  phonemized_output = gr.Textbox(label="Phonemized Text", interactive=False)
103
  gender_output = gr.Textbox(label="Gender", interactive=False)
104
-
105
  slider = gr.Slider(label="Select Data Row")
106
-
107
  with gr.Row():
108
  audio1 = gr.Audio(label="Headset Microphone")
109
  audio2 = gr.Audio(label="Laryngophone (Throat Mic)")
@@ -113,21 +110,12 @@ with gr.Blocks(css="footer {display: none !important}") as demo:
113
  audio5 = gr.Audio(label="Forehead Accelerometer")
114
  audio6 = gr.Audio(label="Temple Vibration Pickup")
115
 
116
- # Update the component lists to include the new text boxes
117
  all_outputs = [loaded_dataset_state, slider, sentence_output, phonemized_output, gender_output, audio1, audio2, audio3, audio4, audio5, audio6, error_box]
118
  data_outputs = [sentence_output, phonemized_output, gender_output, audio1, audio2, audio3, audio4, audio5, audio6]
119
-
120
- # --- WIRING THE EVENT HANDLERS ---
121
- # The handlers themselves don't need to change, as we updated the functions and component lists
122
 
123
- # 1. When the app first loads
124
  demo.load(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
125
-
126
- # 2. When a dropdown value changes
127
  subset_dropdown.change(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
128
  split_dropdown.change(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
129
-
130
- # 3. When ONLY the slider changes
131
  slider.change(fn=get_audio_row, inputs=[loaded_dataset_state, slider], outputs=data_outputs)
132
 
133
  demo.launch()
 
6
  SUBSETS = ["speech_clean", "speech_noisy", "speechless_clean", "speechless_noisy"]
7
  SPLITS = ["train", "validation", "test"]
8
  TEXT_COLUMN = "raw_text"
 
9
  PHONEMIZED_TEXT_COLUMN = "phonemized_text"
10
  GENDER_COLUMN = "gender"
11
  AUDIO_COLUMNS = [
 
27
  dataset = load_dataset(DATASET_NAME, name=subset, split=split)
28
  has_text_fields = TEXT_COLUMN in dataset.features
29
 
 
30
  sample = dataset[0]
31
+ sentence = sample.get(TEXT_COLUMN)
32
+ phonemized_text = sample.get(PHONEMIZED_TEXT_COLUMN)
33
+ gender = sample.get(GENDER_COLUMN)
 
34
 
35
  raw_audio_data = [
36
  (sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
37
  ]
38
 
39
+ # --- THE FIX IS HERE ---
40
+ # We add a condition to handle datasets with only one row.
41
+ dataset_len = len(dataset)
42
+ if dataset_len <= 1:
43
+ # If there's only one item, hide the slider as it's not needed.
44
+ slider_update = gr.update(visible=False)
45
+ else:
46
+ # Otherwise, show and configure the slider as normal.
47
+ slider_update = gr.update(maximum=dataset_len - 1, value=0, visible=True, interactive=True)
48
+ # --------------------
49
+
50
  return (
51
  dataset,
52
+ slider_update, # Use the new slider_update variable here
53
  gr.update(value=sentence, visible=has_text_fields),
 
54
  gr.update(value=phonemized_text, visible=has_text_fields),
55
  gr.update(value=gender, visible=has_text_fields),
56
  *raw_audio_data,
 
59
  except Exception as e:
60
  error_message = f"Failed to load {subset}/{split}. Error: {e}"
61
  empty_audio = (None, None)
 
62
  return (
63
  None,
64
  gr.update(visible=False),
 
72
  Fetches a new row from the currently loaded dataset when the slider moves.
73
  """
74
  if dataset is None:
75
+ return [None] * (3 + len(AUDIO_COLUMNS))
76
 
77
  index = int(index)
78
  sample = dataset[index]
79
 
80
  has_text_fields = TEXT_COLUMN in dataset.features
81
+ sentence = sample.get(TEXT_COLUMN)
82
+ phonemized_text = sample.get(PHONEMIZED_TEXT_COLUMN)
83
+ gender = sample.get(GENDER_COLUMN)
 
84
 
85
  raw_audio_data = [
86
  (sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
 
88
 
89
  return [sentence, phonemized_text, gender] + raw_audio_data
90
 
91
+ # --- Build the Gradio Interface (No changes needed here) ---
92
  with gr.Blocks(css="footer {display: none !important}") as demo:
 
93
  gr.Markdown("# Vibravox Viewer")
 
94
  loaded_dataset_state = gr.State(None)
 
95
  with gr.Row():
96
  subset_dropdown = gr.Dropdown(SUBSETS, value="speech_clean", label="Select Subset")
97
  split_dropdown = gr.Dropdown(SPLITS, value="train", label="Select Split")
 
98
  error_box = gr.Textbox(visible=False, interactive=False, container=False)
 
 
99
  with gr.Row():
100
  sentence_output = gr.Textbox(label="Raw Text", interactive=False)
101
  phonemized_output = gr.Textbox(label="Phonemized Text", interactive=False)
102
  gender_output = gr.Textbox(label="Gender", interactive=False)
 
103
  slider = gr.Slider(label="Select Data Row")
 
104
  with gr.Row():
105
  audio1 = gr.Audio(label="Headset Microphone")
106
  audio2 = gr.Audio(label="Laryngophone (Throat Mic)")
 
110
  audio5 = gr.Audio(label="Forehead Accelerometer")
111
  audio6 = gr.Audio(label="Temple Vibration Pickup")
112
 
 
113
  all_outputs = [loaded_dataset_state, slider, sentence_output, phonemized_output, gender_output, audio1, audio2, audio3, audio4, audio5, audio6, error_box]
114
  data_outputs = [sentence_output, phonemized_output, gender_output, audio1, audio2, audio3, audio4, audio5, audio6]
 
 
 
115
 
 
116
  demo.load(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
 
 
117
  subset_dropdown.change(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
118
  split_dropdown.change(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
 
 
119
  slider.change(fn=get_audio_row, inputs=[loaded_dataset_state, slider], outputs=data_outputs)
120
 
121
  demo.launch()