Spaces:
Sleeping
Sleeping
File size: 1,548 Bytes
89040ed f73c670 89040ed f73c670 89040ed f73c670 89040ed f73c670 89040ed f73c670 89040ed f73c670 89040ed f73c670 |
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 |
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
import torch
import os
from pipeline import build_audiosep, separate_audio
app = FastAPI()
# Create a directory to save processed audio files if it doesn't exist
if not os.path.exists('processed'):
os.makedirs('processed')
# Mount the static files directory
app.mount("/processed", StaticFiles(directory="processed"), name="processed")
def process_audio(audio_file_path, text):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = build_audiosep(
config_yaml='config/audiosep_base.yaml',
checkpoint_path='checkpoint/audiosep_base_4M_steps.ckpt',
device=device)
output_file = os.path.join('processed', 'SeparatedAudio.wav')
separate_audio(model, audio_file_path, text, output_file, device)
return output_file
@app.post("/upload")
async def upload_file(file: UploadFile = File(...), text: str = Form(...)):
contents = await file.read()
# Save the audio file
input_file_path = os.path.join('uploaded', file.filename)
with open(input_file_path, "wb") as f:
f.write(contents)
# Process the audio file
processed_audio_file_path = process_audio(input_file_path, text)
# Response with processed audio file URL
return JSONResponse(status_code=200, content={"message": "Audio file received and processed.", "processed_audio_file": f"/processed/{os.path.basename(processed_audio_file_path)}"})
|