import gradio as gr import os import tempfile import subprocess import sys from auto_mapper import run_pipeline def process_audio(audio_path, bpm, target_stem, mode, quantize_strength, offset, shift, threshold_multiplier, min_votes, tolerance_ms, sr): if not audio_path: raise gr.Error("오디오 파일을 먼저 업로드해주세요.") if not bpm or bpm <= 0: raise gr.Error("올바른 BPM(예: 120, 150)을 입력해주세요.") out_dir = tempfile.mkdtemp() out_path = os.path.join(out_dir, "mapped_output.mid") try: run_pipeline( audio_path=audio_path, output_path=out_path, bpm=float(bpm), threshold_multiplier=float(threshold_multiplier), quantize_strength=quantize_strength, sr=int(sr), target_stem=target_stem, mode=mode, min_votes=int(min_votes), tolerance_ms=float(tolerance_ms), offset=float(offset), shift=float(shift) ) if os.path.exists(out_path): return out_path else: raise gr.Error("MIDI 파일 생성에 실패했습니다. (출력 파일 없음)") except Exception as e: raise gr.Error(f"오류가 발생했습니다: {str(e)}") with gr.Blocks(theme=gr.themes.Soft()) as app: gr.Markdown("# 🎵 리듬 게임 오토매핑 웹 UI 공유 센터") gr.Markdown( "팀원들과 함께 사용할 수 있는 오토매핑 툴입니다.\n\n" "이 페이지의 **외부 주소(Public URL)**를 팀원에게 전달하면, 팀원들은 파이썬이나 개발 환경 없이도 웹 브라우저에서 바로 사용할 수 있습니다." ) with gr.Row(): with gr.Column(scale=1): audio_input = gr.Audio(type="filepath", label="1️⃣ 오디오 파일 업로드 (.wav, .mp3)") bpm_input = gr.Number(label="2️⃣ 목표 곡 BPM (필수 숫자)", value=120, precision=2) with gr.Accordion("기본 설정 패널 (펼치기/접기)", open=True): target_stem_input = gr.Dropdown(choices=["drums", "vocals", "bass", "other"], value="drums", label="분석 악기 (기본: drums)") quantize_input = gr.Dropdown(choices=["1/4", "1/8", "1/16", "1/32"], value="1/16", label="퀀타이즈 그리드 단위") offset_input = gr.Number(label="첫 박자 오프셋 (초 딜레이, 예: 0.045)", value=0.0) shift_input = gr.Number(label="전체 노트 강제 이동 (초, 예: -0.025는 앞당김)", value=0.0) with gr.Accordion("고급 설정 패널 (전문가용)", open=False): mode_input = gr.Radio(choices=["single", "vote"], value="single", label="작동 모드 (기본: single)") threshold_input = gr.Slider(minimum=0.1, maximum=2.0, value=0.5, step=0.1, label="드럼 에너지 임계치 (Threshold)") min_votes_input = gr.Slider(minimum=1, maximum=4, value=2, step=1, label="최소 합의 수 (Vote 모드 한정)") tolerance_input = gr.Number(label="합의 허용 오차 (ms)", value=50.0) sr_input = gr.Number(label="오디오 샘플레이트 (Hz)", value=22050) submit_btn = gr.Button("🚀 MIDI 파이프라인 가동", variant="primary") with gr.Column(scale=1): midi_output = gr.File(label="📥 생성된 결과물 (MIDI 파일 다운로드)") submit_btn.click( fn=process_audio, inputs=[ audio_input, bpm_input, target_stem_input, mode_input, quantize_input, offset_input, shift_input, threshold_input, min_votes_input, tolerance_input, sr_input ], outputs=midi_output ) if __name__ == "__main__": # share=True 설정 시 ngrok을 쓰지 않아도 자동 외부 링크 발급(72시간) print("웹 서버를 시작합니다. 파이썬 콘솔에 출력되는 \"Running on public URL\" 주소를 팀원분들에게 전달해주세요!") app.launch(share=True, server_name="0.0.0.0")