Spaces:
Running
Running
File size: 17,071 Bytes
d90b8a8 62743d3 d90b8a8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | """
server.py โ HarmoSplit ใใใฏใจใณใ
Hugging Face Spaces ๅฏพๅฟ๏ผใใผใ 7860๏ผ+ Stripe ๆ้กๆฑบๆธ
"""
import os
import sys
import uuid
import json
import shutil
import secrets
import tempfile
import threading
import subprocess
from pathlib import Path
from datetime import datetime, timezone
from flask import (
Flask, request, jsonify, send_file,
Response, send_from_directory, redirect, url_for
)
from flask_cors import CORS
sys.path.insert(0, str(Path(__file__).parent))
import app as core
# โโ Flask โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
flask_app = Flask(__name__, static_folder="static", static_url_path="")
CORS(flask_app)
# โโ ่จญๅฎ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
PORT = int(os.environ.get("PORT", 7860))
# Stripe ใญใผ๏ผHF Spaces Secrets ใพใใฏ .env ใง่จญๅฎ๏ผ
STRIPE_SECRET_KEY = os.environ.get("STRIPE_SECRET_KEY", "")
STRIPE_WEBHOOK_SECRET = os.environ.get("STRIPE_WEBHOOK_SECRET", "")
STRIPE_PRICE_ID = os.environ.get("STRIPE_PRICE_ID", "")
# ใขใใชใฎๅ
ฌ้ URL๏ผWebhook / Checkout success URL ็จ๏ผ
APP_URL = os.environ.get("APP_URL", f"http://localhost:{PORT}")
# ๆฑบๆธไธ่ฆใขใผใ๏ผStripe ใญใผๆช่จญๅฎใชใ็กๆ้ๆพ๏ผ
FREE_MODE = not bool(STRIPE_SECRET_KEY)
# โโ ๆฐธ็ถในใใฌใผใธ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# HF Spaces ใงใฏ /data ใๆฐธ็ถใใญใผใซใซใงใฏ ./data ใไฝฟ็จใ
DATA_DIR = Path("/data") if Path("/data").exists() else Path("./data")
DATA_DIR.mkdir(parents=True, exist_ok=True)
TOKENS_FILE = DATA_DIR / "tokens.json"
UPLOAD_DIR = DATA_DIR / "uploads"
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
# โโ ใใผใฏใณ็ฎก็ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
tokens_lock = threading.Lock()
def load_tokens() -> dict:
with tokens_lock:
if TOKENS_FILE.exists():
try:
return json.loads(TOKENS_FILE.read_text("utf-8"))
except Exception:
pass
return {}
def save_tokens(data: dict):
with tokens_lock:
TOKENS_FILE.write_text(json.dumps(data, indent=2, ensure_ascii=False), "utf-8")
def create_token(customer_id: str, subscription_id: str, email: str) -> str:
token = secrets.token_urlsafe(32)
data = load_tokens()
data[token] = {
"customer_id": customer_id,
"subscription_id": subscription_id,
"email": email,
"created_at": datetime.now(timezone.utc).isoformat(),
"active": True,
}
save_tokens(data)
return token
def is_token_valid(token: str) -> bool:
if FREE_MODE:
return True # ็กๆใขใผใใฏๅธธใซๆๅน
data = load_tokens()
entry = data.get(token)
return bool(entry and entry.get("active"))
def deactivate_token_by_subscription(subscription_id: str):
data = load_tokens()
for info in data.values():
if info.get("subscription_id") == subscription_id:
info["active"] = False
save_tokens(data)
# โโ ใธใงใ็ฎก็ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
JOBS: dict[str, dict] = {}
JOBS_LOCK = threading.Lock()
def log_progress(job_id: str, message: str, percent: int | None = None):
entry = {"msg": message}
if percent is not None:
entry["pct"] = percent
with JOBS_LOCK:
if job_id in JOBS:
JOBS[job_id]["progress"].append(entry)
def _load_stems_helper(demucs_out: Path, model: str, wav_path: Path):
import soundfile as sf
import numpy as np
stem_dir = demucs_out / model / wav_path.stem
if not stem_dir.exists():
candidates = list(demucs_out.rglob("*.wav"))
if not candidates:
raise FileNotFoundError("ในใใ ใใกใคใซใ่ฆใคใใใพใใ")
stem_dir = candidates[0].parent
stems = {}
sr = 44100
for wav_file in sorted(stem_dir.glob("*.wav")):
data, sr = sf.read(str(wav_file), always_2d=True)
stems[wav_file.stem] = data.astype(np.float_())
return stems, sr
core._load_stems = _load_stems_helper
def process_job(job_id: str, input_path: Path, inst_vol: float, model: str, use_mdx: bool):
tmp_dir = Path(tempfile.mkdtemp(prefix=f"hmsplit_{job_id[:8]}_"))
demucs_out = tmp_dir / "demucs_out"
demucs_out.mkdir(parents=True, exist_ok=True)
try:
with JOBS_LOCK:
JOBS[job_id]["status"] = "processing"
log_progress(job_id, "๐ต ้ณๅฃฐใ่ชญใฟ่พผใฟไธญ...", 5)
wav_path = core.prepare_audio(input_path, tmp_dir)
log_progress(job_id, "๐ค Demucs AI ใง้ณๆบใๅ้ขไธญ...", 15)
cmd = [sys.executable, "-m", "demucs", "-n", model,
"-o", str(demucs_out), str(wav_path)]
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, encoding="utf-8", errors="replace"
)
for line in proc.stdout:
line = line.rstrip()
if line.strip():
log_progress(job_id, line)
proc.wait()
if proc.returncode != 0:
raise RuntimeError("Demucs ๅฆ็ใซๅคฑๆใใพใใ")
log_progress(job_id, "๐ ในใใ ใ่ชญใฟ่พผใฟไธญ...", 55)
stems, sr = core._load_stems(demucs_out, model, wav_path)
if model == "htdemucs_6s" and "vocals" not in stems:
log_progress(job_id, "โ ๏ธ htdemucs ใซใใฉใผใซใใใฏ", 57)
shutil.rmtree(demucs_out); demucs_out.mkdir(parents=True, exist_ok=True)
model = "htdemucs"
subprocess.run([sys.executable, "-m", "demucs", "-n", model,
"-o", str(demucs_out), str(wav_path)], check=True, capture_output=True)
stems, sr = core._load_stems(demucs_out, model, wav_path)
mdx_model_path = None
if use_mdx:
log_progress(job_id, "๐ง UVR MDX-NET ใขใใซใๆบๅไธญ...", 60)
try:
mdx_cache = DATA_DIR / "models"
mdx_cache.mkdir(parents=True, exist_ok=True)
mdx_model_path = core.download_mdx_model(mdx_cache)
log_progress(job_id, "โ
UVR MDX-NET ๆบๅๅฎไบ", 65)
except Exception as e:
log_progress(job_id, f"โ ๏ธ MDX ๅๅพๅคฑๆใMid/Side ใซใใฉใผใซใใใฏ: {e}", 65)
log_progress(job_id, "๐๏ธ L/R ใใณใใณใฐ & ใใใฏในๅฆ็ไธญ...", 68)
if mdx_model_path:
log_progress(job_id, "๐ฌ ใชใผใ / ใใใญใณใฐ AI ๅ้ขไธญ... (ๆฐๅใใใใพใ)", 70)
mixed = core.mix_stems(stems, model, inst_vol, mdx_model_path=mdx_model_path, sr=sr)
log_progress(job_id, "๐พ WAV ใๆธใๅบใไธญ...", 95)
import soundfile as sf
output_path = UPLOAD_DIR / f"{job_id}_panned.wav"
sf.write(str(output_path), mixed, sr, subtype="PCM_16")
with JOBS_LOCK:
JOBS[job_id]["status"] = "done"
JOBS[job_id]["output_path"] = str(output_path)
log_progress(job_id, "โ
ๅฆ็ๅฎไบ๏ผใใฆใณใญใผใใใฟใณใใฏใชใใฏใใฆใใ ใใใ", 100)
except Exception as e:
import traceback
with JOBS_LOCK:
JOBS[job_id]["status"] = "error"
JOBS[job_id]["error"] = str(e)
log_progress(job_id, f"โ {e}")
finally:
shutil.rmtree(tmp_dir, ignore_errors=True)
try:
input_path.unlink(missing_ok=True)
except Exception:
pass
# โโ ใซใผใใฃใณใฐ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/")
def index():
return send_from_directory("static", "index.html")
@flask_app.route("/pricing")
def pricing():
return send_from_directory("static", "pricing.html")
@flask_app.route("/success")
def success():
return send_from_directory("static", "success.html")
@flask_app.route("/legal")
def legal():
return send_from_directory("static", "legal.html")
@flask_app.route("/auth-mode")
def auth_mode():
"""ใใญใณใใจใณใใ็กๆ/ๆๆใขใผใใ่ญๅฅใใใใใฎใจใณใใใคใณใ"""
return jsonify({"free_mode": FREE_MODE})
@flask_app.route("/pricing-info")
def pricing_info():
"""ๆ้ใใผใธ็จ: Stripe ใใ Price ๆ
ๅ ฑใๅๅพใใฆ่ฟใ"""
if FREE_MODE:
return jsonify({"price": 0, "currency": "jpy", "free_mode": True})
try:
import stripe
stripe.api_key = STRIPE_SECRET_KEY
price = stripe.Price.retrieve(STRIPE_PRICE_ID)
return jsonify({
"price": price.unit_amount,
"currency": price.currency,
"interval": price.recurring.interval if price.recurring else "month",
})
except Exception as e:
return jsonify({"error": str(e)}), 500
# โโ Stripe: Checkout ใปใใทใงใณไฝๆ โโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/create-checkout", methods=["POST"])
def create_checkout():
if FREE_MODE:
return jsonify({"error": "Stripe ๆช่จญๅฎ๏ผ้็บใขใผใ๏ผ"}), 400
try:
import stripe
stripe.api_key = STRIPE_SECRET_KEY
session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=[{"price": STRIPE_PRICE_ID, "quantity": 1}],
mode="subscription",
success_url=f"{APP_URL}/success?session_id={{CHECKOUT_SESSION_ID}}",
cancel_url=f"{APP_URL}/pricing",
)
return jsonify({"url": session.url})
except Exception as e:
return jsonify({"error": str(e)}), 500
# โโ Stripe: Webhook โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/webhook", methods=["POST"])
def stripe_webhook():
if FREE_MODE:
return "", 200
try:
import stripe
stripe.api_key = STRIPE_SECRET_KEY
payload = request.get_data()
sig_header = request.headers.get("Stripe-Signature", "")
event = stripe.Webhook.construct_event(payload, sig_header, STRIPE_WEBHOOK_SECRET)
except Exception as e:
return jsonify({"error": str(e)}), 400
etype = event["type"]
obj = event["data"]["object"]
if etype == "checkout.session.completed":
sub_id = obj.get("subscription")
cust_id = obj.get("customer")
email = obj.get("customer_email") or obj.get("customer_details", {}).get("email", "")
token = create_token(cust_id, sub_id, email)
print(f"[WEBHOOK] ๆฐใตใในใฏใชใใทใงใณ: {email} โ token={token[:8]}...")
elif etype in ("customer.subscription.deleted", "customer.subscription.paused"):
sub_id = obj.get("id")
deactivate_token_by_subscription(sub_id)
print(f"[WEBHOOK] ใตใในใฏใชใใทใงใณๅๆญข: {sub_id}")
return "", 200
# โโ Stripe: ๆๅๅพใใผใฏใณๅๅพ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/get-token")
def get_token():
"""ๆฑบๆธๅฎไบๅพใซ Stripe Session ID ใใใใผใฏใณใ่ฟใ"""
if FREE_MODE:
return jsonify({"token": "FREE_MODE"})
session_id = request.args.get("session_id", "")
if not session_id:
return jsonify({"error": "session_id ใๅฟ
่ฆใงใ"}), 400
try:
import stripe
stripe.api_key = STRIPE_SECRET_KEY
session = stripe.checkout.Session.retrieve(session_id)
sub_id = session.get("subscription")
cust_id = session.get("customer")
email = session.get("customer_details", {}).get("email", "")
# ๆขๅญใใผใฏใณใๆขใ๏ผWebhook ใๅ
ใซๅฆ็ใใฆใใๅ ดๅ๏ผ
data = load_tokens()
for tok, info in data.items():
if info.get("subscription_id") == sub_id:
return jsonify({"token": tok, "email": email})
# Webhook ใใพใ ใชใไฝๆ
token = create_token(cust_id, sub_id, email)
return jsonify({"token": token, "email": email})
except Exception as e:
return jsonify({"error": str(e)}), 500
# โโ ใใผใฏใณๆค่จผ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/verify-token", methods=["POST"])
def verify_token():
token = request.json.get("token", "")
return jsonify({"valid": is_token_valid(token)})
# โโ ใใกใคใซใขใใใญใผใ & ๅฆ็ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/upload", methods=["POST"])
def upload():
# ใใผใฏใณ่ช่จผ
token = request.form.get("token", "")
if not is_token_valid(token):
return jsonify({"error": "็กๅนใชใใผใฏใณใงใใๆ้ใใผใธใใ็ป้ฒใใฆใใ ใใใ"}), 401
if "file" not in request.files:
return jsonify({"error": "ใใกใคใซใใใใพใใ"}), 400
file = request.files["file"]
if not file.filename:
return jsonify({"error": "ใใกใคใซๅใ็ฉบใงใ"}), 400
inst_vol = float(request.form.get("inst_vol", 0.15))
model = request.form.get("model", "htdemucs_6s")
use_mdx = request.form.get("use_mdx", "true").lower() == "true"
job_id = str(uuid.uuid4())
suffix = Path(file.filename).suffix
input_path = UPLOAD_DIR / f"{job_id}_input{suffix}"
file.save(str(input_path))
with JOBS_LOCK:
JOBS[job_id] = {
"status": "queued", "progress": [],
"output_path": None, "error": None,
"filename": file.filename,
}
t = threading.Thread(
target=process_job,
args=(job_id, input_path, inst_vol, model, use_mdx),
daemon=True,
)
t.start()
return jsonify({"job_id": job_id})
# โโ ้ฒๆ SSE โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/progress/<job_id>")
def progress(job_id: str):
def generate():
import time
sent = 0
while True:
with JOBS_LOCK:
if job_id not in JOBS:
yield 'data: {"error":"not found"}\n\n'; return
job = JOBS[job_id]
new_ents = job["progress"][sent:]
sent += len(new_ents)
status = job["status"]
for e in new_ents:
yield f"data: {json.dumps(e, ensure_ascii=False)}\n\n"
if status in ("done", "error"):
yield f"data: {json.dumps({'status': status})}\n\n"; return
time.sleep(0.5)
return Response(generate(), mimetype="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"})
@flask_app.route("/status/<job_id>")
def status(job_id: str):
with JOBS_LOCK:
job = JOBS.get(job_id)
if not job:
return jsonify({"error": "not found"}), 404
return jsonify({"status": job["status"], "error": job.get("error")})
# โโ ใใฆใณใญใผใ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@flask_app.route("/download/<job_id>")
def download(job_id: str):
with JOBS_LOCK:
job = JOBS.get(job_id)
if not job or job["status"] != "done":
return jsonify({"error": "ใใกใคใซๆชๆบๅ"}), 404
output_path = Path(job["output_path"])
if not output_path.exists():
return jsonify({"error": "ใใกใคใซใ่ฆใคใใใพใใ"}), 404
download_name = Path(job.get("filename", "audio")).stem + "_panned.wav"
return send_file(str(output_path), as_attachment=True,
download_name=download_name, mimetype="audio/wav")
# โโ ่ตทๅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if __name__ == "__main__":
mode = "FREE๏ผStripe ๆช่จญๅฎ๏ผ" if FREE_MODE else "ๆๆ๏ผStripe ๆๅน๏ผ"
print("=" * 52)
print(f"๐ต HarmoSplit ่ตทๅไธญ... ใขใผใ: {mode}")
print(f" http://localhost:{PORT}")
print("=" * 52)
flask_app.run(host="0.0.0.0", port=PORT, debug=False, threaded=True)
|