Spaces:
Sleeping
Sleeping
Download main.py from veltrixcode/lofi: direct link, hf CLI and curl.
- Browser
- Download file 6.85 kB
-
https://huggingface.co/spaces/veltrixcode/lofi/resolve/main/main.py
- Command line
-
hf download hf://spaces/veltrixcode/lofi/main.py
-
curl -L -o main.py https://huggingface.co/spaces/veltrixcode/lofi/resolve/main/main.py
6.85 kB
| from fastapi import FastAPI, UploadFile, File, Form, HTTPException, BackgroundTasks | |
| from fastapi.responses import HTMLResponse, FileResponse | |
| import subprocess, uuid, os, shutil, requests | |
| app = FastAPI() | |
| TMP = "/tmp/audio" | |
| os.makedirs(TMP, exist_ok=True) | |
| # 0.85 speed + 0.85 pitch via asetrate, then 40% reverb mix via aecho | |
| # aecho: in=1.0, out=1.0, delay=60ms (short room tail), decay=0.4 (40% wet) | |
| FILTER = ( | |
| "asetrate=44100*0.85," # slow to 85% + lower pitch to 85% | |
| "aresample=44100," # restore clock rate for players | |
| "aecho=1.0:1.0:60:0.4" # 40% reverb — 60ms tail, 0.4 decay = 40% wet | |
| ) | |
| HTML = """<!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Slowed + Reverb</title> | |
| <style> | |
| @import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&display=swap'); | |
| *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } | |
| body { | |
| background: #0a0a0f; | |
| color: #e8e8f0; | |
| font-family: 'Space Mono', monospace; | |
| min-height: 100vh; | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| justify-content: center; | |
| padding: 40px 20px; | |
| background-image: | |
| radial-gradient(ellipse 80% 50% at 50% -10%, rgba(124,109,250,0.15) 0%, transparent 60%), | |
| radial-gradient(ellipse 60% 40% at 80% 100%, rgba(250,109,196,0.08) 0%, transparent 60%); | |
| } | |
| h1 { | |
| font-size: clamp(1.6rem, 5vw, 2.4rem); | |
| font-weight: 700; | |
| letter-spacing: -0.02em; | |
| margin-bottom: 8px; | |
| background: linear-gradient(135deg, #7c6dfa, #fa6dc4); | |
| -webkit-background-clip: text; | |
| -webkit-text-fill-color: transparent; | |
| background-clip: text; | |
| } | |
| .subtitle { | |
| font-size: 0.7rem; | |
| color: #555570; | |
| letter-spacing: 0.14em; | |
| text-transform: uppercase; | |
| margin-bottom: 48px; | |
| } | |
| .card { | |
| background: #13131a; | |
| border: 1px solid #2a2a3a; | |
| border-radius: 16px; | |
| padding: 32px; | |
| width: 100%; | |
| max-width: 480px; | |
| margin-bottom: 16px; | |
| } | |
| .card-label { | |
| font-size: 0.62rem; | |
| letter-spacing: 0.15em; | |
| text-transform: uppercase; | |
| color: #555570; | |
| margin-bottom: 20px; | |
| } | |
| .file-drop { | |
| border: 2px dashed #2a2a3a; | |
| border-radius: 10px; | |
| padding: 28px 20px; | |
| text-align: center; | |
| cursor: pointer; | |
| transition: border-color 0.2s, background 0.2s; | |
| margin-bottom: 16px; | |
| position: relative; | |
| } | |
| .file-drop:hover { border-color: #7c6dfa; background: rgba(124,109,250,0.04); } | |
| .file-drop input[type=file] { | |
| position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; | |
| } | |
| .file-drop .icon { font-size: 2rem; margin-bottom: 8px; } | |
| .file-drop .hint { font-size: 0.72rem; color: #555570; } | |
| .file-drop .filename { font-size: 0.78rem; color: #7c6dfa; margin-top: 6px; min-height: 18px; } | |
| input[type=text] { | |
| width: 100%; | |
| background: #0d0d14; | |
| border: 1px solid #2a2a3a; | |
| border-radius: 8px; | |
| color: #e8e8f0; | |
| font-family: inherit; | |
| font-size: 0.78rem; | |
| padding: 12px 14px; | |
| outline: none; | |
| transition: border-color 0.2s; | |
| margin-bottom: 16px; | |
| } | |
| input[type=text]:focus { border-color: #7c6dfa; } | |
| input[type=text]::placeholder { color: #33334a; } | |
| button[type=submit] { | |
| width: 100%; | |
| background: linear-gradient(135deg, #7c6dfa, #fa6dc4); | |
| border: none; | |
| border-radius: 8px; | |
| color: #fff; | |
| font-family: inherit; | |
| font-size: 0.78rem; | |
| font-weight: 700; | |
| letter-spacing: 0.08em; | |
| padding: 13px; | |
| cursor: pointer; | |
| transition: opacity 0.2s, transform 0.1s; | |
| } | |
| button[type=submit]:hover { opacity: 0.88; } | |
| button[type=submit]:active { transform: scale(0.98); } | |
| .info { | |
| font-size: 0.62rem; | |
| color: #33334a; | |
| text-align: center; | |
| margin-top: 20px; | |
| letter-spacing: 0.08em; | |
| line-height: 2; | |
| } | |
| .info span { color: #7c6dfa; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>⟁ SLOWED + REVERB</h1> | |
| <p class="subtitle">0.85x speed · 0.85x pitch · 40% reverb</p> | |
| <div class="card"> | |
| <div class="card-label">// Upload audio file</div> | |
| <form action="/slowed-reverb" method="post" enctype="multipart/form-data"> | |
| <div class="file-drop"> | |
| <input type="file" name="file" accept="audio/*" | |
| onchange="this.parentElement.querySelector('.filename').textContent=this.files[0]?.name||''"> | |
| <div class="icon">🎵</div> | |
| <div class="hint">mp3 · wav · flac · ogg · m4a</div> | |
| <div class="filename"></div> | |
| </div> | |
| <button type="submit">Process File →</button> | |
| </form> | |
| </div> | |
| <div class="card"> | |
| <div class="card-label">// Or paste direct audio URL</div> | |
| <form action="/slowed-reverb-url" method="post"> | |
| <input type="text" name="url" placeholder="https://example.com/track.mp3"> | |
| <button type="submit">Process URL →</button> | |
| </form> | |
| </div> | |
| <p class="info">output: <span>AAC 256kbps M4A</span></p> | |
| </body> | |
| </html>""" | |
| def run_ffmpeg(inp: str, out: str): | |
| cmd = [ | |
| "ffmpeg", "-y", "-i", inp, | |
| "-af", FILTER, | |
| "-c:a", "aac", | |
| "-b:a", "256k", | |
| "-ar", "44100", | |
| "-movflags", "+faststart", | |
| out | |
| ] | |
| result = subprocess.run(cmd, capture_output=True, text=True) | |
| if result.returncode != 0: | |
| raise HTTPException(status_code=500, detail=f"ffmpeg error:\n{result.stderr[-3000:]}") | |
| def cleanup(*paths: str): | |
| for p in paths: | |
| try: | |
| os.remove(p) | |
| except FileNotFoundError: | |
| pass | |
| def home(): | |
| return HTML | |
| async def slowed_reverb(background_tasks: BackgroundTasks, file: UploadFile = File(...)): | |
| ext = os.path.splitext(file.filename)[1] or ".mp3" | |
| uid = uuid.uuid4() | |
| inp = os.path.join(TMP, f"{uid}_in{ext}") | |
| out = os.path.join(TMP, f"{uid}_out.m4a") | |
| with open(inp, "wb") as f: | |
| shutil.copyfileobj(file.file, f) | |
| run_ffmpeg(inp, out) | |
| background_tasks.add_task(cleanup, inp, out) | |
| return FileResponse(out, media_type="audio/mp4", filename="slowed_reverb.m4a") | |
| def slowed_reverb_url(background_tasks: BackgroundTasks, url: str = Form(...)): | |
| ext = os.path.splitext(url.split("?")[0])[1] or ".mp3" | |
| uid = uuid.uuid4() | |
| inp = os.path.join(TMP, f"{uid}_in{ext}") | |
| out = os.path.join(TMP, f"{uid}_out.m4a") | |
| try: | |
| r = requests.get(url, stream=True, timeout=30) | |
| r.raise_for_status() | |
| except requests.RequestException as e: | |
| raise HTTPException(status_code=400, detail=f"Failed to fetch URL: {e}") | |
| with open(inp, "wb") as f: | |
| for chunk in r.iter_content(chunk_size=65536): | |
| f.write(chunk) | |
| run_ffmpeg(inp, out) | |
| background_tasks.add_task(cleanup, inp, out) | |
| return FileResponse(out, media_type="audio/mp4", filename="slowed_reverb.m4a") |