Juna190825 commited on
Commit
14a2cc5
·
verified ·
1 Parent(s): dc3800a

Add previous and next button and autoplay toggle

Browse files
Files changed (1) hide show
  1. app.py +104 -8
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # # app.py
2
  import os
3
  import json
4
  import csv
@@ -108,6 +108,9 @@ def load_audio(repo_id, file_path):
108
  # -----------------------------
109
 
110
  def load_audio_and_metadata(repo_id, file_path):
 
 
 
111
  audio = load_audio(repo_id, file_path)
112
  metadata = load_metadata(repo_id)
113
 
@@ -132,6 +135,38 @@ def load_audio_and_metadata(repo_id, file_path):
132
  return audio, json.dumps(info, indent=2) if isinstance(info, dict) else info
133
 
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  # -----------------------------
136
  # UI
137
  # -----------------------------
@@ -140,10 +175,20 @@ with gr.Blocks() as demo:
140
  gr.Markdown("# 🎧 List, Play & View Metadata for HF Dataset Audio")
141
 
142
  repo_input = gr.Textbox(label="Dataset repo_id", value="username/dataset_name")
 
 
 
143
 
144
  with gr.Row():
145
- file_list = gr.Dropdown(label="Audio files", choices=[])
146
- play_audio = gr.Audio(label="Audio Player")
 
 
 
 
 
 
 
147
 
148
  metadata_box = gr.Textbox(label="Metadata / Text", lines=10)
149
  output = gr.Textbox(label="All Files", lines=20)
@@ -152,14 +197,65 @@ with gr.Blocks() as demo:
152
 
153
  def update_files(repo_id):
154
  audio_files, all_files_text = list_dataset_files(repo_id)
155
- return gr.Dropdown(choices=audio_files, value=None), all_files_text
 
 
 
 
 
 
 
 
 
 
 
156
 
157
- btn.click(update_files, inputs=repo_input, outputs=[file_list, output])
 
 
 
 
158
 
159
  file_list.change(
160
- load_audio_and_metadata,
161
- inputs=[repo_input, file_list],
162
  outputs=[play_audio, metadata_box]
163
  )
164
 
165
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
  import os
3
  import json
4
  import csv
 
108
  # -----------------------------
109
 
110
  def load_audio_and_metadata(repo_id, file_path):
111
+ if not file_path:
112
+ return None, "No file selected."
113
+
114
  audio = load_audio(repo_id, file_path)
115
  metadata = load_metadata(repo_id)
116
 
 
135
  return audio, json.dumps(info, indent=2) if isinstance(info, dict) else info
136
 
137
 
138
+ # -----------------------------
139
+ # NAVIGATION FUNCTIONS
140
+ # -----------------------------
141
+
142
+ def navigate_files(current_file, direction, audio_files):
143
+ """Navigate to previous or next file in the list."""
144
+ if not audio_files or current_file not in audio_files:
145
+ return current_file
146
+
147
+ current_index = audio_files.index(current_file)
148
+
149
+ if direction == "next":
150
+ new_index = (current_index + 1) % len(audio_files)
151
+ elif direction == "prev":
152
+ new_index = (current_index - 1) % len(audio_files)
153
+ else:
154
+ return current_file
155
+
156
+ return audio_files[new_index]
157
+
158
+
159
+ def update_file_selection(repo_id, current_file, direction, audio_files_state, autoplay):
160
+ """Update file selection and load new audio/metadata."""
161
+ if not audio_files_state:
162
+ return current_file, None, "No audio files available."
163
+
164
+ new_file = navigate_files(current_file, direction, audio_files_state)
165
+ audio, metadata = load_audio_and_metadata(repo_id, new_file)
166
+
167
+ return new_file, audio, metadata
168
+
169
+
170
  # -----------------------------
171
  # UI
172
  # -----------------------------
 
175
  gr.Markdown("# 🎧 List, Play & View Metadata for HF Dataset Audio")
176
 
177
  repo_input = gr.Textbox(label="Dataset repo_id", value="username/dataset_name")
178
+
179
+ # State to store audio files list
180
+ audio_files_state = gr.State([])
181
 
182
  with gr.Row():
183
+ file_list = gr.Dropdown(label="Audio files", choices=[], interactive=True)
184
+
185
+ with gr.Row():
186
+ prev_btn = gr.Button("◀ Previous")
187
+ next_btn = gr.Button("Next ▶")
188
+ autoplay_toggle = gr.Checkbox(label="Autoplay", value=True)
189
+
190
+ with gr.Row():
191
+ play_audio = gr.Audio(label="Audio Player", autoplay=True)
192
 
193
  metadata_box = gr.Textbox(label="Metadata / Text", lines=10)
194
  output = gr.Textbox(label="All Files", lines=20)
 
197
 
198
  def update_files(repo_id):
199
  audio_files, all_files_text = list_dataset_files(repo_id)
200
+ # Update state and dropdown
201
+ return (
202
+ gr.Dropdown(choices=audio_files, value=audio_files[0] if audio_files else None),
203
+ all_files_text,
204
+ audio_files # Update state
205
+ )
206
+
207
+ btn.click(
208
+ update_files,
209
+ inputs=repo_input,
210
+ outputs=[file_list, output, audio_files_state]
211
+ )
212
 
213
+ # When file is selected from dropdown
214
+ def dropdown_change_handler(repo_id, file_path, autoplay):
215
+ audio, metadata = load_audio_and_metadata(repo_id, file_path)
216
+ # Update audio player autoplay based on toggle
217
+ return gr.Audio(value=audio, autoplay=autoplay), metadata
218
 
219
  file_list.change(
220
+ dropdown_change_handler,
221
+ inputs=[repo_input, file_list, autoplay_toggle],
222
  outputs=[play_audio, metadata_box]
223
  )
224
 
225
+ # Previous button click
226
+ def prev_button_handler(repo_id, current_file, audio_files_state, autoplay):
227
+ new_file, audio, metadata = update_file_selection(
228
+ repo_id, current_file, "prev", audio_files_state, autoplay
229
+ )
230
+ return new_file, gr.Audio(value=audio, autoplay=autoplay), metadata
231
+
232
+ prev_btn.click(
233
+ prev_button_handler,
234
+ inputs=[repo_input, file_list, audio_files_state, autoplay_toggle],
235
+ outputs=[file_list, play_audio, metadata_box]
236
+ )
237
+
238
+ # Next button click
239
+ def next_button_handler(repo_id, current_file, audio_files_state, autoplay):
240
+ new_file, audio, metadata = update_file_selection(
241
+ repo_id, current_file, "next", audio_files_state, autoplay
242
+ )
243
+ return new_file, gr.Audio(value=audio, autoplay=autoplay), metadata
244
+
245
+ next_btn.click(
246
+ next_button_handler,
247
+ inputs=[repo_input, file_list, audio_files_state, autoplay_toggle],
248
+ outputs=[file_list, play_audio, metadata_box]
249
+ )
250
+
251
+ # Autoplay toggle change handler
252
+ def autoplay_changed(autoplay, current_audio):
253
+ return gr.Audio(value=current_audio, autoplay=autoplay)
254
+
255
+ autoplay_toggle.change(
256
+ autoplay_changed,
257
+ inputs=[autoplay_toggle, play_audio],
258
+ outputs=[play_audio]
259
+ )
260
+
261
+ demo.launch()