File size: 8,135 Bytes
14a2cc5
3422a71
dc3800a
 
 
 
7f6709e
15f0c84
7f6709e
9305d26
 
 
 
 
 
3422a71
7f6709e
0c589fe
 
3422a71
 
 
 
 
0c589fe
7f6709e
3422a71
0c589fe
 
 
 
 
3422a71
0c589fe
 
dc3800a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7dbb6f
0c589fe
 
 
 
 
 
 
 
f7dbb6f
 
 
 
 
 
 
dc3800a
0c589fe
7f6709e
f7dbb6f
dc3800a
 
 
 
 
14a2cc5
 
 
dc3800a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14a2cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc3800a
 
 
 
3422a71
dc3800a
0c589fe
 
14a2cc5
 
 
0c589fe
 
14a2cc5
 
 
 
 
 
 
 
 
0c589fe
dc3800a
0c589fe
dc3800a
3422a71
7f6709e
0c589fe
 
14a2cc5
 
 
 
 
 
 
 
 
 
 
 
0c589fe
14a2cc5
 
 
 
 
0c589fe
 
14a2cc5
 
dc3800a
0c589fe
7f6709e
14a2cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# app.py
import os
import json
import csv
import tempfile
import shutil
import gradio as gr
from huggingface_hub import HfApi, hf_hub_download, login

hf_token = os.environ.get("HF_TOKEN")
if hf_token is None:
    raise ValueError("HF_TOKEN environment variable not set")
    
login(token=hf_token)

api = HfApi()

AUDIO_EXTS = {".wav", ".mp3", ".flac", ".ogg", ".m4a"}

def list_dataset_files(repo_id: str):
    try:
        files = api.list_repo_files(
            repo_id=repo_id,
            repo_type="dataset",
            token=os.getenv("HF_TOKEN")
        )
        if not files:
            return [], "No files found."

        audio_files = [f for f in files if os.path.splitext(f)[1].lower() in AUDIO_EXTS]
        return audio_files, "\n".join(files)

    except Exception as e:
        return [], f"Error: {e}"


# -----------------------------
# METADATA LOADING
# -----------------------------

def load_metadata(repo_id):
    """Download and parse metadata files if they exist."""
    metadata = {}

    # Possible metadata files
    candidates = ["metadata.jsonl"] #, "metadata.json", "metadata.csv"]

    for fname in candidates:
        try:
            path = hf_hub_download(
                repo_id=repo_id,
                filename=fname,
                repo_type="dataset",
                token=os.getenv("HF_TOKEN")
            )

            ext = os.path.splitext(fname)[1]

            if ext == ".jsonl":
                with open(path, "r", encoding="utf-8") as f:
                    for line in f:
                        item = json.loads(line)
                        audio = item.get("audio") or item.get("file")
                        if audio:
                            metadata[audio] = item

            elif ext == ".json":
                with open(path, "r", encoding="utf-8") as f:
                    data = json.load(f)
                    for item in data:
                        audio = item.get("audio") or item.get("file")
                        if audio:
                            metadata[audio] = item

            elif ext == ".csv":
                with open(path, "r", encoding="utf-8") as f:
                    reader = csv.DictReader(f)
                    for row in reader:
                        audio = row.get("audio") or row.get("file")
                        if audio:
                            metadata[audio] = row

        except Exception:
            pass  # File doesn't exist, skip

    return metadata


# -----------------------------
# AUDIO LOADING
# -----------------------------

def load_audio(repo_id, file_path):
    try:
        local_path = hf_hub_download(
            repo_id=repo_id,
            filename=file_path,
            repo_type="dataset",
            token=os.getenv("HF_TOKEN")
        )

        tmp_dir = tempfile.gettempdir()
        safe_path = os.path.join(tmp_dir, os.path.basename(local_path))
        shutil.copy(local_path, safe_path)

        return safe_path

    except Exception:
        return None


# -----------------------------
# COMBINED HANDLER
# -----------------------------

def load_audio_and_metadata(repo_id, file_path):
    if not file_path:
        return None, "No file selected."
    
    audio = load_audio(repo_id, file_path)
    metadata = load_metadata(repo_id)

    # Try to match metadata
    info = metadata.get(file_path, "")

    # If no metadata file, try matching .txt file
    if not info:
        txt_candidate = file_path.replace(".wav", ".txt").replace(".mp3", ".txt")
        try:
            txt_path = hf_hub_download(
                repo_id=repo_id,
                filename=txt_candidate,
                repo_type="dataset",
                token=os.getenv("HF_TOKEN")
            )
            with open(txt_path, "r", encoding="utf-8") as f:
                info = f.read()
        except Exception:
            info = "No metadata found."

    return audio, json.dumps(info, indent=2) if isinstance(info, dict) else info


# -----------------------------
# NAVIGATION FUNCTIONS
# -----------------------------

def navigate_files(current_file, direction, audio_files):
    """Navigate to previous or next file in the list."""
    if not audio_files or current_file not in audio_files:
        return current_file
    
    current_index = audio_files.index(current_file)
    
    if direction == "next":
        new_index = (current_index + 1) % len(audio_files)
    elif direction == "prev":
        new_index = (current_index - 1) % len(audio_files)
    else:
        return current_file
    
    return audio_files[new_index]


def update_file_selection(repo_id, current_file, direction, audio_files_state, autoplay):
    """Update file selection and load new audio/metadata."""
    if not audio_files_state:
        return current_file, None, "No audio files available."
    
    new_file = navigate_files(current_file, direction, audio_files_state)
    audio, metadata = load_audio_and_metadata(repo_id, new_file)
    
    return new_file, audio, metadata


# -----------------------------
# UI
# -----------------------------

with gr.Blocks() as demo:
    gr.Markdown("# 🎧 List, Play & View Metadata for HF Dataset Audio")

    repo_input = gr.Textbox(label="Dataset repo_id", value="username/dataset_name")
    
    # State to store audio files list
    audio_files_state = gr.State([])

    with gr.Row():
        file_list = gr.Dropdown(label="Audio files", choices=[], interactive=True)
        
    with gr.Row():
        prev_btn = gr.Button("◀ Previous")
        next_btn = gr.Button("Next ▶")
        autoplay_toggle = gr.Checkbox(label="Autoplay", value=True)
    
    with gr.Row():
        play_audio = gr.Audio(label="Audio Player", autoplay=True)

    metadata_box = gr.Textbox(label="Metadata / Text", lines=10)
    output = gr.Textbox(label="All Files", lines=20)

    btn = gr.Button("List files")

    def update_files(repo_id):
        audio_files, all_files_text = list_dataset_files(repo_id)
        # Update state and dropdown
        return (
            gr.Dropdown(choices=audio_files, value=audio_files[0] if audio_files else None),
            all_files_text,
            audio_files  # Update state
        )

    btn.click(
        update_files, 
        inputs=repo_input, 
        outputs=[file_list, output, audio_files_state]
    )

    # When file is selected from dropdown
    def dropdown_change_handler(repo_id, file_path, autoplay):
        audio, metadata = load_audio_and_metadata(repo_id, file_path)
        # Update audio player autoplay based on toggle
        return gr.Audio(value=audio, autoplay=autoplay), metadata

    file_list.change(
        dropdown_change_handler,
        inputs=[repo_input, file_list, autoplay_toggle],
        outputs=[play_audio, metadata_box]
    )

    # Previous button click
    def prev_button_handler(repo_id, current_file, audio_files_state, autoplay):
        new_file, audio, metadata = update_file_selection(
            repo_id, current_file, "prev", audio_files_state, autoplay
        )
        return new_file, gr.Audio(value=audio, autoplay=autoplay), metadata

    prev_btn.click(
        prev_button_handler,
        inputs=[repo_input, file_list, audio_files_state, autoplay_toggle],
        outputs=[file_list, play_audio, metadata_box]
    )

    # Next button click
    def next_button_handler(repo_id, current_file, audio_files_state, autoplay):
        new_file, audio, metadata = update_file_selection(
            repo_id, current_file, "next", audio_files_state, autoplay
        )
        return new_file, gr.Audio(value=audio, autoplay=autoplay), metadata

    next_btn.click(
        next_button_handler,
        inputs=[repo_input, file_list, audio_files_state, autoplay_toggle],
        outputs=[file_list, play_audio, metadata_box]
    )
    
    # Autoplay toggle change handler
    def autoplay_changed(autoplay, current_audio):
        return gr.Audio(value=current_audio, autoplay=autoplay)
    
    autoplay_toggle.change(
        autoplay_changed,
        inputs=[autoplay_toggle, play_audio],
        outputs=[play_audio]
    )

demo.launch()