Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
|
| 4 |
+
# Load dataset
|
| 5 |
+
ds = load_dataset("your-username/your-dataset", split="train")
|
| 6 |
+
|
| 7 |
+
# Build list of audio paths and mapping to full rows
|
| 8 |
+
audio_paths = [row["audio"] for row in ds]
|
| 9 |
+
audio_map = {row["audio"]: row for row in ds}
|
| 10 |
+
|
| 11 |
+
def load_entry(index):
|
| 12 |
+
"""Return audio + metadata for the given index."""
|
| 13 |
+
index = int(index)
|
| 14 |
+
path = audio_paths[index]
|
| 15 |
+
row = audio_map[path]
|
| 16 |
+
|
| 17 |
+
# Build metadata markdown
|
| 18 |
+
md = f"""
|
| 19 |
+
### Metadata
|
| 20 |
+
**Text:** {row.get("text", "")}
|
| 21 |
+
|
| 22 |
+
**Speaker ID:** {row.get("speaker_id", "")}
|
| 23 |
+
**Gender:** {row.get("gender", "")}
|
| 24 |
+
**Age:** {row.get("age", "")}
|
| 25 |
+
**Accent:** {row.get("accent", "")}
|
| 26 |
+
**Genre:** {row.get("genre", "")}
|
| 27 |
+
|
| 28 |
+
**Start Time:** {row.get("start_time", "")}
|
| 29 |
+
**End Time:** {row.get("end_time", "")}
|
| 30 |
+
**Duration:** {row.get("duration", "")}
|
| 31 |
+
**Sampling Rate:** {row.get("sampling_rate", "")}
|
| 32 |
+
**Split:** {row.get("split", "")}
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
return path, md, index, path
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def next_audio(current_index):
|
| 39 |
+
"""Move to the next audio file."""
|
| 40 |
+
new_index = (current_index + 1) % len(audio_paths)
|
| 41 |
+
return load_entry(new_index)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def prev_audio(current_index):
|
| 45 |
+
"""Move to the previous audio file."""
|
| 46 |
+
new_index = (current_index - 1) % len(audio_paths)
|
| 47 |
+
return load_entry(new_index)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("# 🎧 Audio Dataset Browser with Navigation")
|
| 52 |
+
|
| 53 |
+
# State: current index
|
| 54 |
+
current_index = gr.State(0)
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
dropdown = gr.Dropdown(
|
| 58 |
+
choices=audio_paths,
|
| 59 |
+
label="Select an audio file",
|
| 60 |
+
value=audio_paths[0],
|
| 61 |
+
interactive=True
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
player = gr.Audio(label="Audio Player")
|
| 66 |
+
metadata_box = gr.Markdown()
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
prev_btn = gr.Button("⬅️ Previous")
|
| 70 |
+
next_btn = gr.Button("Next ➡️")
|
| 71 |
+
|
| 72 |
+
# Dropdown selection updates everything
|
| 73 |
+
dropdown.change(
|
| 74 |
+
fn=lambda path: load_entry(audio_paths.index(path)),
|
| 75 |
+
inputs=dropdown,
|
| 76 |
+
outputs=[player, metadata_box, current_index, dropdown]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Next button
|
| 80 |
+
next_btn.click(
|
| 81 |
+
fn=next_audio,
|
| 82 |
+
inputs=current_index,
|
| 83 |
+
outputs=[player, metadata_box, current_index, dropdown]
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Previous button
|
| 87 |
+
prev_btn.click(
|
| 88 |
+
fn=prev_audio,
|
| 89 |
+
inputs=current_index,
|
| 90 |
+
outputs=[player, metadata_box, current_index, dropdown]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
demo.launch()
|