Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,8 @@ import shutil
|
|
| 5 |
|
| 6 |
# 설정: 데이터셋 ID
|
| 7 |
DATASET_ID = "englissi/2ndgrade"
|
| 8 |
-
# 파일을 임시로 저장할 로컬 폴더
|
| 9 |
CACHE_DIR = "temp_audio"
|
| 10 |
|
| 11 |
-
# 임시 폴더 생성
|
| 12 |
if not os.path.exists(CACHE_DIR):
|
| 13 |
os.makedirs(CACHE_DIR)
|
| 14 |
|
|
@@ -18,49 +16,51 @@ def get_mp3_list():
|
|
| 18 |
mp3_files = [f for f in all_files if f.lower().endswith('.mp3')]
|
| 19 |
return sorted(mp3_files)
|
| 20 |
except Exception as e:
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
def play_audio(file_path):
|
| 24 |
-
if not file_path
|
| 25 |
return None
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
# 1. HF Hub에서 파일을 캐시 디렉토리로 다운로드
|
| 29 |
downloaded_path = hf_hub_download(
|
| 30 |
repo_id=DATASET_ID,
|
| 31 |
filename=file_path,
|
| 32 |
repo_type="dataset"
|
| 33 |
)
|
| 34 |
|
| 35 |
-
# 2. Gradio가 접근 가능한 현재 작업 디렉토리의 temp 폴더로 파일 복사
|
| 36 |
-
# 파일명만 추출하여 안전한 경로 생성
|
| 37 |
safe_filename = os.path.basename(file_path)
|
| 38 |
dest_path = os.path.join(CACHE_DIR, safe_filename)
|
| 39 |
-
|
| 40 |
shutil.copy2(downloaded_path, dest_path)
|
| 41 |
|
| 42 |
return dest_path
|
| 43 |
except Exception as e:
|
| 44 |
-
print(f"
|
| 45 |
return None
|
| 46 |
|
| 47 |
-
#
|
|
|
|
|
|
|
|
|
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
-
gr.Markdown(f"## 🎧 중
|
| 50 |
-
gr.Markdown("파일을 선택하면 보안 경로 오류 없이 재생됩니다.")
|
| 51 |
|
| 52 |
with gr.Column():
|
| 53 |
file_selector = gr.Dropdown(
|
| 54 |
-
choices=
|
|
|
|
| 55 |
label="음원 파일 선택",
|
| 56 |
interactive=True
|
| 57 |
)
|
| 58 |
|
| 59 |
audio_player = gr.Audio(
|
| 60 |
label="재생기",
|
| 61 |
-
type="filepath"
|
|
|
|
| 62 |
)
|
| 63 |
|
|
|
|
| 64 |
file_selector.change(
|
| 65 |
fn=play_audio,
|
| 66 |
inputs=file_selector,
|
|
@@ -68,5 +68,4 @@ with gr.Blocks() as demo:
|
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
# 보안 허용 경로를 명시적으로 추가 (필요 시)
|
| 72 |
demo.launch(allowed_paths=[CACHE_DIR])
|
|
|
|
| 5 |
|
| 6 |
# 설정: 데이터셋 ID
|
| 7 |
DATASET_ID = "englissi/2ndgrade"
|
|
|
|
| 8 |
CACHE_DIR = "temp_audio"
|
| 9 |
|
|
|
|
| 10 |
if not os.path.exists(CACHE_DIR):
|
| 11 |
os.makedirs(CACHE_DIR)
|
| 12 |
|
|
|
|
| 16 |
mp3_files = [f for f in all_files if f.lower().endswith('.mp3')]
|
| 17 |
return sorted(mp3_files)
|
| 18 |
except Exception as e:
|
| 19 |
+
print(f"List error: {e}")
|
| 20 |
+
return []
|
| 21 |
|
| 22 |
def play_audio(file_path):
|
| 23 |
+
if not file_path:
|
| 24 |
return None
|
| 25 |
|
| 26 |
try:
|
|
|
|
| 27 |
downloaded_path = hf_hub_download(
|
| 28 |
repo_id=DATASET_ID,
|
| 29 |
filename=file_path,
|
| 30 |
repo_type="dataset"
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
| 33 |
safe_filename = os.path.basename(file_path)
|
| 34 |
dest_path = os.path.join(CACHE_DIR, safe_filename)
|
|
|
|
| 35 |
shutil.copy2(downloaded_path, dest_path)
|
| 36 |
|
| 37 |
return dest_path
|
| 38 |
except Exception as e:
|
| 39 |
+
print(f"Download error: {e}")
|
| 40 |
return None
|
| 41 |
|
| 42 |
+
# 파일 목록 미리 가져오기
|
| 43 |
+
mp3_list = get_mp3_list()
|
| 44 |
+
initial_value = mp3_list[0] if mp3_list else None
|
| 45 |
+
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown(f"## 🎧 중2 (윤) 1학기 음원")
|
|
|
|
| 48 |
|
| 49 |
with gr.Column():
|
| 50 |
file_selector = gr.Dropdown(
|
| 51 |
+
choices=mp3_list,
|
| 52 |
+
value=initial_value, # 첫 번째 파일을 기본값으로 설정
|
| 53 |
label="음원 파일 선택",
|
| 54 |
interactive=True
|
| 55 |
)
|
| 56 |
|
| 57 |
audio_player = gr.Audio(
|
| 58 |
label="재생기",
|
| 59 |
+
type="filepath",
|
| 60 |
+
value=play_audio(initial_value) if initial_value else None # 시작하자마자 첫 파일 로드
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# 드롭다운 변경 시 재생기 업데이트
|
| 64 |
file_selector.change(
|
| 65 |
fn=play_audio,
|
| 66 |
inputs=file_selector,
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 71 |
demo.launch(allowed_paths=[CACHE_DIR])
|