final-sp / app.py
ayush0100's picture
Add FastAPI app, frontend, Dockerfile, and ML model
e6f62db
Raw History Blame Contribute Delete
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
# -------------------------
@app.get("/")
def home():
return FileResponse("index.html")
# -------------------------
# Input schema
# -------------------------
class StudentData(BaseModel):
Math: float
English: float
Science: float
# -------------------------
# Prediction endpoint
# -------------------------
@app.post("/predict")
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}