Download Backend/main.py from PranilB/Question_Answering_Classification: direct link, hf CLI and curl.
- Browser
- Download file 3.39 kB
-
https://huggingface.co/spaces/PranilB/Question_Answering_Classification/resolve/main/Backend/main.py
- Command line
-
hf download hf://spaces/PranilB/Question_Answering_Classification/Backend/main.py
-
curl -L -o main.py https://huggingface.co/spaces/PranilB/Question_Answering_Classification/resolve/main/Backend/main.py
3.39 kB
| """ | |
| main.py β FastAPI application entry point for the NEET Biology Q&A system | |
| Run: | |
| uvicorn Backend.main:app --reload --port 8000 | |
| Docs: | |
| http://localhost:8000/docs (Swagger UI) | |
| http://localhost:8000/redoc (ReDoc) | |
| """ | |
| import time | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from Backend.schemas import HealthResponse | |
| from Backend.routers.qa import router as qa_router | |
| # ============================== | |
| # STARTUP / SHUTDOWN | |
| # ============================== | |
| async def lifespan(app: FastAPI): | |
| """ | |
| Pre-load heavy models at startup so the first request is fast. | |
| """ | |
| print("\nπ Starting NEET Biology Q&A Backend...") | |
| start = time.time() | |
| # Pre-load BERT classifier (singleton) | |
| from Classification.classify_and_extract import get_predictor | |
| get_predictor() | |
| # Pre-load embedding model (singleton) | |
| from rag.retriever import get_embedding_model | |
| get_embedding_model() | |
| elapsed = time.time() - start | |
| print(f"β All models loaded in {elapsed:.1f}s β server ready!\n") | |
| yield # App runs here | |
| print("\nπ Shutting down NEET Biology Q&A Backend.") | |
| # ============================== | |
| # APP INSTANCE | |
| # ============================== | |
| app = FastAPI( | |
| title="NEET Biology Q&A API", | |
| description=( | |
| "AI-powered Question Answering system for NEET Biology. " | |
| "Uses BERT classification for chapter detection and a " | |
| "RAG pipeline with NCERT textbook context for accurate answers." | |
| ), | |
| version="1.0.0", | |
| lifespan=lifespan, | |
| ) | |
| # ============================== | |
| # CORS β Allow frontend access | |
| # ============================== | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=[ | |
| "http://localhost:3000", # React/Next.js dev | |
| "http://localhost:5173", # Vite dev | |
| "http://localhost:5500", # Live Server | |
| "http://127.0.0.1:5500", | |
| "*", # Allow all for development | |
| ], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ============================== | |
| # ROUTES | |
| # ============================== | |
| app.include_router(qa_router) | |
| async def health_check(): | |
| """Check if the server and all components are running.""" | |
| from Classification.classify_and_extract import _predictor | |
| from rag.retriever import _embedding_model, _supabase_client | |
| return HealthResponse( | |
| status="ok", | |
| version="1.0.0", | |
| components={ | |
| "bert_classifier": "loaded" if _predictor is not None else "not loaded", | |
| "embedding_model": "loaded" if _embedding_model is not None else "not loaded", | |
| "supabase": "connected" if _supabase_client is not None else "not connected", | |
| }, | |
| ) | |
| async def root(): | |
| """API root β welcome message.""" | |
| return { | |
| "message": "NEET Biology Q&A API", | |
| "version": "1.0.0", | |
| "docs": "/docs", | |
| "endpoints": { | |
| "POST /api/qa": "Full Q&A pipeline (classify + answer + verify)", | |
| "POST /api/classify": "Chapter classification + topic extraction only", | |
| "GET /health": "Health check", | |
| }, | |
| } | |