Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Form, Request
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
import joblib
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
|
| 8 |
+
# Initialize FastAPI app
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# Load saved models
|
| 12 |
+
logistic_regression_model = joblib.load('logistic_regression_model.pkl')
|
| 13 |
+
svm_model = joblib.load('svm_model.pkl')
|
| 14 |
+
rfc_model = joblib.load('random_forest_model.pkl')
|
| 15 |
+
knn_model = joblib.load('knn_model.pkl')
|
| 16 |
+
neural_network_model = joblib.load('neural_network_model.pkl')
|
| 17 |
+
|
| 18 |
+
# Load scaler (assuming you saved it as scaler.pkl)
|
| 19 |
+
scaler = joblib.load('scaler.pkl')
|
| 20 |
+
|
| 21 |
+
# Jinja2 template renderer
|
| 22 |
+
templates = Jinja2Templates(directory="templates")
|
| 23 |
+
|
| 24 |
+
# Define function to make predictions
|
| 25 |
+
def make_prediction(model, data):
|
| 26 |
+
prediction = model.predict([data])
|
| 27 |
+
return prediction[0]
|
| 28 |
+
|
| 29 |
+
# Home page route
|
| 30 |
+
@app.get("/", response_class=HTMLResponse)
|
| 31 |
+
async def home(request: Request):
|
| 32 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 33 |
+
|
| 34 |
+
# Prediction route
|
| 35 |
+
@app.post("/predict", response_class=HTMLResponse)
|
| 36 |
+
async def predict(request: Request, variance: float = Form(...), skewness: float = Form(...),
|
| 37 |
+
curtosis: float = Form(...), entropy: float = Form(...)):
|
| 38 |
+
# Prepare the feature vector
|
| 39 |
+
features = np.array([variance, skewness, curtosis, entropy])
|
| 40 |
+
|
| 41 |
+
# Scale the input features
|
| 42 |
+
scaled_features = scaler.transform([features])
|
| 43 |
+
|
| 44 |
+
# Make predictions using each model
|
| 45 |
+
logistic_regression_prediction = make_prediction(logistic_regression_model, scaled_features)
|
| 46 |
+
svm_prediction = make_prediction(svm_model, scaled_features)
|
| 47 |
+
rfc_prediction = make_prediction(rfc_model, scaled_features)
|
| 48 |
+
knn_prediction = make_prediction(knn_model, scaled_features)
|
| 49 |
+
nn_prediction = make_prediction(neural_network_model, scaled_features)
|
| 50 |
+
|
| 51 |
+
# Render the results page with predictions
|
| 52 |
+
return templates.TemplateResponse("result.html", {
|
| 53 |
+
"request": request,
|
| 54 |
+
"logistic_regression": logistic_regression_prediction,
|
| 55 |
+
"svm": svm_prediction,
|
| 56 |
+
"random_forest": rfc_prediction,
|
| 57 |
+
"knn": knn_prediction,
|
| 58 |
+
"neural_network": nn_prediction
|
| 59 |
+
})
|