File size: 967 Bytes
4e67111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fastapi import FastAPI, Request, Form, Depends
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
import pickle
import numpy as np

# Load the trained model
with open("model.pkl", "rb") as model_file:
    model = pickle.load(model_file)

app = FastAPI()

# Setup templates and static files
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")

@app.get("/")
def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

@app.post("/predict")
def predict(
    request: Request,
    x1: float = Form(...),
    x2: float = Form(...),
    x3: float = Form(...),
    x4: float = Form(...),
):
    input_features = np.array([[x1, x2, x3, x4]])
    prediction = model.predict(input_features)[0]  # Get the predicted category
    return templates.TemplateResponse("index.html", {"request": request, "result": prediction})