Spaces:
Runtime error
Runtime error
nifty-coder commited on
Commit ·
57785f8
1
Parent(s): 1b9b8c1
Cleaned up code
Browse files
main.py
CHANGED
|
@@ -4,10 +4,8 @@ import shutil
|
|
| 4 |
import subprocess
|
| 5 |
import logging
|
| 6 |
import zipfile
|
| 7 |
-
import re
|
| 8 |
import json
|
| 9 |
import hashlib
|
| 10 |
-
import io
|
| 11 |
import uuid
|
| 12 |
import requests
|
| 13 |
import secrets
|
|
@@ -36,12 +34,13 @@ app = FastAPI()
|
|
| 36 |
|
| 37 |
# Configure CORS
|
| 38 |
default_origins = [
|
| 39 |
-
"http://localhost:5173",
|
| 40 |
"http://localhost:8080",
|
| 41 |
-
"http://localhost:3000",
|
| 42 |
-
"http://localhost:8000",
|
| 43 |
"https://aimusicapp-a2b65.web.app",
|
| 44 |
"https://aimusicapp-a2b65.firebaseapp.com",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
]
|
| 46 |
allowed_origins_env = os.environ.get('ALLOWED_ORIGINS')
|
| 47 |
allowed_origins = [o.strip() for o in allowed_origins_env.split(',') if o.strip()] if allowed_origins_env else default_origins
|
|
@@ -64,23 +63,18 @@ def options_handler(request: Request):
|
|
| 64 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 65 |
})
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
@app.get("/")
|
| 70 |
def health_check():
|
| 71 |
-
return {"status": "healthy", "message": "StemSplit Backend is running
|
| 72 |
|
| 73 |
# --- Utility Functions ---
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
def find_ffmpeg() -> str:
|
| 78 |
common_paths = ["/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg", "/usr/bin/ffmpeg"]
|
| 79 |
for path in common_paths:
|
| 80 |
if os.path.exists(path) and os.access(path, os.X_OK):
|
| 81 |
return path
|
| 82 |
|
| 83 |
-
# Check current directory
|
| 84 |
local_binary = os.path.join(os.getcwd(), "ffmpeg")
|
| 85 |
if os.path.exists(local_binary):
|
| 86 |
return local_binary
|
|
@@ -206,7 +200,7 @@ def check_storage_limit():
|
|
| 206 |
|
| 207 |
# --- SMTP2GO & Activation Configuration ---
|
| 208 |
SMTP2GO_API_KEY = os.environ.get('SMTP2GO_API_KEY')
|
| 209 |
-
SMTP2GO_SENDER = os.environ.get('SMTP2GO_SENDER', '
|
| 210 |
ACTIVATIONS_FILE = os.path.join(CACHE_DIR, "activations.json")
|
| 211 |
|
| 212 |
def load_activations() -> dict:
|
|
@@ -445,7 +439,7 @@ async def signup(request: SignupRequest, req: Request, background_tasks: Backgro
|
|
| 445 |
origin = req.headers.get('origin')
|
| 446 |
if not origin or origin not in allowed_origins:
|
| 447 |
# Fallback to the first allowed origin if not provided or invalid (for safety)
|
| 448 |
-
origin = allowed_origins[0] if allowed_origins else "http://localhost:
|
| 449 |
|
| 450 |
try:
|
| 451 |
# Create user in Firebase with email_verified=False
|
|
@@ -559,7 +553,7 @@ def run_demucs_and_extract(input_path: str, tmpdir: str, requested_stems: list =
|
|
| 559 |
output_dir = os.path.join(tmpdir, "stems")
|
| 560 |
os.makedirs(output_dir, exist_ok=True)
|
| 561 |
|
| 562 |
-
cmd = DEMUCS_CMD +["-o", output_dir, input_path]
|
| 563 |
# Run demucs (suppress verbose output)
|
| 564 |
try:
|
| 565 |
subprocess.run(cmd, check=True, capture_output=True, text=True)
|
|
@@ -623,8 +617,6 @@ def run_demucs_and_extract(input_path: str, tmpdir: str, requested_stems: list =
|
|
| 623 |
|
| 624 |
return mp3_dir
|
| 625 |
|
| 626 |
-
|
| 627 |
-
|
| 628 |
@app.post("/upload")
|
| 629 |
async def process_upload(
|
| 630 |
file: UploadFile = File(...),
|
|
@@ -789,11 +781,8 @@ def get_stem(key: str, filename: str):
|
|
| 789 |
|
| 790 |
try:
|
| 791 |
with zipfile.ZipFile(zip_path, 'r') as zf:
|
| 792 |
-
# zipfile names usually match the relative path in the archive
|
| 793 |
-
# if we have subdirs in the zip, we might need to search
|
| 794 |
target = filename
|
| 795 |
if target not in zf.namelist():
|
| 796 |
-
# try to find by basename
|
| 797 |
found = None
|
| 798 |
for name in zf.namelist():
|
| 799 |
if os.path.basename(name) == filename:
|
|
@@ -809,9 +798,7 @@ def get_stem(key: str, filename: str):
|
|
| 809 |
logger.error(f"Error extracting stem: {e}")
|
| 810 |
raise HTTPException(status_code=500, detail="Failed to extract stem from archive")
|
| 811 |
|
| 812 |
-
|
| 813 |
# --- R2 Presigned URL Endpoints ---
|
| 814 |
-
|
| 815 |
class PresignedUrlRequest(BaseModel):
|
| 816 |
song_id: str
|
| 817 |
filename: str
|
|
@@ -850,8 +837,6 @@ async def get_presigned_url(
|
|
| 850 |
raise HTTPException(status_code=404, detail="File not found")
|
| 851 |
|
| 852 |
try:
|
| 853 |
-
# Generate presigned URL (1 hour expiry)
|
| 854 |
-
# Use ResponseContentDisposition to force a direct download when the URL is accessed
|
| 855 |
presigned_url = s3_client.generate_presigned_url(
|
| 856 |
'get_object',
|
| 857 |
Params={
|
|
@@ -864,7 +849,7 @@ async def get_presigned_url(
|
|
| 864 |
|
| 865 |
return {
|
| 866 |
"url": presigned_url,
|
| 867 |
-
"expires_in": 3600,
|
| 868 |
"filename": request.filename
|
| 869 |
}
|
| 870 |
except Exception as e:
|
|
|
|
| 4 |
import subprocess
|
| 5 |
import logging
|
| 6 |
import zipfile
|
|
|
|
| 7 |
import json
|
| 8 |
import hashlib
|
|
|
|
| 9 |
import uuid
|
| 10 |
import requests
|
| 11 |
import secrets
|
|
|
|
| 34 |
|
| 35 |
# Configure CORS
|
| 36 |
default_origins = [
|
|
|
|
| 37 |
"http://localhost:8080",
|
|
|
|
|
|
|
| 38 |
"https://aimusicapp-a2b65.web.app",
|
| 39 |
"https://aimusicapp-a2b65.firebaseapp.com",
|
| 40 |
+
# "https://stemsplit.ai",
|
| 41 |
+
# "https://www.stemsplit.ai",
|
| 42 |
+
# "stemsplit.ai",
|
| 43 |
+
# "www.stemsplit.ai"
|
| 44 |
]
|
| 45 |
allowed_origins_env = os.environ.get('ALLOWED_ORIGINS')
|
| 46 |
allowed_origins = [o.strip() for o in allowed_origins_env.split(',') if o.strip()] if allowed_origins_env else default_origins
|
|
|
|
| 63 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 64 |
})
|
| 65 |
|
|
|
|
|
|
|
| 66 |
@app.get("/")
|
| 67 |
def health_check():
|
| 68 |
+
return {"status": "healthy", "message": "StemSplit Backend is running!"}
|
| 69 |
|
| 70 |
# --- Utility Functions ---
|
|
|
|
|
|
|
|
|
|
| 71 |
def find_ffmpeg() -> str:
|
| 72 |
common_paths = ["/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg", "/usr/bin/ffmpeg"]
|
| 73 |
for path in common_paths:
|
| 74 |
if os.path.exists(path) and os.access(path, os.X_OK):
|
| 75 |
return path
|
| 76 |
|
| 77 |
+
# Check current directory
|
| 78 |
local_binary = os.path.join(os.getcwd(), "ffmpeg")
|
| 79 |
if os.path.exists(local_binary):
|
| 80 |
return local_binary
|
|
|
|
| 200 |
|
| 201 |
# --- SMTP2GO & Activation Configuration ---
|
| 202 |
SMTP2GO_API_KEY = os.environ.get('SMTP2GO_API_KEY')
|
| 203 |
+
SMTP2GO_SENDER = os.environ.get('SMTP2GO_SENDER', 'contact@niftysoftsol.com')
|
| 204 |
ACTIVATIONS_FILE = os.path.join(CACHE_DIR, "activations.json")
|
| 205 |
|
| 206 |
def load_activations() -> dict:
|
|
|
|
| 439 |
origin = req.headers.get('origin')
|
| 440 |
if not origin or origin not in allowed_origins:
|
| 441 |
# Fallback to the first allowed origin if not provided or invalid (for safety)
|
| 442 |
+
origin = allowed_origins[0] if allowed_origins else "http://localhost:8080"
|
| 443 |
|
| 444 |
try:
|
| 445 |
# Create user in Firebase with email_verified=False
|
|
|
|
| 553 |
output_dir = os.path.join(tmpdir, "stems")
|
| 554 |
os.makedirs(output_dir, exist_ok=True)
|
| 555 |
|
| 556 |
+
cmd = DEMUCS_CMD + ["-o", output_dir, input_path]
|
| 557 |
# Run demucs (suppress verbose output)
|
| 558 |
try:
|
| 559 |
subprocess.run(cmd, check=True, capture_output=True, text=True)
|
|
|
|
| 617 |
|
| 618 |
return mp3_dir
|
| 619 |
|
|
|
|
|
|
|
| 620 |
@app.post("/upload")
|
| 621 |
async def process_upload(
|
| 622 |
file: UploadFile = File(...),
|
|
|
|
| 781 |
|
| 782 |
try:
|
| 783 |
with zipfile.ZipFile(zip_path, 'r') as zf:
|
|
|
|
|
|
|
| 784 |
target = filename
|
| 785 |
if target not in zf.namelist():
|
|
|
|
| 786 |
found = None
|
| 787 |
for name in zf.namelist():
|
| 788 |
if os.path.basename(name) == filename:
|
|
|
|
| 798 |
logger.error(f"Error extracting stem: {e}")
|
| 799 |
raise HTTPException(status_code=500, detail="Failed to extract stem from archive")
|
| 800 |
|
|
|
|
| 801 |
# --- R2 Presigned URL Endpoints ---
|
|
|
|
| 802 |
class PresignedUrlRequest(BaseModel):
|
| 803 |
song_id: str
|
| 804 |
filename: str
|
|
|
|
| 837 |
raise HTTPException(status_code=404, detail="File not found")
|
| 838 |
|
| 839 |
try:
|
|
|
|
|
|
|
| 840 |
presigned_url = s3_client.generate_presigned_url(
|
| 841 |
'get_object',
|
| 842 |
Params={
|
|
|
|
| 849 |
|
| 850 |
return {
|
| 851 |
"url": presigned_url,
|
| 852 |
+
"expires_in": 3600, # 1 hour
|
| 853 |
"filename": request.filename
|
| 854 |
}
|
| 855 |
except Exception as e:
|