Delete app.py
Browse files
app.py
DELETED
|
@@ -1,235 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
|
| 3 |
-
from pydub import AudioSegment
|
| 4 |
-
import os, uuid, threading, time
|
| 5 |
-
|
| 6 |
-
app = FastAPI(title="MONX AI")
|
| 7 |
-
|
| 8 |
-
UPLOAD_DIR = "uploads"
|
| 9 |
-
OUTPUT_DIR = "outputs"
|
| 10 |
-
CROSSFADE_MS = 20_000
|
| 11 |
-
|
| 12 |
-
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 13 |
-
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 14 |
-
|
| 15 |
-
# --------------------
|
| 16 |
-
# Estado de tareas
|
| 17 |
-
# --------------------
|
| 18 |
-
tasks = {}
|
| 19 |
-
|
| 20 |
-
# --------------------
|
| 21 |
-
# Audio engine
|
| 22 |
-
# --------------------
|
| 23 |
-
def process_mix(task_id, paths):
|
| 24 |
-
try:
|
| 25 |
-
tasks[task_id]["progress"] = 10
|
| 26 |
-
current = AudioSegment.from_file(paths[0])
|
| 27 |
-
|
| 28 |
-
step = 80 // (len(paths) - 1)
|
| 29 |
-
|
| 30 |
-
for i, p in enumerate(paths[1:], start=1):
|
| 31 |
-
next_track = AudioSegment.from_file(p)
|
| 32 |
-
current = current.append(next_track, crossfade=CROSSFADE_MS)
|
| 33 |
-
tasks[task_id]["progress"] = 10 + step * i
|
| 34 |
-
time.sleep(0.5) # da tiempo a mostrar progreso
|
| 35 |
-
|
| 36 |
-
name = f"{uuid.uuid4().hex}.wav"
|
| 37 |
-
out_path = os.path.join(OUTPUT_DIR, name)
|
| 38 |
-
current.export(out_path, format="wav")
|
| 39 |
-
|
| 40 |
-
tasks[task_id]["progress"] = 100
|
| 41 |
-
tasks[task_id]["file"] = name
|
| 42 |
-
except Exception as e:
|
| 43 |
-
tasks[task_id]["error"] = str(e)
|
| 44 |
-
|
| 45 |
-
# --------------------
|
| 46 |
-
# UI
|
| 47 |
-
# --------------------
|
| 48 |
-
@app.get("/", response_class=HTMLResponse)
|
| 49 |
-
def home():
|
| 50 |
-
return """
|
| 51 |
-
<!DOCTYPE html>
|
| 52 |
-
<html lang="es">
|
| 53 |
-
<head>
|
| 54 |
-
<meta charset="UTF-8">
|
| 55 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 56 |
-
<title>MONX AI</title>
|
| 57 |
-
<link rel="stylesheet"
|
| 58 |
-
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
|
| 59 |
-
<style>
|
| 60 |
-
body{
|
| 61 |
-
margin:0;
|
| 62 |
-
min-height:100vh;
|
| 63 |
-
background:linear-gradient(135deg,#0f0c29,#302b63,#24243e);
|
| 64 |
-
font-family:system-ui,Arial;
|
| 65 |
-
color:#fff;
|
| 66 |
-
display:flex;
|
| 67 |
-
justify-content:center;
|
| 68 |
-
align-items:center;
|
| 69 |
-
}
|
| 70 |
-
.card{
|
| 71 |
-
width:100%;
|
| 72 |
-
max-width:420px;
|
| 73 |
-
margin:16px;
|
| 74 |
-
background:rgba(0,0,0,.45);
|
| 75 |
-
border-radius:20px;
|
| 76 |
-
padding:24px;
|
| 77 |
-
text-align:center;
|
| 78 |
-
box-shadow:0 20px 40px rgba(0,0,0,.4);
|
| 79 |
-
}
|
| 80 |
-
h1{margin:0;font-size:28px}
|
| 81 |
-
p{opacity:.8;font-size:14px}
|
| 82 |
-
.upload{
|
| 83 |
-
margin-top:20px;
|
| 84 |
-
border:2px dashed #7fdbff;
|
| 85 |
-
border-radius:16px;
|
| 86 |
-
padding:22px;
|
| 87 |
-
cursor:pointer;
|
| 88 |
-
}
|
| 89 |
-
input{display:none}
|
| 90 |
-
button{
|
| 91 |
-
width:100%;
|
| 92 |
-
margin-top:18px;
|
| 93 |
-
padding:14px;
|
| 94 |
-
border:none;
|
| 95 |
-
border-radius:14px;
|
| 96 |
-
font-size:16px;
|
| 97 |
-
background:linear-gradient(135deg,#7fdbff,#9f7aea);
|
| 98 |
-
color:#000;
|
| 99 |
-
cursor:pointer;
|
| 100 |
-
}
|
| 101 |
-
.progress{
|
| 102 |
-
margin-top:18px;
|
| 103 |
-
height:10px;
|
| 104 |
-
background:#222;
|
| 105 |
-
border-radius:6px;
|
| 106 |
-
overflow:hidden;
|
| 107 |
-
display:none;
|
| 108 |
-
}
|
| 109 |
-
.progress-bar{
|
| 110 |
-
height:100%;
|
| 111 |
-
width:0%;
|
| 112 |
-
background:linear-gradient(90deg,#7fdbff,#9f7aea);
|
| 113 |
-
transition:width .3s;
|
| 114 |
-
}
|
| 115 |
-
audio{margin-top:18px;width:100%}
|
| 116 |
-
.status{margin-top:10px;font-size:14px}
|
| 117 |
-
</style>
|
| 118 |
-
</head>
|
| 119 |
-
|
| 120 |
-
<body>
|
| 121 |
-
<div class="card">
|
| 122 |
-
<h1>🎵 MONX AI</h1>
|
| 123 |
-
<p>Fusión de audio inteligente con crossfade</p>
|
| 124 |
-
|
| 125 |
-
<label class="upload">
|
| 126 |
-
<i class="fa-solid fa-cloud-arrow-up fa-2x"></i><br><br>
|
| 127 |
-
Selecciona 2 a 4 canciones
|
| 128 |
-
<input id="files" type="file" multiple accept=".mp3,.wav,.flac,.m4a">
|
| 129 |
-
</label>
|
| 130 |
-
|
| 131 |
-
<button onclick="start()">✨ Crear Mix</button>
|
| 132 |
-
|
| 133 |
-
<div class="progress" id="progressBox">
|
| 134 |
-
<div class="progress-bar" id="bar"></div>
|
| 135 |
-
</div>
|
| 136 |
-
|
| 137 |
-
<div class="status" id="status"></div>
|
| 138 |
-
<audio id="player" controls></audio>
|
| 139 |
-
</div>
|
| 140 |
-
|
| 141 |
-
<script>
|
| 142 |
-
let taskId=null;
|
| 143 |
-
|
| 144 |
-
async function start(){
|
| 145 |
-
const files=document.getElementById("files").files;
|
| 146 |
-
const status=document.getElementById("status");
|
| 147 |
-
|
| 148 |
-
if(files.length<2||files.length>4){
|
| 149 |
-
status.innerText="⚠️ Sube entre 2 y 4 canciones";
|
| 150 |
-
return;
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
let fd=new FormData();
|
| 154 |
-
for(let f of files){fd.append("files",f)}
|
| 155 |
-
|
| 156 |
-
status.innerText="Subiendo archivos...";
|
| 157 |
-
document.getElementById("progressBox").style.display="block";
|
| 158 |
-
|
| 159 |
-
const r=await fetch("/upload",{method:"POST",body:fd});
|
| 160 |
-
const j=await r.json();
|
| 161 |
-
|
| 162 |
-
if(j.error){status.innerText="❌ "+j.error;return;}
|
| 163 |
-
|
| 164 |
-
taskId=j.task_id;
|
| 165 |
-
poll();
|
| 166 |
-
}
|
| 167 |
-
|
| 168 |
-
async function poll(){
|
| 169 |
-
const r=await fetch("/progress/"+taskId);
|
| 170 |
-
const j=await r.json();
|
| 171 |
-
|
| 172 |
-
document.getElementById("bar").style.width=j.progress+"%";
|
| 173 |
-
document.getElementById("status").innerText="Procesando: "+j.progress+"%";
|
| 174 |
-
|
| 175 |
-
if(j.progress<100){
|
| 176 |
-
setTimeout(poll,800);
|
| 177 |
-
}else{
|
| 178 |
-
document.getElementById("status").innerText="✅ Mix listo";
|
| 179 |
-
document.getElementById("player").src="/download?file="+j.file;
|
| 180 |
-
}
|
| 181 |
-
}
|
| 182 |
-
</script>
|
| 183 |
-
</body>
|
| 184 |
-
</html>
|
| 185 |
-
"""
|
| 186 |
-
|
| 187 |
-
# --------------------
|
| 188 |
-
# API
|
| 189 |
-
# --------------------
|
| 190 |
-
@app.post("/upload")
|
| 191 |
-
async def upload(files=File(...)):
|
| 192 |
-
if len(files) < 2 or len(files) > 4:
|
| 193 |
-
return JSONResponse({"error": "Sube entre 2 y 4 canciones"}, 400)
|
| 194 |
-
|
| 195 |
-
paths=[]
|
| 196 |
-
for f in files:
|
| 197 |
-
ext=f.filename.split(".")[-1].lower()
|
| 198 |
-
if ext not in ["mp3","wav","flac","m4a"]:
|
| 199 |
-
return JSONResponse({"error":"Formato no soportado"},400)
|
| 200 |
-
|
| 201 |
-
name=f"{uuid.uuid4().hex}.{ext}"
|
| 202 |
-
path=os.path.join(UPLOAD_DIR,name)
|
| 203 |
-
with open(path,"wb") as out:
|
| 204 |
-
out.write(await f.read())
|
| 205 |
-
paths.append(path)
|
| 206 |
-
|
| 207 |
-
task_id=uuid.uuid4().hex
|
| 208 |
-
tasks[task_id]={"progress":0}
|
| 209 |
-
|
| 210 |
-
threading.Thread(
|
| 211 |
-
target=process_mix,
|
| 212 |
-
args=(task_id,paths),
|
| 213 |
-
daemon=True
|
| 214 |
-
).start()
|
| 215 |
-
|
| 216 |
-
return {"task_id":task_id}
|
| 217 |
-
|
| 218 |
-
@app.get("/progress/{task_id}")
|
| 219 |
-
def progress(task_id:str):
|
| 220 |
-
return tasks.get(task_id,{"progress":0})
|
| 221 |
-
|
| 222 |
-
@app.get("/download")
|
| 223 |
-
def download(file:str):
|
| 224 |
-
return FileResponse(
|
| 225 |
-
os.path.join(OUTPUT_DIR,file),
|
| 226 |
-
media_type="audio/wav",
|
| 227 |
-
filename="monx_ai_mix.wav"
|
| 228 |
-
)
|
| 229 |
-
|
| 230 |
-
# --------------------
|
| 231 |
-
# HF entrypoint
|
| 232 |
-
# --------------------
|
| 233 |
-
if __name__=="__main__":
|
| 234 |
-
import uvicorn
|
| 235 |
-
uvicorn.run(app,host="0.0.0.0",port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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