jhauret commited on
Commit
9803b15
·
verified ·
1 Parent(s): 6f9f32c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -44
app.py CHANGED
@@ -2,14 +2,10 @@ import gradio as gr
2
  from datasets import load_dataset
3
 
4
  # --- Configuration ---
5
- DATASET_NAME = "Cnam-LMSSC/vibravox-test"
6
- DATASET_CONFIG = "speech_clean"
7
- DATASET_SPLIT = "train"
8
  TEXT_COLUMN = "raw_text"
9
-
10
- # --- THE FINAL, CORRECT COLUMN NAMES ---
11
- # Based on the official dataset viewer on Hugging Face and the KeyError.
12
- # This list is now definitive.
13
  AUDIO_COLUMNS = [
14
  "audio.headset_microphone",
15
  "audio.throat_microphone",
@@ -19,60 +15,127 @@ AUDIO_COLUMNS = [
19
  "audio.temple_vibration_pickup"
20
  ]
21
 
22
- # --- Load Dataset ---
23
- try:
24
- # Load the dataset normally.
25
- dataset = load_dataset(DATASET_NAME, DATASET_CONFIG, split=DATASET_SPLIT)
26
- except Exception as e:
27
- dataset = None
28
- app_error = e
29
 
30
- # --- App Logic ---
31
- def get_audio_row(index: int):
32
  """
33
- Retrieves a row and returns the text and the RAW audio data.
 
34
  """
35
- row_index = int(index)
36
- sample = dataset[row_index]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- sentence = sample[TEXT_COLUMN]
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # This will now work because we are using the correct column names.
41
- # We extract the raw audio (NumPy array) and sampling rate directly.
42
  raw_audio_data = [
43
  (sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
44
  ]
45
 
46
  return [sentence] + raw_audio_data
47
 
 
48
  # --- Build the Gradio Interface ---
49
  with gr.Blocks(css="footer {display: none !important}") as demo:
50
- gr.Markdown("# Vibravox Multi-Audio Viewer")
51
-
52
- if dataset is None:
53
- gr.Markdown("## 💥 Application Error")
54
- gr.Markdown(f"Could not load or process the dataset. Error: `{app_error}`")
55
- else:
56
- gr.Markdown("Select a row to listen to all corresponding audio sensor recordings.")
57
 
58
- slider = gr.Slider(minimum=0, maximum=len(dataset) - 1, step=1, value=0, label="Select Data Row")
 
 
59
 
60
- sentence_output = gr.Textbox(label="Raw Text", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- # Labels now match the correct column names
63
- with gr.Row():
64
- audio1 = gr.Audio(label="Headset Microphone")
65
- audio2 = gr.Audio(label="Laryngophone (Throat Mic)")
66
- audio3 = gr.Audio(label="Soft In-Ear Microphone")
67
- with gr.Row():
68
- audio4 = gr.Audio(label="Rigid In-Ear Microphone")
69
- audio5 = gr.Audio(label="Forehead Accelerometer")
70
- audio6 = gr.Audio(label="Temple Vibration Pickup")
71
 
72
- outputs = [sentence_output, audio1, audio2, audio3, audio4, audio5, audio6]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- demo.load(fn=get_audio_row, inputs=gr.State(0), outputs=outputs)
75
- slider.change(fn=get_audio_row, inputs=slider, outputs=outputs)
 
 
 
 
76
 
77
- # Launch the application
78
  demo.launch()
 
2
  from datasets import load_dataset
3
 
4
  # --- Configuration ---
5
+ DATASET_NAME = "Cnam-LMSSC/vibravox"
6
+ SUBSETS = ["speech_clean", "speech_noisy", "speechless_clean", "speechless_noisy"]
7
+ SPLITS = ["train", "validation", "test"]
8
  TEXT_COLUMN = "raw_text"
 
 
 
 
9
  AUDIO_COLUMNS = [
10
  "audio.headset_microphone",
11
  "audio.throat_microphone",
 
15
  "audio.temple_vibration_pickup"
16
  ]
17
 
18
+ # --- Main Application Logic ---
 
 
 
 
 
 
19
 
20
+ def load_and_update_all(subset, split):
 
21
  """
22
+ This is the main function. It loads a new dataset based on user selection
23
+ and returns updates for the entire UI, including the first row of data.
24
  """
25
+ try:
26
+ # Load the newly selected dataset
27
+ dataset = load_dataset(DATASET_NAME, name=subset, split=split)
28
+
29
+ # Check if the text column exists in this subset
30
+ has_text = TEXT_COLUMN in dataset.features
31
+
32
+ # Get the first row to display immediately
33
+ sample = dataset[0]
34
+ sentence = sample[TEXT_COLUMN] if has_text else None
35
+ raw_audio_data = [
36
+ (sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
37
+ ]
38
+
39
+ # Return updates for all UI components
40
+ return (
41
+ dataset, # Update the state object
42
+ gr.update(maximum=len(dataset) - 1, value=0, visible=True, interactive=True), # Update slider
43
+ gr.update(value=sentence, visible=has_text), # Update and show/hide text box
44
+ *raw_audio_data, # Unpack audio data for all players
45
+ gr.update(value="", visible=False) # Hide any previous error messages
46
+ )
47
+ except Exception as e:
48
+ # If loading fails, show an error and hide the data components
49
+ error_message = f"Failed to load {subset}/{split}. Error: {e}"
50
+ empty_audio = (None, None)
51
+ return (
52
+ None, # Clear the state
53
+ gr.update(visible=False), # Hide slider
54
+ gr.update(visible=False), # Hide text box
55
+ *[empty_audio] * len(AUDIO_COLUMNS), # Clear all audio players
56
+ gr.update(value=error_message, visible=True) # Show the error message
57
+ )
58
+
59
 
60
+ def get_audio_row(dataset, index):
61
+ """
62
+ This function is called ONLY when the slider changes.
63
+ It fetches a new row from the currently loaded dataset (held in the state).
64
+ """
65
+ if dataset is None:
66
+ # This case handles when the initial load failed
67
+ return [None] * (1 + len(AUDIO_COLUMNS))
68
+
69
+ index = int(index)
70
+ sample = dataset[index]
71
+
72
+ has_text = TEXT_COLUMN in dataset.features
73
+ sentence = sample[TEXT_COLUMN] if has_text else None
74
 
 
 
75
  raw_audio_data = [
76
  (sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
77
  ]
78
 
79
  return [sentence] + raw_audio_data
80
 
81
+
82
  # --- Build the Gradio Interface ---
83
  with gr.Blocks(css="footer {display: none !important}") as demo:
84
+ gr.Markdown("# Vibravox Multi-Sensor Explorer")
 
 
 
 
 
 
85
 
86
+ # This state object holds the currently loaded dataset in memory
87
+ # It's invisible to the user but accessible by our functions
88
+ loaded_dataset_state = gr.State(None)
89
 
90
+ # --- INPUT CONTROLS ---
91
+ with gr.Row():
92
+ subset_dropdown = gr.Dropdown(SUBSETS, value="speech_clean", label="Select Subset")
93
+ split_dropdown = gr.Dropdown(SPLITS, value="train", label="Select Split")
94
+
95
+ # --- UI COMPONENTS FOR DATA ---
96
+ error_box = gr.Textbox(visible=False, interactive=False, container=False)
97
+ sentence_output = gr.Textbox(label="Raw Text", interactive=False, container=False)
98
+ slider = gr.Slider(label="Select Data Row", container=False)
99
+
100
+ with gr.Row():
101
+ audio1 = gr.Audio(label="Headset Microphone")
102
+ audio2 = gr.Audio(label="Laryngophone (Throat Mic)")
103
+ audio3 = gr.Audio(label="Soft In-Ear Microphone")
104
+ with gr.Row():
105
+ audio4 = gr.Audio(label="Rigid In-Ear Microphone")
106
+ audio5 = gr.Audio(label="Forehead Accelerometer")
107
+ audio6 = gr.Audio(label="Temple Vibration Pickup")
108
 
109
+ # A list of all the output components for easier reference
110
+ all_outputs = [loaded_dataset_state, slider, sentence_output, audio1, audio2, audio3, audio4, audio5, audio6, error_box]
111
+ audio_outputs = [sentence_output, audio1, audio2, audio3, audio4, audio5, audio6]
 
 
 
 
 
 
112
 
113
+ # --- WIRING THE EVENT HANDLERS ---
114
+
115
+ # 1. When the app first loads, run the main function with default values
116
+ demo.load(
117
+ fn=load_and_update_all,
118
+ inputs=[subset_dropdown, split_dropdown],
119
+ outputs=all_outputs
120
+ )
121
+
122
+ # 2. When a dropdown value changes, re-run the main function
123
+ subset_dropdown.change(
124
+ fn=load_and_update_all,
125
+ inputs=[subset_dropdown, split_dropdown],
126
+ outputs=all_outputs
127
+ )
128
+ split_dropdown.change(
129
+ fn=load_and_update_all,
130
+ inputs=[subset_dropdown, split_dropdown],
131
+ outputs=all_outputs
132
+ )
133
 
134
+ # 3. When ONLY the slider changes, run the simpler function
135
+ slider.change(
136
+ fn=get_audio_row,
137
+ inputs=[loaded_dataset_state, slider],
138
+ outputs=audio_outputs
139
+ )
140
 
 
141
  demo.launch()