File size: 3,184 Bytes
716d566
465b08b
40c7f6b
 
 
 
716d566
465b08b
 
716d566
 
40c7f6b
 
 
7ef829f
 
 
 
 
40c7f6b
 
465b08b
 
 
 
 
 
 
 
 
 
 
 
 
 
40c7f6b
 
 
 
 
 
 
 
 
716d566
de77399
 
 
 
 
465b08b
 
 
 
716d566
 
 
 
 
 
 
 
 
465b08b
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import joblib
import numpy as np

# Load the trained model and scaler
try:
    model = joblib.load('LR1.pkl')  # Ensure model path is correct
    scaler = joblib.load('scaler1.pkl')  # Ensure scaler is saved properly
except Exception as e:
    raise RuntimeError(f"Failed to load model or scaler: {e}")

app = FastAPI()

# Root endpoint
@app.get("/")
def read_root():
    return {"message": "Welcome to the PCOS Prediction API!"}

# Define the input data structure
class PCOSInput(BaseModel):
    Follicle_No_R: int = Field(..., ge=0, description="Number of follicles in the right ovary (>=0)")
    Follicle_No_L: int = Field(..., ge=0, description="Number of follicles in the left ovary (>=0)")
    Skin_darkening: int = Field(..., ge=0, le=1, description="Skin darkening (1: Yes, 0: No)")
    hair_growth: int = Field(..., ge=0, le=1, description="Hair growth (1: Yes, 0: No)")
    Weight_gain: int = Field(..., ge=0, le=1, description="Weight gain (1: Yes, 0: No)")
    Cycle_length: int = Field(..., ge=1, description="Menstrual cycle length in days (>=1)")
    AMH: float = Field(..., gt=0, description="Anti-Müllerian Hormone level (positive float)")
    Fast_food: int = Field(..., ge=0, le=1, description="Fast food consumption (1: Yes, 0: No)")
    Cycle_R_I: int = Field(..., ge=0, le=1, description="Regular/Irregular cycle (1: Regular, 0: Irregular)")
    FSH_LH: float = Field(..., gt=0, description="FSH/LH ratio (positive float)")
    PRL: float = Field(..., gt=0, description="Prolactin level (positive float)")
    Pimples: int = Field(..., ge=0, le=1, description="Pimples (1: Yes, 0: No)")
    Age: int = Field(..., ge=12, le=60, description="Age in years (between 12 and 60)")
    BMI: float = Field(..., gt=0, description="Body Mass Index (positive float)")

# Define the prediction response structure
class PCOSPrediction(BaseModel):
    prediction: int
    probability: float

# Define the prediction endpoint
@app.post("/predict/", response_model=PCOSPrediction)
def predict(data: PCOSInput):
    try:
        # Convert input data to array
        input_data = np.array([[data.Follicle_No_R, data.Follicle_No_L, data.Skin_darkening, data.hair_growth,
                                data.Weight_gain, data.Cycle_length, data.AMH, data.Fast_food, data.Cycle_R_I,
                                data.FSH_LH, data.PRL, data.Pimples, data.Age, data.BMI]])
        
        # Ensure scaler and model work with the same feature shape
        if input_data.shape[1] != scaler.n_features_in_:
            raise ValueError(f"Input data shape mismatch. Expected {scaler.n_features_in_}, got {input_data.shape[1]}")

        # Scale the input data using the loaded scaler
        scaled_input = scaler.transform(input_data)

        # Make prediction
        prediction = model.predict(scaled_input)
        probability = model.predict_proba(scaled_input)[0][1]  # Probability for class 1 (PCOS)

        return PCOSPrediction(prediction=int(prediction[0]), probability=probability)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Prediction error: {e}")