Spaces:
Sleeping
Sleeping
| import os | |
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| from train_model import train_and_save_model, MODEL_PATH, LABEL_ENCODER_PATH | |
| if not (os.path.exists(MODEL_PATH) and os.path.exists(LABEL_ENCODER_PATH)): | |
| train_and_save_model() | |
| model = joblib.load(MODEL_PATH) | |
| le = joblib.load(LABEL_ENCODER_PATH) | |
| def predict_risk(temperature, duration): | |
| # Create a DataFrame with the same feature names used in training | |
| input_df = pd.DataFrame([[temperature, duration]], columns=['Max_Temperature', 'Duration']) | |
| pred_encoded = model.predict(input_df)[0] | |
| pred_proba = model.predict_proba(input_df)[0] | |
| pred_label = le.inverse_transform([pred_encoded])[0] | |
| confidence = pred_proba[pred_encoded] * 100 | |
| return pred_label, confidence |