Download web_ui.py from oddsilk/MidiMaker_For_Calorie_Hunters: direct link, hf CLI and curl.
- Browser
- Download file 16.3 kB
-
https://huggingface.co/spaces/oddsilk/MidiMaker_For_Calorie_Hunters/resolve/main/web_ui.py
- Command line
-
hf download hf://spaces/oddsilk/MidiMaker_For_Calorie_Hunters/web_ui.py
-
curl -L -o web_ui.py https://huggingface.co/spaces/oddsilk/MidiMaker_For_Calorie_Hunters/resolve/main/web_ui.py
16.3 kB
| import gradio as gr | |
| import os | |
| import tempfile | |
| import subprocess | |
| import sys | |
| import shutil | |
| import numpy as np | |
| import librosa | |
| import soundfile as sf | |
| import plotly.graph_objects as go | |
| from plotly.subplots import make_subplots | |
| import traceback | |
| from auto_mapper import run_pipeline | |
| LIBRARY_DIR = os.path.join(os.getcwd(), "library") | |
| os.makedirs(LIBRARY_DIR, exist_ok=True) | |
| def get_cached_songs(): | |
| songs = [] | |
| if os.path.exists(LIBRARY_DIR): | |
| for f in os.listdir(LIBRARY_DIR): | |
| if f.lower().endswith(('.wav', '.mp3')): | |
| songs.append(f) | |
| return sorted(songs) | |
| def generate_validation_data(audio_path, onsets, out_dir, bpm, offset=0.0): | |
| """ | |
| λΆμλ Onset λ°μ΄ν°λ₯Ό λ°νμΌλ‘ ν±(Click) μ¬μ΄λλ₯Ό μΆκ°ν κ²μ¦μ© μ€λμ€ νμΌκ³Ό, | |
| Waveform & Piano Roll λκΈ°ν μκ°ν Plotly Figureλ₯Ό μμ±ν©λλ€. | |
| """ | |
| times = [float(t) for t, p in onsets] | |
| # 1. μλ³Έ μ€λμ€ λ‘λ λ° ν± νΈλ μμ± | |
| y, sr = librosa.load(audio_path, sr=22050) | |
| click_track = librosa.clicks(times=times, sr=sr, length=len(y)) | |
| # μλ³Έ(μ½κ° μ€μ) + ν±(κ°νκ²) λ―Ήμ€ | |
| y_mixed = (y * 0.7) + (click_track * 1.5) | |
| # μ κ·ννμ¬ ν΄λ¦¬ν λ°©μ§ | |
| max_val = np.max(np.abs(y_mixed)) | |
| if max_val > 0: | |
| y_mixed = y_mixed / max_val | |
| mixed_path = os.path.join(out_dir, "validation_mixed.wav") | |
| sf.write(mixed_path, y_mixed, sr) | |
| # 2. Plotly νν μμ± | |
| # λΈλΌμ°μ λ©λͺ¨λ¦¬μ λ λλ§ μ±λ₯μ μν΄ λ€μ΄μνλ§ (1μ΄μ 100κ° ν¬μΈνΈ μ λ) | |
| down_factor = max(1, sr // 100) | |
| y_down = y[::down_factor] | |
| time_axis = np.linspace(0, len(y)/sr, len(y_down)) | |
| # Ableton Live μ€νμΌλ‘ μλ¨: μ€λμ€, νλ¨: λ―Έλμ λ 립λ νΈλ μμ± | |
| fig = make_subplots( | |
| rows=2, cols=1, | |
| shared_xaxes=True, | |
| vertical_spacing=0.03, | |
| row_heights=[0.5, 0.5] | |
| ) | |
| # [Track 1] μ€λμ€ νν (μμ΄λΈν€ λλμ λ°μ μ΄λ‘μ) | |
| fig.add_trace(go.Scatter( | |
| x=time_axis, | |
| y=y_down, | |
| mode='lines', | |
| name="μ€λμ€ μλ³Έ", | |
| line=dict(color='#00ff88', width=1), | |
| hoverinfo='none' | |
| ), row=1, col=1) | |
| # [Track 2] MIDI νΌμλ Έ λ‘€ (μμ΄λΈν€ λλμ νλμ λΈλ‘ νν) | |
| x_midi = [] | |
| y_midi = [] | |
| # Plotlyμμ λμ΄μ§λ μ (λΈλ‘)μ 그리기 μν΄ Noneμ μ€κ°μ μ½μ | |
| for t, p in onsets: | |
| x_midi.extend([t, t + 0.08, None]) # κ° λ ΈνΈμ μκ°μ κΈΈμ΄λ₯Ό 0.08μ΄λ‘ νν | |
| y_midi.extend([p, p, None]) | |
| fig.add_trace(go.Scatter( | |
| x=x_midi, | |
| y=y_midi, | |
| mode='lines', | |
| name="MIDI ν격 μ§μ ", | |
| line=dict(color='#3399ff', width=16), | |
| hovertemplate='μκ°: %{x:.3f}μ΄<br>νΌμΉ: %{y}<extra></extra>' | |
| ), row=2, col=1) | |
| # λ μ΄μμ μ΄κΈ°ν λ° λμμΈ | |
| min_pitch = min([p for _, p in onsets]) if onsets else 60 | |
| max_pitch = max([p for _, p in onsets]) if onsets else 64 | |
| total_time = len(y) / sr | |
| beat_duration = 60.0 / bpm | |
| measure_duration = beat_duration * 4.0 # 4/4 λ°μ κΈ°μ€ | |
| fig.update_layout( | |
| title=dict(text=f"<b>Ableton Live μ€νμΌ λ€μ€ νΈλ λκΈ°ν λ·° (BPM: {bpm})</b>", font=dict(color="#e9ecef")), | |
| template='plotly_dark', | |
| height=450, | |
| margin=dict(l=40, r=20, t=50, b=40), | |
| paper_bgcolor='rgba(0,0,0,0)', | |
| plot_bgcolor='rgba(255,255,255,0.03)', | |
| showlegend=False | |
| ) | |
| # X/YμΆ λμμΈ μ‘°μ : λ°μ(Beat) λ¨μλ‘ μΌλ° 그리λ νμ | |
| fig.update_xaxes( | |
| showgrid=True, gridcolor='rgba(255,255,255,0.1)', gridwidth=1, | |
| tickmode='linear', tick0=offset, dtick=beat_duration, | |
| title="μκ° (μ΄)", row=2, col=1 | |
| ) | |
| fig.update_xaxes( | |
| showgrid=True, gridcolor='rgba(255,255,255,0.1)', gridwidth=1, | |
| tickmode='linear', tick0=offset, dtick=beat_duration, | |
| row=1, col=1 | |
| ) | |
| # λ§λ(Measure) μμμ λ§λ€ κ΅΅μ μΈλ‘μ μΆκ° (μμλ νΈλ μ 체 κ΄ν΅) | |
| measure_time = offset | |
| while measure_time <= total_time: | |
| fig.add_vline(x=measure_time, line_width=2, line_color='rgba(255, 255, 255, 0.5)') | |
| measure_time += measure_duration | |
| # μ€λμ€ νΈλ YμΆ λμμΈ | |
| fig.update_yaxes(showgrid=False, showticklabels=False, range=[-1.1, 1.1], row=1, col=1) | |
| # MIDI νΈλ YμΆ λμμΈ (ν΄λΉνλ νΌμΉλ§ 보μ΄λλ‘) | |
| fig.update_yaxes(showgrid=True, gridcolor='rgba(255,255,255,0.1)', title="νΌμΉ", range=[min_pitch - 2, max_pitch + 2], row=2, col=1) | |
| return mixed_path, fig | |
| def process_audio(input_mode, audio_upload, audio_cached, bpm, target_stem, mode, quantize_strength, offset, shift, threshold_multiplier, min_votes, tolerance_ms, sr): | |
| try: | |
| if input_mode == "μ μ€λμ€ μ λ‘λ": | |
| if not audio_upload: | |
| return None, None, None, "β οΈ μ€λ₯: μ€λμ€ νμΌμ λ¨Όμ μ λ‘λν΄μ£ΌμΈμ." | |
| audio_path = audio_upload | |
| safe_name = os.path.basename(audio_path) | |
| library_audio_path = os.path.join(LIBRARY_DIR, safe_name) | |
| if audio_path != library_audio_path: | |
| shutil.copy(audio_path, library_audio_path) | |
| audio_path_to_use = library_audio_path | |
| base_name = os.path.splitext(safe_name)[0] | |
| else: | |
| if not audio_cached: | |
| return None, None, None, "β οΈ μ€λ₯: μΊμλ 곑μ μ νν΄μ£ΌμΈμ." | |
| audio_path_to_use = os.path.join(LIBRARY_DIR, audio_cached) | |
| base_name = os.path.splitext(audio_cached)[0] | |
| if not bpm or bpm <= 0: | |
| return None, None, None, "β οΈ μ€λ₯: μ¬λ°λ₯Έ BPM(μ: 120, 150)μ μ λ ₯ν΄μ£ΌμΈμ." | |
| out_dir = tempfile.mkdtemp() | |
| out_path = os.path.join(LIBRARY_DIR, f"{base_name}.mid") | |
| onsets = run_pipeline( | |
| audio_path=audio_path_to_use, | |
| 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): | |
| mixed_path, fig = generate_validation_data(audio_path_to_use, onsets, out_dir, float(bpm), float(offset)) | |
| return out_path, mixed_path, fig, "β μμ μ΄ μ±κ³΅μ μΌλ‘ μλ£λμμ΅λλ€." | |
| else: | |
| return None, None, None, "β οΈ μ€λ₯: MIDI νμΌ μμ±μ μ€ν¨νμ΅λλ€. (μΆλ ₯ νμΌ μμ)" | |
| except Exception as e: | |
| error_log = traceback.format_exc() | |
| print(error_log) | |
| return None, None, None, f"β μμ€ν μ€λ₯ λ°μ! μλ ν μ€νΈλ₯Ό 볡μ¬νμ¬ κ°λ°μμκ² μ λ¬ν΄μ£ΌμΈμ:\n\n{error_log}" | |
| # βββββββββββββββββββββββββββββββββββββββββββββ | |
| # κ³ κΈμ€λ¬μ΄ λμμΈμ μν Custom CSS (λ€ν¬ μ€ν¨ κ°μ κ³ μ ) | |
| # βββββββββββββββββββββββββββββββββββββββββββββ | |
| custom_css = """ | |
| .gradio-container { | |
| background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%) !important; | |
| font-family: 'Inter', 'Apple SD Gothic Neo', sans-serif !important; | |
| color: #e9ecef !important; | |
| } | |
| /* μμ€ν λΌμ΄νΈλͺ¨λμμλ μ λ ₯μ°½ λ° ν¨λμ΄ μ΄λ‘κ² λ λλ§λκ² κ°μ μ§μ */ | |
| .gradio-container input, | |
| .gradio-container textarea, | |
| .gradio-container select, | |
| .gradio-container .gr-box, | |
| .gradio-container .gr-input, | |
| .gradio-container .gr-dropdown, | |
| .gradio-container .gr-form, | |
| .gradio-container .gr-panel, | |
| .gradio-container [data-testid="textbox"] { | |
| background-color: rgba(255, 255, 255, 0.08) !important; | |
| color: #e9ecef !important; | |
| border-color: rgba(255, 255, 255, 0.1) !important; | |
| } | |
| /* ν μ€νΈ μμλ€ κ°λ μ±μ μν΄ μμ ν°μ κ³μ΄λ‘ λ³ν */ | |
| .gradio-container label, | |
| .gradio-container span, | |
| .gradio-container p { | |
| color: #e9ecef !important; | |
| } | |
| /* λλ‘λ€μ΄ 리μ€νΈ νλͺ© κ°λ μ± λ³΄μ */ | |
| ul.options > li { | |
| background-color: #1a1a2e !important; | |
| color: #e9ecef !important; | |
| } | |
| ul.options > li:hover, ul.options > li.selected { | |
| background-color: #4361ee !important; | |
| } | |
| .glass-panel { | |
| background: rgba(255, 255, 255, 0.05) !important; | |
| backdrop-filter: blur(10px) !important; | |
| border: 1px solid rgba(255, 255, 255, 0.1) !important; | |
| border-radius: 20px !important; | |
| padding: 2rem !important; | |
| box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37) !important; | |
| } | |
| h1 { | |
| background: linear-gradient(90deg, #4cc9f0, #4361ee) !important; | |
| -webkit-background-clip: text !important; | |
| -webkit-text-fill-color: transparent !important; | |
| font-weight: 800 !important; | |
| letter-spacing: -0.05em !important; | |
| } | |
| .gr-button-primary { | |
| background: linear-gradient(90deg, #4361ee, #4cc9f0) !important; | |
| border: none !important; | |
| color: white !important; | |
| transition: transform 0.2s ease !important; | |
| } | |
| .gr-button-primary:hover { | |
| transform: translateY(-2px) !important; | |
| box-shadow: 0 10px 20px rgba(67, 97, 238, 0.3) !important; | |
| } | |
| /* λΆνμν ν λ리 μ κ±° */ | |
| .gr-box, .gr-block, .gr-form, .gr-panel, fieldset, .block, .group { | |
| border-color: transparent !important; | |
| border: none !important; | |
| box-shadow: none !important; | |
| } | |
| footer {visibility: hidden} | |
| """ | |
| custom_theme = gr.themes.Soft( | |
| primary_hue="blue", | |
| secondary_hue="slate" | |
| ).set( | |
| block_border_width="0px", | |
| block_border_width_dark="0px", | |
| panel_border_width="0px", | |
| panel_border_width_dark="0px", | |
| block_border_color="transparent", | |
| block_border_color_dark="transparent", | |
| border_color_primary="transparent", | |
| border_color_primary_dark="transparent" | |
| ) | |
| # μλ°μ€ν¬λ¦½νΈλ₯Ό μ΄μ©ν΄ 무쑰건 λ€ν¬λͺ¨λλ‘ λ λλ§λκ² body ν΄λμ€ κ°μ μ§μ | |
| js_func = """ | |
| function() { | |
| document.body.classList.remove('light'); | |
| document.body.classList.add('dark'); | |
| } | |
| """ | |
| with gr.Blocks(theme=custom_theme, css=custom_css, js=js_func) as app: | |
| with gr.Column(elem_classes="glass-panel"): | |
| gr.Markdown("# π΅ UV MIDI Maker - Pro") | |
| gr.Markdown( | |
| "### μ€λμ€λ₯Ό λΆμνμ¬ μ λ°ν λ¦¬λ¬ κ²μ MIDIλ₯Ό μμ±ν©λλ€.\n" | |
| "μ΄ λꡬλ Metaμ Demucs AIλ₯Ό ν΅ν΄ μ κΈ°λ₯Ό κ³ μμ§λ‘ λΆλ¦¬νκ³ , Librosaλ₯Ό ν΅ν΄ ν격μ μ μ λ°νκ² μΆμΆν©λλ€." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### π 1. μ€λμ€ μμ€ μ λ ₯") | |
| input_mode = gr.Radio(choices=["μ μ€λμ€ μ λ‘λ", "μΊμλ 곑 μ ν"], value="μ μ€λμ€ μ λ‘λ", label="μ λ ₯ λ°©μ") | |
| with gr.Group(visible=True) as upload_group: | |
| audio_upload = gr.Audio(type="filepath", label="μ€λμ€ νμΌ μ λ‘λ (.wav, .mp3)") | |
| with gr.Group(visible=False) as cached_group: | |
| def update_cache(): | |
| return gr.Dropdown(choices=get_cached_songs()) | |
| audio_cached = gr.Dropdown(choices=get_cached_songs(), label="λΌμ΄λΈλ¬λ¦¬ μΊμ 곑 μ ν") | |
| refresh_btn = gr.Button("π λͺ©λ£ μλ‘κ³ μΉ¨", size="sm") | |
| refresh_btn.click(fn=update_cache, outputs=audio_cached) | |
| def change_input_mode(mode): | |
| if mode == "μ μ€λμ€ μ λ‘λ": | |
| return gr.update(visible=True), gr.update(visible=False) | |
| else: | |
| return gr.update(visible=False), gr.update(visible=True) | |
| input_mode.change(fn=change_input_mode, inputs=input_mode, outputs=[upload_group, cached_group]) | |
| bpm_input = gr.Number(label="λͺ©ν 곑 BPM", value=120, precision=2, info="λ¦¬λ¬ λ°μλ₯Ό λ§μΆκΈ° μν νμ κ°μ λλ€.") | |
| with gr.Accordion("βοΈ κΈ°λ³Έ μ€μ ν¨λ", open=True): | |
| target_stem_input = gr.Dropdown( | |
| choices=["drums", "vocals", "bass", "other"], | |
| value="drums", | |
| label="λΆμ λμ μ κΈ°" | |
| ) | |
| quantize_input = gr.Dropdown( | |
| choices=["1/4", "1/8", "1/16", "1/32"], | |
| value="1/16", | |
| label="ννμ΄μ¦ (그리λ)" | |
| ) | |
| offset_input = gr.Number(label="첫 λ°μ μ€νμ (μ΄ λλ μ΄)", value=0.0) | |
| shift_input = gr.Number(label="μ 체 λ ΈνΈ κ°μ μ΄λ (μ΄)", value=0.0) | |
| with gr.Accordion("π οΈ κ³ κΈ μ λ¬Έκ° μ€μ ", open=False): | |
| mode_input = gr.Radio(choices=["single", "vote"], value="single", label="μλ λͺ¨λ") | |
| threshold_input = gr.Slider(minimum=0.1, maximum=2.0, value=0.5, step=0.1, label="μλμ§ μκ³μΉ (λ―Όκ°λ)") | |
| 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): | |
| gr.Markdown("### π₯ 2. μμ±λ κ²°κ³Όλ¬Ό λ° κ²μ¦") | |
| with gr.Group(): | |
| gr.Markdown("#### π§ κ²μ¦μ© νμλΌμΈ λ° μ€λμ€") | |
| plot_output = gr.Plot(label="μ€λμ€ & λ―Έλ λ§€ν λκΈ°ν λ·°") | |
| mixed_audio_output = gr.Audio(label="ν± μλ¦¬κ° ν©μ±λ λ Έλ (미리λ£κΈ°)") | |
| midi_output = gr.File(label="MIDI νμΌ λ€μ΄λ‘λ") | |
| with gr.Group(): | |
| gr.Markdown("#### π‘ TIP") | |
| gr.Markdown("- λλΌ λΉνΈκ° κ°ν 곑μ `drums` μ€ν μ΄ κ°μ₯ μ νν©λλ€.\n" | |
| "- λΉ λ₯Έ λ©μ΄λ λ³΄μ»¬μ΄ μ£Όκ° λλ ꡬκ°μ `vocals` μ€ν μ μ¬μ©ν΄ 보μΈμ.") | |
| log_output = gr.Textbox(label="π μμ€ν λ‘κ·Έ λ° μ€λ₯ 리ν¬νΈ", lines=8, interactive=True, info="μ€λ₯κ° λ°μνλ©΄ μ΄ κ³³μ ν μ€νΈλ₯Ό 볡μ¬ν΄μ μ λ¬ν΄μ£ΌμΈμ.") | |
| submit_btn.click( | |
| fn=process_audio, | |
| inputs=[ | |
| input_mode, audio_upload, audio_cached, 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, mixed_audio_output, plot_output, log_output] | |
| ) | |
| if __name__ == "__main__": | |
| # λ‘컬 μλ² μ€ν μ€μ | |
| # share=Trueλ₯Ό μ€μ νλ©΄ μΈλΆμμλ μ μ κ°λ₯ν μμ URLμ΄ μμ±λ©λλ€. | |
| port = int(os.environ.get("PORT", 7860)) | |
| print(f"μΉ μλ²λ₯Ό μμν©λλ€. ν¬νΈ: {port}") | |
| result = app.launch( | |
| server_name="0.0.0.0", | |
| server_port=port, | |
| share=True # μ΄ μ΅μ μΌλ‘ μΈλΆ 곡μ κ°λ₯ | |
| ) | |
| # URL μΆμΆ (λ²μ λ³ μ°¨μ΄ λ°©μ΄) | |
| share_url = getattr(app, "share_url", "URL μμ± μ€...") | |
| if not share_url and isinstance(result, tuple) and len(result) >= 3: | |
| share_url = result[2] | |
| # URLμ νμΌλ‘ μ μ₯νμ¬ νμΈ κ°λ₯νκ² ν¨ | |
| with open("last_url.txt", "w") as f: | |
| f.write(str(share_url)) | |
| print(f"곡μ URL: {share_url}") | |