| import gradio as gr |
| from huggingface_hub import hf_hub_download, list_repo_files |
| import os |
| import shutil |
|
|
| |
| 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]) |