teszenofficial commited on
Commit
42371e3
·
verified ·
1 Parent(s): 6355795

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
3
+ from pydub import AudioSegment
4
+ import os
5
+ import uuid
6
+
7
+ app = FastAPI(title="MONX AI")
8
+
9
+ UPLOAD_DIR = "uploads"
10
+ OUTPUT_DIR = "outputs"
11
+ CROSSFADE_MS = 20_000 # 20 segundos reales estilo Apple Music
12
+
13
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
14
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
15
+
16
+ # ======================
17
+ # AUDIO ENGINE
18
+ # ======================
19
+
20
+ def smart_crossfade(track_a, track_b):
21
+ return track_a.append(track_b, crossfade=CROSSFADE_MS)
22
+
23
+ def build_mix(paths):
24
+ current = AudioSegment.from_file(paths[0])
25
+
26
+ for p in paths[1:]:
27
+ next_track = AudioSegment.from_file(p)
28
+ current = smart_crossfade(current, next_track)
29
+
30
+ out_name = f"{uuid.uuid4().hex}.wav"
31
+ out_path = os.path.join(OUTPUT_DIR, out_name)
32
+ current.export(out_path, format="wav")
33
+ return out_name
34
+
35
+ # ======================
36
+ # WEB UI
37
+ # ======================
38
+
39
+ @app.get("/", response_class=HTMLResponse)
40
+ def home():
41
+ return """
42
+ <!DOCTYPE html>
43
+ <html lang="es">
44
+ <head>
45
+ <meta charset="UTF-8">
46
+ <title>MONX AI</title>
47
+
48
+ <link rel="stylesheet"
49
+ href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
50
+
51
+ <style>
52
+ body{
53
+ background:linear-gradient(135deg,#0f0c29,#302b63,#24243e);
54
+ color:#fff;font-family:Arial;margin:0
55
+ }
56
+ .box{
57
+ max-width:520px;margin:60px auto;padding:30px;
58
+ background:rgba(0,0,0,.4);border-radius:16px;text-align:center
59
+ }
60
+ .upload{
61
+ border:2px dashed #7fdbff;padding:20px;border-radius:14px;cursor:pointer
62
+ }
63
+ input{display:none}
64
+ button{
65
+ margin-top:20px;padding:12px 20px;border:none;border-radius:12px;
66
+ background:#7fdbff;color:#000;font-size:16px;cursor:pointer
67
+ }
68
+ audio{margin-top:20px;width:100%}
69
+ </style>
70
+ </head>
71
+
72
+ <body>
73
+ <div class="box">
74
+ <h1><i class="fa-solid fa-wave-square"></i> MONX AI</h1>
75
+ <p>Sube 2 a 4 canciones y crea un mix inteligente</p>
76
+
77
+ <label class="upload">
78
+ <i class="fa-solid fa-cloud-arrow-up fa-2x"></i><br><br>
79
+ Seleccionar canciones
80
+ <input type="file" id="files" multiple accept=".mp3,.wav">
81
+ </label>
82
+
83
+ <button onclick="send()">Crear mix</button>
84
+
85
+ <p id="status"></p>
86
+ <audio id="player" controls></audio>
87
+ </div>
88
+
89
+ <script>
90
+ async function send(){
91
+ const files=document.getElementById("files").files;
92
+ const status=document.getElementById("status");
93
+ const player=document.getElementById("player");
94
+
95
+ if(files.length<2||files.length>4){
96
+ status.innerText="⚠️ Sube entre 2 y 4 canciones";
97
+ return;
98
+ }
99
+
100
+ let fd=new FormData();
101
+ for(let f of files){fd.append("files",f)}
102
+
103
+ status.innerText="⏳ Procesando mix...";
104
+
105
+ const r=await fetch("/upload-mix",{method:"POST",body:fd});
106
+ const j=await r.json();
107
+
108
+ if(j.error){status.innerText="❌ "+j.error;return;}
109
+
110
+ status.innerText="✅ Mix listo";
111
+ player.src="/download?file="+j.file;
112
+ player.load();
113
+ }
114
+ </script>
115
+ </body>
116
+ </html>
117
+ """
118
+
119
+ # ======================
120
+ # API
121
+ # ======================
122
+
123
+ @app.post("/upload-mix")
124
+ async def upload_and_mix(files=File(...)):
125
+ if len(files) < 2 or len(files) > 4:
126
+ return JSONResponse({"error": "Sube entre 2 y 4 canciones"}, status_code=400)
127
+
128
+ paths = []
129
+
130
+ for f in files:
131
+ ext = f.filename.split(".")[-1].lower()
132
+ if ext not in ["mp3", "wav"]:
133
+ return JSONResponse({"error": "Formato no soportado"}, status_code=400)
134
+
135
+ name = f"{uuid.uuid4().hex}.{ext}"
136
+ path = os.path.join(UPLOAD_DIR, name)
137
+
138
+ with open(path, "wb") as out:
139
+ out.write(await f.read())
140
+
141
+ paths.append(path)
142
+
143
+ out = build_mix(paths)
144
+ return {"file": out}
145
+
146
+ @app.get("/download")
147
+ def download(file: str):
148
+ return FileResponse(
149
+ os.path.join(OUTPUT_DIR, file),
150
+ media_type="audio/wav",
151
+ filename="monx_ai_mix.wav"
152
+ )