mominah commited on
Commit
ae9d8c1
·
verified ·
1 Parent(s): c3af556

Update transcription_routes.py

Browse files
Files changed (1) hide show
  1. transcription_routes.py +5 -14
transcription_routes.py CHANGED
@@ -1,11 +1,9 @@
1
  # transcription_routes.py
2
 
3
  import os
4
- import io
5
  from fastapi import APIRouter, File, UploadFile, HTTPException, Request
6
  from fastapi.responses import JSONResponse
7
  from groq import Groq
8
- from groq.errors import GroqError
9
 
10
  router = APIRouter()
11
 
@@ -18,9 +16,7 @@ client = Groq(api_key=GROQ_API_KEY)
18
 
19
  @router.exception_handler(Exception)
20
  async def global_exception_handler(request: Request, exc: Exception):
21
- return JSONResponse(
22
- status_code=500, content={"detail": str(exc)}
23
- )
24
 
25
  @router.post(
26
  "/transcribe",
@@ -41,20 +37,15 @@ async def transcribe_audio(file: UploadFile = File(...)):
41
 
42
  data = await file.read()
43
  try:
44
- # send to Groq Whisper model
45
  resp = client.audio.transcriptions.create(
46
  file=(file.filename, data),
47
  model="whisper-large-v3",
48
  response_format="verbose_json",
49
  )
50
- # the library returns an object with a `.text` attribute
51
- transcript = getattr(resp, "text", None)
52
- if transcript is None:
53
- # fallback if resp is dict-like
54
- transcript = resp.get("text") # type: ignore
55
- except GroqError as e:
56
- raise HTTPException(status_code=502, detail=f"Transcription service error: {e}")
57
  except Exception as e:
58
- raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
 
59
 
60
  return JSONResponse(content={"transcript": transcript})
 
1
  # transcription_routes.py
2
 
3
  import os
 
4
  from fastapi import APIRouter, File, UploadFile, HTTPException, Request
5
  from fastapi.responses import JSONResponse
6
  from groq import Groq
 
7
 
8
  router = APIRouter()
9
 
 
16
 
17
  @router.exception_handler(Exception)
18
  async def global_exception_handler(request: Request, exc: Exception):
19
+ return JSONResponse(status_code=500, content={"detail": str(exc)})
 
 
20
 
21
  @router.post(
22
  "/transcribe",
 
37
 
38
  data = await file.read()
39
  try:
 
40
  resp = client.audio.transcriptions.create(
41
  file=(file.filename, data),
42
  model="whisper-large-v3",
43
  response_format="verbose_json",
44
  )
45
+ # The client returns .text in resp, or .get("text") if dict-like
46
+ transcript = getattr(resp, "text", None) or resp.get("text")
 
 
 
 
 
47
  except Exception as e:
48
+ # All errors are handled here
49
+ raise HTTPException(status_code=502, detail=f"Transcription service error: {e}")
50
 
51
  return JSONResponse(content={"transcript": transcript})