Spaces:
Sleeping
Sleeping
surbi karki commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
# Load the trained model and scaler
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -39,16 +43,19 @@ class PCOSPrediction(BaseModel):
|
|
| 39 |
# Define the prediction endpoint
|
| 40 |
@app.post("/predict/", response_model=PCOSPrediction)
|
| 41 |
def predict(data: PCOSInput):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import joblib
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
# Load the trained model and scaler
|
| 8 |
+
try:
|
| 9 |
+
model = joblib.load('LR1.pkl') # Replace with the path to your saved model
|
| 10 |
+
scaler = joblib.load('scaler.pkl') # Ensure the scaler is saved during training and available here
|
| 11 |
+
except Exception as e:
|
| 12 |
+
raise RuntimeError(f"Failed to load model or scaler: {e}")
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
|
|
|
| 43 |
# Define the prediction endpoint
|
| 44 |
@app.post("/predict/", response_model=PCOSPrediction)
|
| 45 |
def predict(data: PCOSInput):
|
| 46 |
+
try:
|
| 47 |
+
# Convert input data to array
|
| 48 |
+
input_data = np.array([[data.Follicle_No_R, data.Follicle_No_L, data.Skin_darkening, data.hair_growth,
|
| 49 |
+
data.Weight_gain, data.Cycle_length, data.AMH, data.Fast_food, data.Cycle_R_I,
|
| 50 |
+
data.FSH_LH, data.PRL, data.Pimples, data.Age, data.BMI]])
|
| 51 |
+
|
| 52 |
+
# Scale the input data using the loaded scaler
|
| 53 |
+
scaled_input = scaler.transform(input_data)
|
| 54 |
+
|
| 55 |
+
# Make prediction
|
| 56 |
+
prediction = model.predict(scaled_input)
|
| 57 |
+
probability = model.predict_proba(scaled_input)[0][1] # Probability for class 1 (PCOS)
|
| 58 |
+
|
| 59 |
+
return PCOSPrediction(prediction=int(prediction[0]), probability=probability)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
raise HTTPException(status_code=500, detail=f"Prediction error: {e}")
|