Spaces:
Running
Running
Update transcription_routes.py
Browse files- transcription_routes.py +7 -9
transcription_routes.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
# transcription_routes.py
|
2 |
|
3 |
import os
|
4 |
-
from fastapi import APIRouter, File, UploadFile, HTTPException
|
5 |
from fastapi.responses import JSONResponse
|
6 |
from groq import Groq
|
7 |
|
8 |
-
router = APIRouter()
|
9 |
|
10 |
# Load your Groq API key
|
11 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
@@ -14,12 +14,8 @@ if not GROQ_API_KEY:
|
|
14 |
|
15 |
client = Groq(api_key=GROQ_API_KEY)
|
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 |
-
"/
|
23 |
summary="Upload an audio file and return its transcription",
|
24 |
response_description="Returns transcribed text as JSON"
|
25 |
)
|
@@ -42,10 +38,12 @@ async def transcribe_audio(file: UploadFile = File(...)):
|
|
42 |
model="whisper-large-v3",
|
43 |
response_format="verbose_json",
|
44 |
)
|
45 |
-
# The client returns .text
|
46 |
transcript = getattr(resp, "text", None) or resp.get("text")
|
|
|
|
|
47 |
except Exception as e:
|
48 |
-
# All errors
|
49 |
raise HTTPException(status_code=502, detail=f"Transcription service error: {e}")
|
50 |
|
51 |
return JSONResponse(content={"transcript": transcript})
|
|
|
1 |
# transcription_routes.py
|
2 |
|
3 |
import os
|
4 |
+
from fastapi import APIRouter, File, UploadFile, HTTPException
|
5 |
from fastapi.responses import JSONResponse
|
6 |
from groq import Groq
|
7 |
|
8 |
+
router = APIRouter(prefix="/transcribe", tags=["transcription"])
|
9 |
|
10 |
# Load your Groq API key
|
11 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
14 |
|
15 |
client = Groq(api_key=GROQ_API_KEY)
|
16 |
|
|
|
|
|
|
|
|
|
17 |
@router.post(
|
18 |
+
"/audio",
|
19 |
summary="Upload an audio file and return its transcription",
|
20 |
response_description="Returns transcribed text as JSON"
|
21 |
)
|
|
|
38 |
model="whisper-large-v3",
|
39 |
response_format="verbose_json",
|
40 |
)
|
41 |
+
# The client returns .text or, if dict-like, resp.get("text")
|
42 |
transcript = getattr(resp, "text", None) or resp.get("text")
|
43 |
+
if transcript is None:
|
44 |
+
raise ValueError("No transcript returned by service")
|
45 |
except Exception as e:
|
46 |
+
# All errors return 502 with the exception message
|
47 |
raise HTTPException(status_code=502, detail=f"Transcription service error: {e}")
|
48 |
|
49 |
return JSONResponse(content={"transcript": transcript})
|