Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,13 @@ import gradio as gr
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
|
| 4 |
# --- Configuration ---
|
| 5 |
-
# The ONLY change is on this line: we're pointing back to the smaller test dataset.
|
| 6 |
DATASET_NAME = "Cnam-LMSSC/vibravox-test"
|
| 7 |
-
# ---------------------------------------------------------------------------------
|
| 8 |
-
|
| 9 |
SUBSETS = ["speech_clean", "speech_noisy", "speechless_clean", "speechless_noisy"]
|
| 10 |
SPLITS = ["train", "validation", "test"]
|
| 11 |
TEXT_COLUMN = "raw_text"
|
|
|
|
|
|
|
|
|
|
| 12 |
AUDIO_COLUMNS = [
|
| 13 |
"audio.headset_microphone",
|
| 14 |
"audio.throat_microphone",
|
|
@@ -22,82 +22,88 @@ AUDIO_COLUMNS = [
|
|
| 22 |
|
| 23 |
def load_and_update_all(subset, split):
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
and returns updates for the entire UI, including the first row of data.
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
-
# Load the newly selected dataset
|
| 30 |
dataset = load_dataset(DATASET_NAME, name=subset, split=split)
|
| 31 |
-
|
| 32 |
-
# Check if the text column exists in this subset
|
| 33 |
-
has_text = TEXT_COLUMN in dataset.features
|
| 34 |
|
| 35 |
# Get the first row to display immediately
|
| 36 |
sample = dataset[0]
|
| 37 |
-
sentence = sample[TEXT_COLUMN] if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 43 |
return (
|
| 44 |
-
dataset,
|
| 45 |
-
gr.update(maximum=len(dataset) - 1, value=0, visible=True, interactive=True),
|
| 46 |
-
gr.update(value=sentence, visible=
|
| 47 |
-
|
| 48 |
-
gr.update(value=
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
except Exception as e:
|
| 51 |
-
# If loading fails, show an error and hide the data components
|
| 52 |
error_message = f"Failed to load {subset}/{split}. Error: {e}"
|
| 53 |
empty_audio = (None, None)
|
|
|
|
| 54 |
return (
|
| 55 |
-
None,
|
| 56 |
-
gr.update(visible=False),
|
| 57 |
-
gr.update(visible=False),
|
| 58 |
-
*[empty_audio] * len(AUDIO_COLUMNS),
|
| 59 |
-
gr.update(value=error_message, visible=True)
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
def get_audio_row(dataset, index):
|
| 64 |
"""
|
| 65 |
-
|
| 66 |
-
It fetches a new row from the currently loaded dataset (held in the state).
|
| 67 |
"""
|
| 68 |
if dataset is None:
|
| 69 |
-
|
| 70 |
-
return [None] * (1 + len(AUDIO_COLUMNS))
|
| 71 |
|
| 72 |
index = int(index)
|
| 73 |
sample = dataset[index]
|
| 74 |
|
| 75 |
-
|
| 76 |
-
sentence = sample[TEXT_COLUMN] if
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
raw_audio_data = [
|
| 79 |
(sample[col]['sampling_rate'], sample[col]['array']) for col in AUDIO_COLUMNS
|
| 80 |
]
|
| 81 |
|
| 82 |
-
return [sentence] + raw_audio_data
|
| 83 |
-
|
| 84 |
|
| 85 |
# --- Build the Gradio Interface ---
|
| 86 |
with gr.Blocks(css="footer {display: none !important}") as demo:
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
# This state object holds the currently loaded dataset in memory
|
| 90 |
loaded_dataset_state = gr.State(None)
|
| 91 |
|
| 92 |
-
# --- INPUT CONTROLS ---
|
| 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 |
-
# --- UI COMPONENTS FOR DATA ---
|
| 98 |
error_box = gr.Textbox(visible=False, interactive=False, container=False)
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
audio1 = gr.Audio(label="Headset Microphone")
|
|
@@ -108,29 +114,21 @@ with gr.Blocks(css="footer {display: none !important}") as demo:
|
|
| 108 |
audio5 = gr.Audio(label="Forehead Accelerometer")
|
| 109 |
audio6 = gr.Audio(label="Temple Vibration Pickup")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
# --- WIRING THE EVENT HANDLERS ---
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
fn=load_and_update_all,
|
| 127 |
-
inputs=[subset_dropdown, split_dropdown],
|
| 128 |
-
outputs=all_outputs
|
| 129 |
-
)
|
| 130 |
-
slider.change(
|
| 131 |
-
fn=get_audio_row,
|
| 132 |
-
inputs=[loaded_dataset_state, slider],
|
| 133 |
-
outputs=audio_outputs
|
| 134 |
-
)
|
| 135 |
|
| 136 |
demo.launch()
|
|
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
|
| 4 |
# --- Configuration ---
|
|
|
|
| 5 |
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 = [
|
| 13 |
"audio.headset_microphone",
|
| 14 |
"audio.throat_microphone",
|
|
|
|
| 22 |
|
| 23 |
def load_and_update_all(subset, split):
|
| 24 |
"""
|
| 25 |
+
Loads a new dataset and returns updates for the entire UI.
|
|
|
|
| 26 |
"""
|
| 27 |
try:
|
|
|
|
| 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,
|
| 51 |
+
gr.update(value="", visible=False)
|
| 52 |
)
|
| 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),
|
| 60 |
+
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
|
| 61 |
+
*[empty_audio] * len(AUDIO_COLUMNS),
|
| 62 |
+
gr.update(value=error_message, visible=True)
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
def get_audio_row(dataset, index):
|
| 66 |
"""
|
| 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
|
| 83 |
]
|
| 84 |
|
| 85 |
+
return [sentence, phonemized_text, gender] + raw_audio_data
|
|
|
|
| 86 |
|
| 87 |
# --- Build the Gradio Interface ---
|
| 88 |
with gr.Blocks(css="footer {display: none !important}") as demo:
|
| 89 |
+
# Change the app title
|
| 90 |
+
gr.Markdown("# Vibravox Viewer")
|
| 91 |
|
|
|
|
| 92 |
loaded_dataset_state = gr.State(None)
|
| 93 |
|
|
|
|
| 94 |
with gr.Row():
|
| 95 |
subset_dropdown = gr.Dropdown(SUBSETS, value="speech_clean", label="Select Subset")
|
| 96 |
split_dropdown = gr.Dropdown(SPLITS, value="train", label="Select Split")
|
| 97 |
|
|
|
|
| 98 |
error_box = gr.Textbox(visible=False, interactive=False, container=False)
|
| 99 |
+
|
| 100 |
+
# Group the text outputs together
|
| 101 |
+
with gr.Row():
|
| 102 |
+
sentence_output = gr.Textbox(label="Raw Text", interactive=False)
|
| 103 |
+
phonemized_output = gr.Textbox(label="Phonemized Text", interactive=False)
|
| 104 |
+
gender_output = gr.Textbox(label="Gender", interactive=False)
|
| 105 |
+
|
| 106 |
+
slider = gr.Slider(label="Select Data Row")
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
audio1 = gr.Audio(label="Headset Microphone")
|
|
|
|
| 114 |
audio5 = gr.Audio(label="Forehead Accelerometer")
|
| 115 |
audio6 = gr.Audio(label="Temple Vibration Pickup")
|
| 116 |
|
| 117 |
+
# Update the component lists to include the new text boxes
|
| 118 |
+
all_outputs = [loaded_dataset_state, slider, sentence_output, phonemized_output, gender_output, audio1, audio2, audio3, audio4, audio5, audio6, error_box]
|
| 119 |
+
data_outputs = [sentence_output, phonemized_output, gender_output, audio1, audio2, audio3, audio4, audio5, audio6]
|
| 120 |
|
| 121 |
# --- WIRING THE EVENT HANDLERS ---
|
| 122 |
+
# The handlers themselves don't need to change, as we updated the functions and component lists
|
| 123 |
+
|
| 124 |
+
# 1. When the app first loads
|
| 125 |
+
demo.load(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
|
| 126 |
+
|
| 127 |
+
# 2. When a dropdown value changes
|
| 128 |
+
subset_dropdown.change(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
|
| 129 |
+
split_dropdown.change(fn=load_and_update_all, inputs=[subset_dropdown, split_dropdown], outputs=all_outputs)
|
| 130 |
+
|
| 131 |
+
# 3. When ONLY the slider changes
|
| 132 |
+
slider.change(fn=get_audio_row, inputs=[loaded_dataset_state, slider], outputs=data_outputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
demo.launch()
|