2ndgrade / app.py
englissi's picture
Update app.py
931afe5 verified
import gradio as gr
from huggingface_hub import hf_hub_download, list_repo_files
import os
import shutil
# ์„ค์ •: ๋ฐ์ดํ„ฐ์…‹ ID
DATASET_ID = "englissi/2ndgrade"
CACHE_DIR = "temp_audio"
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
def get_mp3_list():
try:
all_files = list_repo_files(repo_id=DATASET_ID, repo_type="dataset")
mp3_files = [f for f in all_files if f.lower().endswith('.mp3')]
return sorted(mp3_files)
except Exception as e:
print(f"List error: {e}")
return []
def play_audio(file_path):
if not file_path:
return None
try:
downloaded_path = hf_hub_download(
repo_id=DATASET_ID,
filename=file_path,
repo_type="dataset"
)
safe_filename = os.path.basename(file_path)
dest_path = os.path.join(CACHE_DIR, safe_filename)
shutil.copy2(downloaded_path, dest_path)
return dest_path
except Exception as e:
print(f"Download error: {e}")
return None
# ํŒŒ์ผ ๋ชฉ๋ก ๋ฏธ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ
mp3_list = get_mp3_list()
initial_value = mp3_list[0] if mp3_list else None
with gr.Blocks() as demo:
gr.Markdown(f"## ๐ŸŽง ์ค‘2 (์œค) 1ํ•™๊ธฐ ์Œ์›")
with gr.Column():
file_selector = gr.Dropdown(
choices=mp3_list,
value=initial_value, # ์ฒซ ๋ฒˆ์งธ ํŒŒ์ผ์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •
label="์Œ์› ํŒŒ์ผ ์„ ํƒ",
interactive=True
)
audio_player = gr.Audio(
label="์žฌ์ƒ๊ธฐ",
type="filepath",
value=play_audio(initial_value) if initial_value else None # ์‹œ์ž‘ํ•˜์ž๋งˆ์ž ์ฒซ ํŒŒ์ผ ๋กœ๋“œ
)
# ๋“œ๋กญ๋‹ค์šด ๋ณ€๊ฒฝ ์‹œ ์žฌ์ƒ๊ธฐ ์—…๋ฐ์ดํŠธ
file_selector.change(
fn=play_audio,
inputs=file_selector,
outputs=audio_player
)
if __name__ == "__main__":
demo.launch(allowed_paths=[CACHE_DIR])