teszenofficial commited on
Commit
65f3324
·
verified ·
1 Parent(s): 6cf9b8c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
+ nxt = AudioSegment.from_file(p)
28
+ current = smart_crossfade(current, nxt)
29
+
30
+ name = f"{uuid.uuid4().hex}.wav"
31
+ path = os.path.join(OUTPUT_DIR, name)
32
+ current.export(path, format="wav")
33
+ return 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
+ <link rel="stylesheet"
48
+ href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
49
+ <style>
50
+ body{
51
+ background:linear-gradient(135deg,#0f0c29,#302b63,#24243e);
52
+ color:#fff;font-family:Arial;margin:0
53
+ }
54
+ .box{
55
+ max-width:520px;margin:60px auto;padding:30px;
56
+ background:rgba(0,0,0,.4);border-radius:16px;text-align:center
57
+ }
58
+ .upload{
59
+ border:2px dashed #7fdbff;padding:20px;border-radius:14px;cursor:pointer
60
+ }
61
+ input{display:none}
62
+ button{
63
+ margin-top:20px;padding:12px 20px;border:none;border-radius:12px;
64
+ background:#7fdbff;color:#000;font-size:16px;cursor:pointer
65
+ }
66
+ audio{margin-top:20px;width:100%}
67
+ </style>
68
+ </head>
69
+ <body>
70
+ <div class="box">
71
+ <h1><i class="fa-solid fa-wave-square"></i> MONX AI</h1>
72
+ <p>Sube 2 a 4 canciones y crea un mix inteligente</p>
73
+
74
+ <label class="upload">
75
+ <i class="fa-solid fa-cloud-arrow-up fa-2x"></i><br><br>
76
+ Seleccionar canciones
77
+ <input type="file" id="files" multiple accept=".mp3,.wav">
78
+ </label>
79
+
80
+ <button onclick="send()">Crear mix</button>
81
+ <p id="status"></p>
82
+ <audio id="player" controls></audio>
83
+ </div>
84
+
85
+ <script>
86
+ async function send(){
87
+ const files=document.getElementById("files").files;
88
+ const status=document.getElementById("status");
89
+ const player=document.getElementById("player");
90
+
91
+ if(files.length<2||files.length>4){
92
+ status.innerText="⚠️ Sube entre 2 y 4 canciones";
93
+ return;
94
+ }
95
+
96
+ let fd=new FormData();
97
+ for(let f of files){fd.append("files",f)}
98
+
99
+ status.innerText="⏳ Procesando mix...";
100
+
101
+ const r=await fetch("/upload-mix",{method:"POST",body:fd});
102
+ const j=await r.json();
103
+
104
+ if(j.error){status.innerText="❌ "+j.error;return;}
105
+
106
+ status.innerText="✅ Mix listo";
107
+ player.src="/download?file="+j.file;
108
+ player.load();
109
+ }
110
+ </script>
111
+ </body>
112
+ </html>
113
+ """
114
+
115
+ # ======================
116
+ # API
117
+ # ======================
118
+
119
+ @app.post("/upload-mix")
120
+ async def upload_and_mix(files=File(...)):
121
+ if len(files) < 2 or len(files) > 4:
122
+ return JSONResponse({"error": "Sube entre 2 y 4 canciones"}, status_code=400)
123
+
124
+ paths = []
125
+
126
+ for f in files:
127
+ ext = f.filename.split(".")[-1].lower()
128
+ if ext not in ["mp3", "wav"]:
129
+ return JSONResponse({"error": "Formato no soportado"}, status_code=400)
130
+
131
+ name = f"{uuid.uuid4().hex}.{ext}"
132
+ path = os.path.join(UPLOAD_DIR, name)
133
+
134
+ with open(path, "wb") as out:
135
+ out.write(await f.read())
136
+
137
+ paths.append(path)
138
+
139
+ out = build_mix(paths)
140
+ return {"file": out}
141
+
142
+ @app.get("/download")
143
+ def download(file: str):
144
+ return FileResponse(
145
+ os.path.join(OUTPUT_DIR, file),
146
+ media_type="audio/wav",
147
+ filename="monx_ai_mix.wav"
148
+ )
149
+
150
+ # ======================
151
+ # HF SPACE ENTRYPOINT
152
+ # ======================
153
+
154
+ if __name__ == "__main__":
155
+ import uvicorn
156
+ uvicorn.run(app, host="0.0.0.0", port=7860)