Phuoctai commited on
Commit
5355f52
·
1 Parent(s): 050979d

fix: pass Content-Type header to HF Whisper API to fix 400 error

Browse files
Files changed (1) hide show
  1. app.py +4 -3
app.py CHANGED
@@ -42,9 +42,9 @@ ALLOWED_AUDIO_TYPES = {
42
  # ---------------------------------------------------------------------------
43
  # Whisper – Speech-to-Text via HF Serverless Inference API
44
  # ---------------------------------------------------------------------------
45
- def transcribe_audio(audio_bytes: bytes) -> str:
46
  """Gửi bytes âm thanh tới Hugging Face Inference API và nhận lại văn bản."""
47
- headers = {}
48
  if HF_TOKEN:
49
  headers["Authorization"] = f"Bearer {HF_TOKEN}"
50
 
@@ -416,7 +416,8 @@ async def detect_voice_task(file: UploadFile = File(..., description="File ghi
416
  raise HTTPException(status_code=400, detail="File quá lớn. Giới hạn 25MB.")
417
 
418
  # Bước 1: Speech-to-Text bằng Whisper
419
- transcribed_text = transcribe_audio(audio_bytes)
 
420
 
421
  if not transcribed_text:
422
  return {
 
42
  # ---------------------------------------------------------------------------
43
  # Whisper – Speech-to-Text via HF Serverless Inference API
44
  # ---------------------------------------------------------------------------
45
+ def transcribe_audio(audio_bytes: bytes, content_type: str = "audio/mpeg") -> str:
46
  """Gửi bytes âm thanh tới Hugging Face Inference API và nhận lại văn bản."""
47
+ headers = {"Content-Type": content_type}
48
  if HF_TOKEN:
49
  headers["Authorization"] = f"Bearer {HF_TOKEN}"
50
 
 
416
  raise HTTPException(status_code=400, detail="File quá lớn. Giới hạn 25MB.")
417
 
418
  # Bước 1: Speech-to-Text bằng Whisper
419
+ audio_content_type = file.content_type or "audio/mpeg"
420
+ transcribed_text = transcribe_audio(audio_bytes, audio_content_type)
421
 
422
  if not transcribed_text:
423
  return {