ArnavGhost commited on
Commit
f73c670
1 Parent(s): 89040ed

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +16 -6
server.py CHANGED
@@ -1,10 +1,19 @@
1
  from fastapi import FastAPI, File, UploadFile, Form
2
- from starlette.responses import JSONResponse
 
3
  import torch
 
4
  from pipeline import build_audiosep, separate_audio
5
 
6
  app = FastAPI()
7
 
 
 
 
 
 
 
 
8
  def process_audio(audio_file_path, text):
9
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
 
@@ -13,7 +22,7 @@ def process_audio(audio_file_path, text):
13
  checkpoint_path='checkpoint/audiosep_base_4M_steps.ckpt',
14
  device=device)
15
 
16
- output_file = 'SeparatedAudio.wav'
17
  separate_audio(model, audio_file_path, text, output_file, device)
18
  return output_file
19
 
@@ -21,11 +30,12 @@ def process_audio(audio_file_path, text):
21
  async def upload_file(file: UploadFile = File(...), text: str = Form(...)):
22
  contents = await file.read()
23
  # Save the audio file
24
- with open(file.filename, "wb") as f:
 
25
  f.write(contents)
26
 
27
  # Process the audio file
28
- processed_audio_file_path = process_audio(file.filename, text)
29
 
30
- # Response with processed audio file path
31
- return JSONResponse(status_code=200, content={"message": "Audio file received and processed.", "processed_audio_file": processed_audio_file_path})
 
1
  from fastapi import FastAPI, File, UploadFile, Form
2
+ from fastapi.responses import JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
  import torch
5
+ import os
6
  from pipeline import build_audiosep, separate_audio
7
 
8
  app = FastAPI()
9
 
10
+ # Create a directory to save processed audio files if it doesn't exist
11
+ if not os.path.exists('processed'):
12
+ os.makedirs('processed')
13
+
14
+ # Mount the static files directory
15
+ app.mount("/processed", StaticFiles(directory="processed"), name="processed")
16
+
17
  def process_audio(audio_file_path, text):
18
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
19
 
 
22
  checkpoint_path='checkpoint/audiosep_base_4M_steps.ckpt',
23
  device=device)
24
 
25
+ output_file = os.path.join('processed', 'SeparatedAudio.wav')
26
  separate_audio(model, audio_file_path, text, output_file, device)
27
  return output_file
28
 
 
30
  async def upload_file(file: UploadFile = File(...), text: str = Form(...)):
31
  contents = await file.read()
32
  # Save the audio file
33
+ input_file_path = os.path.join('uploaded', file.filename)
34
+ with open(input_file_path, "wb") as f:
35
  f.write(contents)
36
 
37
  # Process the audio file
38
+ processed_audio_file_path = process_audio(input_file_path, text)
39
 
40
+ # Response with processed audio file URL
41
+ return JSONResponse(status_code=200, content={"message": "Audio file received and processed.", "processed_audio_file": f"/processed/{os.path.basename(processed_audio_file_path)}"})