-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_app.py
More file actions
103 lines (82 loc) · 2.98 KB
/
web_app.py
File metadata and controls
103 lines (82 loc) · 2.98 KB
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
#!/usr/bin/env python3
import os
import secrets
from pathlib import Path
from flask import Flask, render_template_string, request, send_from_directory
from remove_guitar import remove_guitar
BASE_DIR = Path(__file__).resolve().parent
UPLOAD_DIR = BASE_DIR / "uploads"
OUTPUT_DIR = BASE_DIR / "outputs"
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
app = Flask(__name__)
PAGE_TEMPLATE = """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Remove Guitar (Demucs)</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.card { max-width: 720px; padding: 24px; border: 1px solid #ddd; border-radius: 8px; }
.row { margin-top: 18px; }
audio { width: 100%; }
.error { color: #b00020; }
</style>
</head>
<body>
<div class="card">
<h2>Remove Guitar (Approx)</h2>
<p>This mixes drums + bass + vocals from Demucs to reduce guitar.</p>
<form method="post" enctype="multipart/form-data">
<input type="file" name="audio_file" accept="audio/*" required>
<button type="submit">Process</button>
</form>
{% if error %}
<div class="row error">{{ error }}</div>
{% endif %}
{% if original_url and output_url %}
<div class="row">
<strong>Original</strong>
<audio controls src="{{ original_url }}"></audio>
</div>
<div class="row">
<strong>No Guitar (Approx)</strong>
<audio controls src="{{ output_url }}"></audio>
</div>
{% endif %}
</div>
</body>
</html>
"""
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
audio_file = request.files.get("audio_file")
if not audio_file:
return render_template_string(PAGE_TEMPLATE, error="No file provided.")
token = secrets.token_hex(4)
safe_name = f"{token}_{Path(audio_file.filename).name}"
upload_path = UPLOAD_DIR / safe_name
audio_file.save(upload_path)
output_name = f"{upload_path.stem}_no_guitar.mp3"
output_path = OUTPUT_DIR / output_name
try:
remove_guitar(upload_path, output_path, model="htdemucs")
except Exception as exc:
return render_template_string(PAGE_TEMPLATE, error=str(exc))
original_url = f"/uploads/{upload_path.name}"
output_url = f"/outputs/{output_name}"
return render_template_string(
PAGE_TEMPLATE, original_url=original_url, output_url=output_url
)
return render_template_string(PAGE_TEMPLATE, original_url=None, output_url=None)
@app.route("/uploads/<path:filename>")
def get_upload(filename: str):
return send_from_directory(UPLOAD_DIR, filename)
@app.route("/outputs/<path:filename>")
def get_output(filename: str):
return send_from_directory(OUTPUT_DIR, filename)
if __name__ == "__main__":
port = int(os.environ.get("PORT", "8000"))
app.run(host="0.0.0.0", port=port, debug=True)