ArnavGhost commited on
Commit
3ef2b3b
1 Parent(s): f73c670

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +8 -5
server.py CHANGED
@@ -7,9 +7,11 @@ 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")
@@ -29,7 +31,7 @@ def process_audio(audio_file_path, text):
29
  @app.post("/upload")
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)
@@ -38,4 +40,5 @@ async def upload_file(file: UploadFile = File(...), text: str = Form(...)):
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)}"})
 
 
7
 
8
  app = FastAPI()
9
 
10
+ @app.on_event("startup")
11
+ async def startup_event():
12
+ # Create directories for uploaded and processed files
13
+ os.makedirs('uploaded', exist_ok=True)
14
+ os.makedirs('processed', exist_ok=True)
15
 
16
  # Mount the static files directory
17
  app.mount("/processed", StaticFiles(directory="processed"), name="processed")
 
31
  @app.post("/upload")
32
  async def upload_file(file: UploadFile = File(...), text: str = Form(...)):
33
  contents = await file.read()
34
+ # Save the uploaded audio file
35
  input_file_path = os.path.join('uploaded', file.filename)
36
  with open(input_file_path, "wb") as f:
37
  f.write(contents)
 
40
  processed_audio_file_path = process_audio(input_file_path, text)
41
 
42
  # Response with processed audio file URL
43
+ processed_file_url = f"/processed/{os.path.basename(processed_audio_file_path)}"
44
+ return JSONResponse(status_code=200, content={"message": "Audio file received and processed.", "processed_audio_file": processed_file_url})