surbi karki commited on
Commit
716d566
·
verified ·
1 Parent(s): e605432

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -1,11 +1,15 @@
1
- from fastapi import FastAPI
 
2
  from pydantic import BaseModel
3
  import joblib
4
  import numpy as np
5
 
6
  # Load the trained model and scaler
7
- model = joblib.load('LR1.pkl') # Replace with the path to your saved model
8
- scaler = joblib.load('scaler.pkl') # Ensure the scaler is saved during training and available here
 
 
 
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
- # Convert input data to array
43
- input_data = np.array([[data.Follicle_No_R, data.Follicle_No_L, data.Skin_darkening, data.hair_growth,
44
- data.Weight_gain, data.Cycle_length, data.AMH, data.Fast_food, data.Cycle_R_I,
45
- data.FSH_LH, data.PRL, data.Pimples, data.Age, data.BMI]])
46
-
47
- # Scale the input data using the loaded scaler
48
- scaled_input = scaler.transform(input_data)
49
-
50
- # Make prediction
51
- prediction = model.predict(scaled_input)
52
- probability = model.predict_proba(scaled_input)[0][1] # Probability for class 1 (PCOS)
53
-
54
- return PCOSPrediction(prediction=int(prediction[0]), probability=probability)
 
 
 
 
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}")