Download app.py from ayush0100/final-sp: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/ayush0100/final-sp/resolve/main/app.py
- Command line
-
hf download hf://spaces/ayush0100/final-sp/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ayush0100/final-sp/resolve/main/app.py
1.28 kB
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import FileResponse | |
| from pydantic import BaseModel | |
| import pickle | |
| import os | |
| # ------------------------- | |
| # Load ML model | |
| # ------------------------- | |
| model_path = os.path.join(os.path.dirname(__file__), "student_model.pkl") | |
| with open(model_path, "rb") as f: | |
| model = pickle.load(f) | |
| # ------------------------- | |
| # FastAPI app setup | |
| # ------------------------- | |
| app = FastAPI(title="Student Pass/Fail Predictor") | |
| # Enable CORS so JS can call API | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"] | |
| ) | |
| # ------------------------- | |
| # Serve HTML page at root | |
| # ------------------------- | |
| def home(): | |
| return FileResponse("index.html") | |
| # ------------------------- | |
| # Input schema | |
| # ------------------------- | |
| class StudentData(BaseModel): | |
| Math: float | |
| English: float | |
| Science: float | |
| # ------------------------- | |
| # Prediction endpoint | |
| # ------------------------- | |
| def predict(data: StudentData): | |
| features = [[data.Math, data.English, data.Science]] | |
| prediction = model.predict(features)[0] | |
| result = "Pass" if prediction == 1 else "Fail" | |
| return {"result": result} | |