Download app/main.py from shoaibahmed912/transcript-data-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/spaces/shoaibahmed912/transcript-data-backend/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/shoaibahmed912/transcript-data-backend/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/shoaibahmed912/transcript-data-backend/resolve/main/app/main.py
2.16 kB
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| from typing import Any, Dict | |
| from fastapi import Depends, FastAPI, File, HTTPException, UploadFile | |
| from fastapi.responses import RedirectResponse | |
| load_dotenv() | |
| from app.models import TranscriptResponse | |
| from app.services.firebase_auth import require_firebase_user | |
| from app.services.llm import GeminiExtractionError, extract_transcript_with_gemini | |
| ALLOWED_MIME_TYPES = { | |
| ".jpg": "image/jpeg", | |
| ".jpeg": "image/jpeg", | |
| ".jpp": "image/jpeg", | |
| ".png": "image/png", | |
| ".pdf": "application/pdf", | |
| } | |
| MAX_UPLOAD_MB = int(os.getenv("MAX_UPLOAD_MB", "10")) | |
| MAX_UPLOAD_SIZE = MAX_UPLOAD_MB * 1024 * 1024 | |
| app = FastAPI( | |
| title="Transcript Extraction API", | |
| description="Extracts student, semester, subject, marks, credit hour, and GPA data from transcript files.", | |
| version="0.1.0", | |
| ) | |
| def health() -> dict[str, str]: | |
| return {"status": "ok"} | |
| def root() -> RedirectResponse: | |
| return RedirectResponse(url="/docs") | |
| async def extract_transcript( | |
| file: UploadFile = File(...), | |
| firebase_user: Dict[str, Any] = Depends(require_firebase_user), | |
| ) -> TranscriptResponse: | |
| suffix = Path(file.filename or "").suffix.lower() | |
| mime_type = ALLOWED_MIME_TYPES.get(suffix) | |
| if mime_type is None: | |
| raise HTTPException( | |
| status_code=400, | |
| detail="Unsupported file type. Upload jpg, jpeg, jpp, png, or pdf.", | |
| ) | |
| file_bytes = await file.read() | |
| if not file_bytes: | |
| raise HTTPException(status_code=400, detail="Uploaded file is empty.") | |
| if len(file_bytes) > MAX_UPLOAD_SIZE: | |
| raise HTTPException( | |
| status_code=413, | |
| detail=f"File is too large. Maximum upload size is {MAX_UPLOAD_MB} MB.", | |
| ) | |
| try: | |
| return await extract_transcript_with_gemini(file_bytes, mime_type) | |
| except GeminiExtractionError as exc: | |
| raise HTTPException( | |
| status_code=502, | |
| detail=exc.message, | |
| ) from exc | |