Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,97 @@
|
|
| 1 |
-
# app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
-
from huggingface_hub import HfApi
|
| 5 |
|
| 6 |
api = HfApi()
|
| 7 |
|
|
|
|
|
|
|
| 8 |
def list_dataset_files(repo_id: str):
|
| 9 |
-
# repo_id example: "username/dataset_name"
|
| 10 |
try:
|
| 11 |
files = api.list_repo_files(
|
| 12 |
repo_id=repo_id,
|
| 13 |
repo_type="dataset",
|
| 14 |
-
token=os.getenv("HF_TOKEN")
|
| 15 |
)
|
| 16 |
if not files:
|
| 17 |
-
return "No files found."
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
-
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
with gr.Blocks() as demo:
|
| 23 |
-
gr.Markdown("# List
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
btn = gr.Button("List files")
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
demo.launch()
|
|
|
|
| 1 |
+
# # app.py
|
| 2 |
+
# import os
|
| 3 |
+
# import gradio as gr
|
| 4 |
+
# from huggingface_hub import HfApi
|
| 5 |
+
|
| 6 |
+
# api = HfApi()
|
| 7 |
+
|
| 8 |
+
# def list_dataset_files(repo_id: str):
|
| 9 |
+
# # repo_id example: "username/dataset_name"
|
| 10 |
+
# try:
|
| 11 |
+
# files = api.list_repo_files(
|
| 12 |
+
# repo_id=repo_id,
|
| 13 |
+
# repo_type="dataset",
|
| 14 |
+
# token=os.getenv("HF_TOKEN") # None for public datasets
|
| 15 |
+
# )
|
| 16 |
+
# if not files:
|
| 17 |
+
# return "No files found."
|
| 18 |
+
# return "\n".join(files)
|
| 19 |
+
# except Exception as e:
|
| 20 |
+
# return f"Error: {e}"
|
| 21 |
+
|
| 22 |
+
# with gr.Blocks() as demo:
|
| 23 |
+
# gr.Markdown("# List files in a Hugging Face dataset repo")
|
| 24 |
+
# repo_input = gr.Textbox(label="Dataset repo_id", value="Juna190825/ZomiAudioDataset")
|
| 25 |
+
# output = gr.Textbox(label="Files", lines=20)
|
| 26 |
+
# btn = gr.Button("List files")
|
| 27 |
+
|
| 28 |
+
# btn.click(list_dataset_files, inputs=repo_input, outputs=output)
|
| 29 |
+
|
| 30 |
+
# demo.launch()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
import os
|
| 34 |
import gradio as gr
|
| 35 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 36 |
|
| 37 |
api = HfApi()
|
| 38 |
|
| 39 |
+
AUDIO_EXTS = {".wav", ".mp3", ".flac", ".ogg", ".m4a"}
|
| 40 |
+
|
| 41 |
def list_dataset_files(repo_id: str):
|
|
|
|
| 42 |
try:
|
| 43 |
files = api.list_repo_files(
|
| 44 |
repo_id=repo_id,
|
| 45 |
repo_type="dataset",
|
| 46 |
+
token=os.getenv("HF_TOKEN")
|
| 47 |
)
|
| 48 |
if not files:
|
| 49 |
+
return [], "No files found."
|
| 50 |
+
|
| 51 |
+
# Filter audio files
|
| 52 |
+
audio_files = [f for f in files if os.path.splitext(f)[1].lower() in AUDIO_EXTS]
|
| 53 |
+
|
| 54 |
+
return audio_files, "\n".join(files)
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
return [], f"Error: {e}"
|
| 58 |
+
|
| 59 |
+
def load_audio(repo_id, file_path):
|
| 60 |
+
try:
|
| 61 |
+
local_path = hf_hub_download(
|
| 62 |
+
repo_id=repo_id,
|
| 63 |
+
filename=file_path,
|
| 64 |
+
repo_type="dataset",
|
| 65 |
+
token=os.getenv("HF_TOKEN")
|
| 66 |
+
)
|
| 67 |
+
return local_path
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return None
|
| 70 |
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("# 🎧 List & Play Audio Files from a HF Dataset Repo")
|
| 73 |
+
|
| 74 |
+
repo_input = gr.Textbox(label="Dataset repo_id", value="username/dataset_name")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
file_list = gr.Dropdown(label="Audio files", choices=[])
|
| 78 |
+
play_audio = gr.Audio(label="Audio Player")
|
| 79 |
+
|
| 80 |
+
output = gr.Textbox(label="All Files", lines=20)
|
| 81 |
btn = gr.Button("List files")
|
| 82 |
|
| 83 |
+
# When clicking "List files"
|
| 84 |
+
def update_files(repo_id):
|
| 85 |
+
audio_files, all_files_text = list_dataset_files(repo_id)
|
| 86 |
+
return gr.Dropdown(choices=audio_files, value=None), all_files_text
|
| 87 |
+
|
| 88 |
+
btn.click(update_files, inputs=repo_input, outputs=[file_list, output])
|
| 89 |
+
|
| 90 |
+
# When selecting an audio file
|
| 91 |
+
file_list.change(
|
| 92 |
+
load_audio,
|
| 93 |
+
inputs=[repo_input, file_list],
|
| 94 |
+
outputs=play_audio
|
| 95 |
+
)
|
| 96 |
|
| 97 |
demo.launch()
|