Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,21 +37,45 @@ model_path = hf_hub_download(
|
|
| 37 |
model = joblib.load(model_path)
|
| 38 |
print("Model loaded successfully.")
|
| 39 |
|
| 40 |
-
# --- Define Input Data Model ---
|
| 41 |
-
|
|
|
|
| 42 |
age: float
|
| 43 |
-
gender: str
|
| 44 |
-
country: str
|
| 45 |
-
cancer_stage: str
|
| 46 |
-
family_history: int
|
| 47 |
-
smoking_status: str
|
| 48 |
bmi: float
|
| 49 |
cholesterol_level: float
|
| 50 |
hypertension: int
|
| 51 |
asthma: int
|
| 52 |
cirrhosis: int
|
| 53 |
other_cancer: int
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# --- API Endpoints ---
|
| 57 |
@app.get("/")
|
|
@@ -59,67 +83,9 @@ def read_root():
|
|
| 59 |
return {"message": "Welcome to the PulmoProbe AI API"}
|
| 60 |
|
| 61 |
@app.post("/predict")
|
| 62 |
-
def predict(data:
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
feature_order = [
|
| 66 |
-
'age', 'bmi', 'cholesterol_level', 'hypertension', 'asthma', 'cirrhosis',
|
| 67 |
-
'other_cancer', 'gender_Female', 'gender_Male',
|
| 68 |
-
'country_Belgium', 'country_Croatia', 'country_Denmark', 'country_Finland',
|
| 69 |
-
'country_France', 'country_Germany', 'country_Hungary', 'country_Italy',
|
| 70 |
-
'country_Netherlands', 'country_Slovakia', 'country_Spain', 'country_Sweden',
|
| 71 |
-
'cancer_stage_Stage I', 'cancer_stage_Stage II', 'cancer_stage_Stage III',
|
| 72 |
-
'cancer_stage_Stage IV',
|
| 73 |
-
'family_history_Yes',
|
| 74 |
-
'smoking_status_Current Smoker', 'smoking_status_Former Smoker',
|
| 75 |
-
'smoking_status_Never Smoked', 'smoking_status_Passive Smoker',
|
| 76 |
-
'treatment_type_Chemotherapy', 'treatment_type_Combined',
|
| 77 |
-
'treatment_type_Radiation', 'treatment_type_Surgery'
|
| 78 |
-
]
|
| 79 |
-
|
| 80 |
-
# Convert the Pydantic model to a dictionary
|
| 81 |
-
input_data = data.dict()
|
| 82 |
-
|
| 83 |
-
# Initialize an empty dictionary for one-hot encoded features
|
| 84 |
-
encoded_data = {feature: 0 for feature in feature_order}
|
| 85 |
-
|
| 86 |
-
# Map original data to the one-hot encoded format
|
| 87 |
-
encoded_data['age'] = input_data['age']
|
| 88 |
-
encoded_data['bmi'] = input_data['bmi']
|
| 89 |
-
encoded_data['cholesterol_level'] = input_data['cholesterol_level']
|
| 90 |
-
encoded_data['hypertension'] = input_data['hypertension']
|
| 91 |
-
encoded_data['asthma'] = input_data['asthma']
|
| 92 |
-
encoded_data['cirrhosis'] = input_data['cirrhosis']
|
| 93 |
-
encoded_data['other_cancer'] = input_data['other_cancer']
|
| 94 |
-
|
| 95 |
-
# One-hot encode categorical features
|
| 96 |
-
gender_key = f"gender_{input_data['gender']}"
|
| 97 |
-
if gender_key in encoded_data:
|
| 98 |
-
encoded_data[gender_key] = 1
|
| 99 |
-
|
| 100 |
-
country_key = f"country_{input_data['country']}"
|
| 101 |
-
if country_key in encoded_data:
|
| 102 |
-
encoded_data[country_key] = 1
|
| 103 |
-
|
| 104 |
-
cancer_stage_key = f"cancer_stage_{input_data['cancer_stage']}"
|
| 105 |
-
if cancer_stage_key in encoded_data:
|
| 106 |
-
encoded_data[cancer_stage_key] = 1
|
| 107 |
-
|
| 108 |
-
smoking_status_key = f"smoking_status_{input_data['smoking_status']}"
|
| 109 |
-
if smoking_status_key in encoded_data:
|
| 110 |
-
encoded_data[smoking_status_key] = 1
|
| 111 |
-
|
| 112 |
-
treatment_type_key = f"treatment_type_{input_data['treatment_type']}"
|
| 113 |
-
if treatment_type_key in encoded_data:
|
| 114 |
-
encoded_data[treatment_type_key] = 1
|
| 115 |
-
|
| 116 |
-
# Family history is already 0/1, just rename the key
|
| 117 |
-
encoded_data['family_history_Yes'] = input_data['family_history']
|
| 118 |
-
|
| 119 |
-
# Create the DataFrame with the correct order
|
| 120 |
-
input_df = pd.DataFrame([encoded_data])
|
| 121 |
-
|
| 122 |
-
# Make the prediction
|
| 123 |
probabilities = model.predict_proba(input_df)[0]
|
| 124 |
confidence_high_risk = probabilities[0]
|
| 125 |
risk_level = "High Risk of Non-Survival" if confidence_high_risk > 0.5 else "Low Risk of Non-Survival"
|
|
@@ -129,5 +95,4 @@ def predict(data: PatientData):
|
|
| 129 |
"confidence": f"{confidence_high_risk * 100:.1f}%"
|
| 130 |
}
|
| 131 |
except Exception as e:
|
| 132 |
-
|
| 133 |
-
return {"error": str(e), "input_data_received": data.dict()}
|
|
|
|
| 37 |
model = joblib.load(model_path)
|
| 38 |
print("Model loaded successfully.")
|
| 39 |
|
| 40 |
+
# --- Define Input Data Model for one-hot encoded features ---
|
| 41 |
+
# This new model directly matches the one-hot encoded data from the frontend
|
| 42 |
+
class OneHotPatientData(BaseModel):
|
| 43 |
age: float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
bmi: float
|
| 45 |
cholesterol_level: float
|
| 46 |
hypertension: int
|
| 47 |
asthma: int
|
| 48 |
cirrhosis: int
|
| 49 |
other_cancer: int
|
| 50 |
+
family_history_Yes: int
|
| 51 |
+
gender_Male: int
|
| 52 |
+
gender_Female: int
|
| 53 |
+
country_Sweden: int
|
| 54 |
+
country_Netherlands: int
|
| 55 |
+
country_Hungary: int
|
| 56 |
+
country_Belgium: int
|
| 57 |
+
country_Italy: int
|
| 58 |
+
country_Croatia: int
|
| 59 |
+
country_Denmark: int
|
| 60 |
+
country_Germany: int
|
| 61 |
+
country_France: int
|
| 62 |
+
country_Slovakia: int
|
| 63 |
+
country_Finland: int
|
| 64 |
+
country_Spain: int
|
| 65 |
+
country_UnitedKingdom: int
|
| 66 |
+
country_UnitedStates: int
|
| 67 |
+
cancer_stage_StageI: int
|
| 68 |
+
cancer_stage_StageII: int
|
| 69 |
+
cancer_stage_StageIII: int
|
| 70 |
+
cancer_stage_StageIV: int
|
| 71 |
+
smoking_status_NeverSmoked: int
|
| 72 |
+
smoking_status_FormerSmoker: int
|
| 73 |
+
smoking_status_PassiveSmoker: int
|
| 74 |
+
smoking_status_CurrentSmoker: int
|
| 75 |
+
treatment_type_Chemotherapy: int
|
| 76 |
+
treatment_type_Surgery: int
|
| 77 |
+
treatment_type_Radiation: int
|
| 78 |
+
treatment_type_Combined: int
|
| 79 |
|
| 80 |
# --- API Endpoints ---
|
| 81 |
@app.get("/")
|
|
|
|
| 83 |
return {"message": "Welcome to the PulmoProbe AI API"}
|
| 84 |
|
| 85 |
@app.post("/predict")
|
| 86 |
+
def predict(data: OneHotPatientData):
|
| 87 |
try:
|
| 88 |
+
input_df = pd.DataFrame([data.dict()])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
probabilities = model.predict_proba(input_df)[0]
|
| 90 |
confidence_high_risk = probabilities[0]
|
| 91 |
risk_level = "High Risk of Non-Survival" if confidence_high_risk > 0.5 else "Low Risk of Non-Survival"
|
|
|
|
| 95 |
"confidence": f"{confidence_high_risk * 100:.1f}%"
|
| 96 |
}
|
| 97 |
except Exception as e:
|
| 98 |
+
return {"error": str(e)}
|
|
|