Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,80 @@ import os
|
|
| 5 |
import random
|
| 6 |
from werkzeug.utils import secure_filename
|
| 7 |
|
|
|
|
|
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@app.route('/vocal', methods=['GET', 'POST'])
|
| 12 |
def split_vocal():
|
| 13 |
separator = None
|
|
@@ -102,14 +173,16 @@ def index():
|
|
| 102 |
return jsonify({
|
| 103 |
'message': 'Audio Splitter API',
|
| 104 |
'endpoints': {
|
| 105 |
-
'GET /
|
|
|
|
| 106 |
'POST /vocal': 'Upload and split audio file (form-data with key "file")',
|
| 107 |
'GET /download/<filename>': 'Download separated files',
|
| 108 |
'GET /health': 'Health check'
|
| 109 |
},
|
| 110 |
-
'
|
| 111 |
-
'
|
| 112 |
-
'
|
|
|
|
| 113 |
}
|
| 114 |
})
|
| 115 |
|
|
|
|
| 5 |
import random
|
| 6 |
from werkzeug.utils import secure_filename
|
| 7 |
|
| 8 |
+
os.environ['MODEL_PATH'] = '/tmp/pretrained_models'
|
| 9 |
+
|
| 10 |
app = Flask(__name__)
|
| 11 |
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
|
| 12 |
|
| 13 |
+
@app.route('/upload', methods=['GET'])
|
| 14 |
+
def upload_and_split():
|
| 15 |
+
separator = None
|
| 16 |
+
input_file = None
|
| 17 |
+
base_dir = None
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
audio_url = request.args.get('url')
|
| 21 |
+
if not audio_url:
|
| 22 |
+
return jsonify({'error': 'URL parameter required'}), 400
|
| 23 |
+
|
| 24 |
+
random_id = random.randint(1000, 9999)
|
| 25 |
+
input_file = f'/tmp/input_{random_id}.mp3'
|
| 26 |
+
output_dir = '/tmp'
|
| 27 |
+
|
| 28 |
+
headers = {
|
| 29 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
response = requests.get(audio_url, timeout=60, headers=headers, allow_redirects=True, stream=True)
|
| 33 |
+
response.raise_for_status()
|
| 34 |
+
|
| 35 |
+
with open(input_file, 'wb') as f:
|
| 36 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 37 |
+
f.write(chunk)
|
| 38 |
+
|
| 39 |
+
separator = Separator('spleeter:2stems')
|
| 40 |
+
separator.separate_to_file(input_file, output_dir)
|
| 41 |
+
|
| 42 |
+
base_name = f'input_{random_id}'
|
| 43 |
+
base_dir = f'/tmp/{base_name}'
|
| 44 |
+
vocal_src = f'{base_dir}/vocals.wav'
|
| 45 |
+
instrumental_src = f'{base_dir}/accompaniment.wav'
|
| 46 |
+
|
| 47 |
+
vocal_dest = f'/tmp/vocal_{random_id}.mp3'
|
| 48 |
+
instrumental_dest = f'/tmp/instrumental_{random_id}.mp3'
|
| 49 |
+
|
| 50 |
+
os.system(f'ffmpeg -i "{vocal_src}" -b:a 192k "{vocal_dest}" -y -loglevel quiet')
|
| 51 |
+
os.system(f'ffmpeg -i "{instrumental_src}" -b:a 192k "{instrumental_dest}" -y -loglevel quiet')
|
| 52 |
+
|
| 53 |
+
return jsonify({
|
| 54 |
+
'success': True,
|
| 55 |
+
'message': 'Audio processed successfully',
|
| 56 |
+
'vocal': vocal_dest,
|
| 57 |
+
'instrumental': instrumental_dest,
|
| 58 |
+
'id': random_id,
|
| 59 |
+
'download_vocal': f'/download/vocal_{random_id}.mp3',
|
| 60 |
+
'download_instrumental': f'/download/instrumental_{random_id}.mp3'
|
| 61 |
+
})
|
| 62 |
+
|
| 63 |
+
except requests.exceptions.RequestException as e:
|
| 64 |
+
return jsonify({'error': f'Failed to download audio: {str(e)}'}), 400
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return jsonify({'error': str(e)}), 500
|
| 67 |
+
|
| 68 |
+
finally:
|
| 69 |
+
if separator:
|
| 70 |
+
del separator
|
| 71 |
+
if input_file and os.path.exists(input_file):
|
| 72 |
+
try:
|
| 73 |
+
os.remove(input_file)
|
| 74 |
+
except:
|
| 75 |
+
pass
|
| 76 |
+
if base_dir and os.path.exists(base_dir):
|
| 77 |
+
try:
|
| 78 |
+
os.system(f'rm -rf {base_dir}')
|
| 79 |
+
except:
|
| 80 |
+
pass
|
| 81 |
+
|
| 82 |
@app.route('/vocal', methods=['GET', 'POST'])
|
| 83 |
def split_vocal():
|
| 84 |
separator = None
|
|
|
|
| 173 |
return jsonify({
|
| 174 |
'message': 'Audio Splitter API',
|
| 175 |
'endpoints': {
|
| 176 |
+
'GET /upload?url=<audio_url>': 'Download from URL, split, then delete source',
|
| 177 |
+
'GET /vocal?url=<audio_url>': 'Split audio from URL (legacy)',
|
| 178 |
'POST /vocal': 'Upload and split audio file (form-data with key "file")',
|
| 179 |
'GET /download/<filename>': 'Download separated files',
|
| 180 |
'GET /health': 'Health check'
|
| 181 |
},
|
| 182 |
+
'example': {
|
| 183 |
+
'upload': 'curl "https://herzaj-spleeter.hf.space/upload?url=YOUR_AUDIO_URL"',
|
| 184 |
+
'vocal_url': 'curl "https://herzaj-spleeter.hf.space/vocal?url=YOUR_AUDIO_URL"',
|
| 185 |
+
'vocal_file': 'curl -X POST -F "file=@audio.mp3" https://herzaj-spleeter.hf.space/vocal'
|
| 186 |
}
|
| 187 |
})
|
| 188 |
|