Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,7 +32,7 @@ print("Downloading model from Hugging Face Hub...")
|
|
| 32 |
model_path = hf_hub_download(
|
| 33 |
repo_id=MODEL_REPO_ID,
|
| 34 |
filename=MODEL_FILENAME,
|
| 35 |
-
cache_dir=os.environ['HF_HOME']
|
| 36 |
)
|
| 37 |
model = joblib.load(model_path)
|
| 38 |
print("Model loaded successfully.")
|
|
@@ -61,7 +61,65 @@ def read_root():
|
|
| 61 |
@app.post("/predict")
|
| 62 |
def predict(data: PatientData):
|
| 63 |
try:
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
probabilities = model.predict_proba(input_df)[0]
|
| 66 |
confidence_high_risk = probabilities[0]
|
| 67 |
risk_level = "High Risk of Non-Survival" if confidence_high_risk > 0.5 else "Low Risk of Non-Survival"
|
|
@@ -71,4 +129,5 @@ def predict(data: PatientData):
|
|
| 71 |
"confidence": f"{confidence_high_risk * 100:.1f}%"
|
| 72 |
}
|
| 73 |
except Exception as e:
|
| 74 |
-
|
|
|
|
|
|
| 32 |
model_path = hf_hub_download(
|
| 33 |
repo_id=MODEL_REPO_ID,
|
| 34 |
filename=MODEL_FILENAME,
|
| 35 |
+
cache_dir=os.environ['HF_HOME']
|
| 36 |
)
|
| 37 |
model = joblib.load(model_path)
|
| 38 |
print("Model loaded successfully.")
|
|
|
|
| 61 |
@app.post("/predict")
|
| 62 |
def predict(data: PatientData):
|
| 63 |
try:
|
| 64 |
+
# Define the exact list of features your model expects
|
| 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 |
"confidence": f"{confidence_high_risk * 100:.1f}%"
|
| 130 |
}
|
| 131 |
except Exception as e:
|
| 132 |
+
# Return a detailed error message for better debugging
|
| 133 |
+
return {"error": str(e), "input_data_received": data.dict()}
|