teszenofficial commited on
Commit
abff482
·
verified ·
1 Parent(s): afff318

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import librosa
3
+ import numpy as np
4
+ from pydub import AudioSegment, effects
5
+ import uuid, os
6
+
7
+ UPLOAD_DIR = "uploads"
8
+ OUTPUT_DIR = "outputs"
9
+ CROSSFADE_MS = 15_000 # 15s seguro en HF
10
+
11
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
12
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
13
+
14
+ # -----------------------------
15
+ # ANÁLISIS MUSICAL SEGURO
16
+ # -----------------------------
17
+
18
+ def analyze_safe(path):
19
+ try:
20
+ y, sr = librosa.load(path, mono=True)
21
+ tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
22
+
23
+ if len(beats) < 4:
24
+ raise ValueError("Pocos beats")
25
+
26
+ beat_times = librosa.frames_to_time(beats, sr=sr)
27
+ phrases = beat_times[::8] if len(beat_times) >= 8 else beat_times
28
+
29
+ return {
30
+ "tempo": tempo,
31
+ "phrases": phrases
32
+ }
33
+ except Exception:
34
+ # Fallback seguro
35
+ return {
36
+ "tempo": 120.0,
37
+ "phrases": [10.0]
38
+ }
39
+
40
+ # -----------------------------
41
+ # MEZCLADOR DJ ESTABLE
42
+ # -----------------------------
43
+
44
+ def auto_dj_mix(files, progress=gr.Progress()):
45
+ if not files or len(files) < 2 or len(files) > 4:
46
+ raise gr.Error("Sube entre 2 y 4 canciones")
47
+
48
+ progress(0, desc="Cargando canciones…")
49
+
50
+ tracks = []
51
+ analyses = []
52
+
53
+ for f in files:
54
+ ext = f.name.split(".")[-1].lower()
55
+ path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4().hex}.{ext}")
56
+
57
+ with open(f.name, "rb") as src, open(path, "wb") as dst:
58
+ dst.write(src.read())
59
+
60
+ audio = AudioSegment.from_file(path)
61
+ audio = effects.normalize(audio)
62
+
63
+ tracks.append(audio)
64
+ analyses.append(analyze_safe(path))
65
+
66
+ mix = tracks[0]
67
+ progress(30, desc="Mezclando…")
68
+
69
+ for i in range(1, len(tracks)):
70
+ prev = mix
71
+ curr = tracks[i]
72
+
73
+ info_prev = analyses[i - 1]
74
+ info_curr = analyses[i]
75
+
76
+ exit_ms = int(info_prev["phrases"][-1] * 1000)
77
+ entry_ms = int(info_curr["phrases"][0] * 1000)
78
+
79
+ exit_ms = min(exit_ms, len(prev) - CROSSFADE_MS)
80
+ entry_ms = min(entry_ms, len(curr) - CROSSFADE_MS)
81
+
82
+ prev_seg = prev[:exit_ms]
83
+ curr_seg = curr[entry_ms:]
84
+
85
+ progress(30 + i * 20, desc=f"Crossfade canción {i+1}")
86
+
87
+ mix = prev_seg.append(
88
+ curr_seg,
89
+ crossfade=CROSSFADE_MS
90
+ )
91
+
92
+ progress(90, desc="Exportando mix…")
93
+
94
+ out_name = f"monx_ai_dj_mix_{uuid.uuid4().hex}.m4a"
95
+ out_path = os.path.join(OUTPUT_DIR, out_name)
96
+
97
+ mix.export(
98
+ out_path,
99
+ format="ipod",
100
+ codec="aac",
101
+ bitrate="192k"
102
+ )
103
+
104
+ progress(100, desc="Mix listo 🎧🔥")
105
+
106
+ return out_path
107
+
108
+ # -----------------------------
109
+ # UI GRADIO
110
+ # -----------------------------
111
+
112
+ with gr.Blocks(title="MONX AI DJ") as demo:
113
+ gr.Markdown(
114
+ """
115
+ <div style="text-align:center">
116
+ <h1>🎚️ MONX AI DJ</h1>
117
+ <p>Mezcladora inteligente estable (beat-aware)</p>
118
+ </div>
119
+ """
120
+ )
121
+
122
+ files = gr.File(
123
+ label="Sube 2 a 4 canciones",
124
+ file_count="multiple",
125
+ file_types=[".mp3", ".wav", ".flac", ".m4a"]
126
+ )
127
+
128
+ btn = gr.Button("🔥 Auto DJ Mix", variant="primary")
129
+
130
+ output = gr.Audio(
131
+ label="Resultado (M4A)",
132
+ type="filepath"
133
+ )
134
+
135
+ btn.click(
136
+ fn=auto_dj_mix,
137
+ inputs=files,
138
+ outputs=output
139
+ )
140
+
141
+ if __name__ == "__main__":
142
+ demo.launch()