from fastapi import FastAPI from pydantic import BaseModel import pandas as pd import joblib # ========================== # Load Model & Encoders # ========================== model = joblib.load("xgboost_model.pkl") le_area = joblib.load("area_encoder.pkl") le_dept = joblib.load("dept_encoder.pkl") le_cat = joblib.load("category_encoder.pkl") # ========================== # FastAPI App # ========================== app = FastAPI( title="FFCL Consumable Prediction API", description="Predict Turnaround Consumable Quantity", version="1.0" ) # ========================== # Input Schema # ========================== class PredictionInput(BaseModel): unit_price: float y2017: float y2019: float y2021: float area: str dept: str category: str # ========================== # Home # ========================== @app.get("/") def home(): return { "message": "FFCL Quantity Prediction API is Running" } # ========================== # Prediction API # ========================== @app.post("/predict") def predict(data: PredictionInput): avg_history = ( data.y2017 + data.y2019 + data.y2021 ) / 3 trend = data.y2021 - data.y2019 consumption_count = sum([ data.y2017 > 0, data.y2019 > 0, data.y2021 > 0 ]) area_encoded = le_area.transform([data.area])[0] dept_encoded = le_dept.transform([data.dept])[0] cat_encoded = le_cat.transform([data.category])[0] input_df = pd.DataFrame({ "Unit Price":[data.unit_price], 2017:[data.y2017], 2019:[data.y2019], 2021:[data.y2021], "Avg_History":[avg_history], "Trend":[trend], "Consumption_Count":[consumption_count], "Area_Encoded":[area_encoded], "Dept_Encoded":[dept_encoded], "Category_Encoded":[cat_encoded] }) prediction = model.predict(input_df)[0] return { "Predicted Qty": round(float(prediction),2) }