-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_app.py
More file actions
156 lines (133 loc) · 4.85 KB
/
web_app.py
File metadata and controls
156 lines (133 loc) · 4.85 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
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
#!/usr/bin/env python3
import os
import secrets
from pathlib import Path
from flask import Flask, render_template_string, request, send_from_directory
from karaoke import remove_vocals
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>GuitarKaraoke</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>GuitarKaraoke</h2>
<p>Select a mode, upload audio, and preview the result.</p>
<form method="post" enctype="multipart/form-data">
<input type="file" name="audio_file" accept="audio/*" required>
<div class="row">
<label>
Mode:
<select name="mode">
<option value="karaoke">Karaoke (No Vocals)</option>
<option value="guitar_karaoke" selected>Guitar Karaoke (No Guitar)</option>
</select>
</label>
</div>
<div class="row">
<label>
Other stem level (for Guitar Karaoke, 0.0 - 1.0):
<input type="number" name="other_level" min="0" max="1" step="0.1" value="0.0">
</label>
</div>
<div class="row">
<label>
Output format:
<select name="output_format">
<option value="mp3" selected>MP3</option>
<option value="wav">WAV</option>
</select>
</label>
</div>
<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>{{ result_label }}</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)
mode = request.form.get("mode", "guitar_karaoke").lower()
if mode not in {"karaoke", "guitar_karaoke"}:
mode = "guitar_karaoke"
output_format = request.form.get("output_format", "mp3").lower()
if output_format not in {"mp3", "wav"}:
output_format = "mp3"
suffix = "karaoke" if mode == "karaoke" else "no_guitar"
output_name = f"{upload_path.stem}_{suffix}.{output_format}"
output_path = OUTPUT_DIR / output_name
try:
if mode == "karaoke":
remove_vocals(upload_path, output_path, model="htdemucs")
result_label = "Karaoke (No Vocals)"
else:
other_level_raw = request.form.get("other_level", "0.0")
other_level = float(other_level_raw)
other_level = max(0.0, min(1.0, other_level))
remove_guitar(
upload_path,
output_path,
model="htdemucs",
other_level=other_level,
)
result_label = "Guitar Karaoke (No Guitar)"
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,
result_label=result_label,
)
return render_template_string(
PAGE_TEMPLATE, original_url=None, output_url=None, result_label=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"))
debug = os.environ.get("FLASK_DEBUG", "0") == "1"
app.run(host="0.0.0.0", port=port, debug=debug)