-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
83 lines (69 loc) · 2.91 KB
/
main.py
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
from fastapi import FastAPI, File, UploadFile, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
import os
import urllib.request
from spleeter.separator import Separator
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
FILEPATH = {}
@app.post('/split-audio')
async def split_audio(request: Request, file: UploadFile = File(...)):
if not file:
return {'success': False, 'message': 'No file uploaded'}, 400
output_dir = './output'
separator = Separator('spleeter:4stems')
filepath = os.path.join(output_dir, file.filename)
with open(filepath, 'wb') as f:
f.write(await file.read())
separator.separate_to_file(filepath, output_dir)
# Get the base URL of the server
server_url = str(request.base_url)
global FILEPATH
FILEPATH = {
"vocals":f"{server_url}audio/?filepath={os.path.splitext(file.filename)[0]}/vocals.wav",
"bass":f"{server_url}audio/?filepath={os.path.splitext(file.filename)[0]}/bass.wav",
"drums":f"{server_url}audio/?filepath={os.path.splitext(file.filename)[0]}/drums.wav",
'other':f"{server_url}audio/?filepath={os.path.splitext(file.filename)[0]}/other.wav"
}
# Return URLs for each stem file, replacing backslashes with forward slashes
return {
'success': True,
"vocals":f"{server_url}audio?filepath={os.path.splitext(file.filename)[0]}/vocals.wav",
"bass":f"{server_url}audio/?filepath={os.path.splitext(file.filename)[0]}/bass.wav",
"drums":f"{server_url}audio/?filepath={os.path.splitext(file.filename)[0]}/drums.wav",
'other':f"{server_url}audio/?filepath={os.path.splitext(file.filename)[0]}/other.wav"
}
@app.get('/audio')
async def download_file(filepath: str):
filename = os.path.basename(filepath)
file_path = f"./output/{filepath}"
return FileResponse(path=file_path, media_type="application/octet-stream", filename=filename)
@app.get('/playback-urls')
async def get_playback_urls():
try:
global FILEPATH
data = FILEPATH
if data is None:
return {'success': False, 'message': 'No JSON body found in the request'}
vocalsurl = data['vocals']
bassurl = data['bass']
drumsurl = data['drums']
otherurl = data['other']
# Return the URLs as a JSON response
return {
'success': True,
'vocalsurl': vocalsurl,
'bassurl': bassurl,
'drumsurl': drumsurl,
'otherurl': otherurl
}
except Exception as e:
print(f"Error getting playback URLs: {e}")
return {'success': False}