mominah commited on
Commit
93badc8
·
verified ·
1 Parent(s): 894d33c

Create transcription_routes.py

Browse files
Files changed (1) hide show
  1. transcription_routes.py +60 -0
transcription_routes.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
12
+ # Load your Groq API key
13
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
14
+ if not GROQ_API_KEY:
15
+ raise ValueError("GROQ_API_KEY environment variable is not set")
16
+
17
+ 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",
27
+ summary="Upload an audio file and return its transcription",
28
+ response_description="Returns transcribed text as JSON"
29
+ )
30
+ async def transcribe_audio(file: UploadFile = File(...)):
31
+ if not file.filename:
32
+ raise HTTPException(status_code=400, detail="No file provided")
33
+
34
+ # Only allow common audio formats
35
+ allowed_exts = (".mp3", ".wav", ".m4a", ".flac")
36
+ if not file.filename.lower().endswith(allowed_exts):
37
+ raise HTTPException(
38
+ status_code=415,
39
+ detail=f"Unsupported file type. Allowed: {', '.join(allowed_exts)}"
40
+ )
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})