Spaces:
Runtime error
Runtime error
Download backend/inference.py from IDKHowToCodeFr/tinyml-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/IDKHowToCodeFr/tinyml-backend/resolve/67e143daa0ae14e3e902e362fbffbf7ec53fc36f/backend/inference.py
- Command line
-
hf download hf://spaces/IDKHowToCodeFr/tinyml-backend@67e143daa0ae14e3e902e362fbffbf7ec53fc36f/backend/inference.py
-
curl -L -o inference.py https://huggingface.co/spaces/IDKHowToCodeFr/tinyml-backend/resolve/67e143daa0ae14e3e902e362fbffbf7ec53fc36f/backend/inference.py
1.71 kB
| from typing import Dict, Any | |
| import pandas as pd | |
| import numpy as np | |
| from preprocessing import preprocess_data | |
| def evaluate(ensemble_model, data) -> Dict[str, Any]: | |
| # Domain orchestrator: PatientData -> InferenceResult | |
| raw_df = pd.DataFrame([data.model_dump(by_alias=True)]) | |
| # Mapping properties to expected feature names | |
| raw_df = raw_df.rename(columns={ | |
| "Heart_Rate": "Heart Rate (bpm)", | |
| "SpO2_Level": "SpO2 Level (%)", | |
| "Systolic_BP": "Systolic Blood Pressure (mmHg)", | |
| "Diastolic_BP": "Diastolic Blood Pressure (mmHg)", | |
| "Body_Temp": "Body Temperature (°C)", | |
| "Fall_Detection": "Fall Detection" | |
| }) | |
| # Drop anything the model hasn't seen | |
| unseen = ["Physical Activity Level", "Age", "Gender", "Stress Level", "Cholesterol Level (mg/dL)", | |
| "Physical_Activity_Level", "Stress_Level", "Cholesterol_Level", "Fall_Detection"] | |
| for f in unseen: | |
| if f in raw_df.columns: | |
| raw_df = raw_df.drop(columns=[f]) | |
| # Feature engineering | |
| processed_df, _ = preprocess_data(raw_df, is_training=False) | |
| if ensemble_model is None: | |
| return {"error": "Ensemble model not loaded"} | |
| # Ensemble Prediction | |
| disease_label, conf, ind_preds, class_probs, weights, ind_conf = ensemble_model.predict(processed_df) | |
| is_at_risk = (disease_label.lower() not in ["healthy", "normal", "none"]) | |
| return { | |
| "prediction": is_at_risk, | |
| "prediction_label": disease_label, | |
| "probability": float(conf), | |
| "ensemble_prediction": is_at_risk, | |
| "model_outputs": ind_preds, | |
| "model_probs": ind_conf, | |
| "disease_probs": class_probs, | |
| "weights": weights | |
| } | |