AudioDeNoiseAPI / server.py
ArnavGhost's picture
Update server.py
f73c670 verified
raw
history blame
No virus
1.55 kB
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)}"})