Rthur2003 commited on
Commit
95c22a6
·
1 Parent(s): de799a3

fix: update audio processing endpoint to accept options as JSON string and validate input

Browse files
Files changed (1) hide show
  1. app/routes/data_processing.py +13 -3
app/routes/data_processing.py CHANGED
@@ -6,7 +6,8 @@ import time
6
  from collections import defaultdict
7
  from fastapi import APIRouter, File, UploadFile, Form, HTTPException, Request, Depends, status
8
  from fastapi.responses import StreamingResponse
9
- from pydantic import Json
 
10
  import logging
11
 
12
  from app.schemas import AudioAugmentationOptions
@@ -45,7 +46,7 @@ async def _process_rate_limit(request: Request) -> None:
45
  @router.post("/audio", dependencies=[Depends(_process_rate_limit)])
46
  async def process_audio_endpoint(
47
  file: UploadFile = File(...),
48
- options: Json[AudioAugmentationOptions] = Form(...)
49
  ):
50
  """
51
  Process an audio file with the given augmentation options.
@@ -54,6 +55,15 @@ async def process_audio_endpoint(
54
  MAX_PAYLOAD_BYTES = 30 * 1024 * 1024 # 30 MB
55
  logger.info(f"Received audio processing request for file: {file.filename}")
56
 
 
 
 
 
 
 
 
 
 
57
  if not file.content_type or not file.content_type.startswith("audio/"):
58
  raise HTTPException(status_code=400, detail={"code": "invalid_file_type", "message": "Invalid file type. Must be audio."})
59
 
@@ -73,7 +83,7 @@ async def process_audio_endpoint(
73
  content = b"".join(chunks)
74
 
75
  # Process audio
76
- processed_audio = process_audio(content, options)
77
 
78
  # Return as downloadable file
79
  filename = f"processed_{file.filename}.wav"
 
6
  from collections import defaultdict
7
  from fastapi import APIRouter, File, UploadFile, Form, HTTPException, Request, Depends, status
8
  from fastapi.responses import StreamingResponse
9
+ from pydantic import ValidationError
10
+ import json
11
  import logging
12
 
13
  from app.schemas import AudioAugmentationOptions
 
46
  @router.post("/audio", dependencies=[Depends(_process_rate_limit)])
47
  async def process_audio_endpoint(
48
  file: UploadFile = File(...),
49
+ options: str = Form(...)
50
  ):
51
  """
52
  Process an audio file with the given augmentation options.
 
55
  MAX_PAYLOAD_BYTES = 30 * 1024 * 1024 # 30 MB
56
  logger.info(f"Received audio processing request for file: {file.filename}")
57
 
58
+ # Parse options JSON string into validated Pydantic model
59
+ try:
60
+ parsed_options = AudioAugmentationOptions.model_validate_json(options)
61
+ except (ValidationError, json.JSONDecodeError) as e:
62
+ raise HTTPException(
63
+ status_code=422,
64
+ detail={"code": "invalid_options", "message": f"Invalid options format: {e}"}
65
+ )
66
+
67
  if not file.content_type or not file.content_type.startswith("audio/"):
68
  raise HTTPException(status_code=400, detail={"code": "invalid_file_type", "message": "Invalid file type. Must be audio."})
69
 
 
83
  content = b"".join(chunks)
84
 
85
  # Process audio
86
+ processed_audio = process_audio(content, parsed_options)
87
 
88
  # Return as downloadable file
89
  filename = f"processed_{file.filename}.wav"