Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
|
| 2 |
-
import os
|
| 3 |
-
import shutil
|
| 4 |
-
import zipfile
|
| 5 |
-
import pathlib
|
| 6 |
-
import pandas
|
| 7 |
-
import gradio
|
| 8 |
-
import huggingface_hub
|
| 9 |
-
import autogluon.tabular
|
| 10 |
|
| 11 |
# Settings
|
| 12 |
-
MODEL_REPO_ID = "jennifee/classical_automl_model"
|
| 13 |
-
ZIP_FILENAME = "autogluon_predictor_dir.zip"
|
| 14 |
CACHE_DIR = pathlib.Path("hf_assets")
|
| 15 |
-
EXTRACT_DIR = CACHE_DIR / "predictor_native_sleep"
|
| 16 |
|
| 17 |
# Feature column names and target column names based on the provided data structure
|
| 18 |
FEATURE_COLS = [
|
|
@@ -23,12 +23,13 @@ FEATURE_COLS = [
|
|
| 23 |
"sleep_time",
|
| 24 |
"sleep_hours",
|
| 25 |
]
|
| 26 |
-
TARGET_COL = "use_before_bed"
|
| 27 |
|
| 28 |
-
# Encoding for sleep quality
|
|
|
|
| 29 |
SLEEP_QUALITY_MAP = {"Poor": 0, "Fair": 1, "Good": 2, "Excellent": 3}
|
| 30 |
|
| 31 |
-
# Encoding for outcome
|
| 32 |
OUTCOME_LABELS = {
|
| 33 |
0: "Does not use device before bed",
|
| 34 |
1: "Uses device before bed",
|
|
@@ -83,16 +84,19 @@ def do_predict(phone_hours, computer_hours, device_count, sleep_quality_label, s
|
|
| 83 |
}
|
| 84 |
X = pandas.DataFrame([row], columns=FEATURE_COLS)
|
| 85 |
|
|
|
|
| 86 |
pred_series = PREDICTOR.predict(X)
|
| 87 |
raw_pred = pred_series.iloc[0]
|
| 88 |
|
|
|
|
| 89 |
try:
|
| 90 |
proba = PREDICTOR.predict_proba(X)
|
| 91 |
if isinstance(proba, pandas.Series):
|
| 92 |
proba = proba.to_frame().T
|
| 93 |
-
except Exception:
|
| 94 |
proba = None
|
| 95 |
|
|
|
|
| 96 |
pred_label = _human_label(raw_pred)
|
| 97 |
|
| 98 |
proba_dict = None
|
|
@@ -104,6 +108,7 @@ def do_predict(phone_hours, computer_hours, device_count, sleep_quality_label, s
|
|
| 104 |
tmp[key] = float(val) + float(tmp.get(key, 0.0))
|
| 105 |
proba_dict = dict(sorted(tmp.items(), key=lambda kv: kv[1], reverse=True))
|
| 106 |
|
|
|
|
| 107 |
df_out = pandas.DataFrame([{
|
| 108 |
"Predicted outcome": pred_label,
|
| 109 |
"Confidence (%)": round((proba_dict.get(pred_label, 1.0) if proba_dict else 1.0) * 100, 2),
|
|
@@ -114,9 +119,11 @@ def do_predict(phone_hours, computer_hours, device_count, sleep_quality_label, s
|
|
| 114 |
md += f"
|
| 115 |
**Confidence:** {round(proba_dict.get(pred_label, 0.0) * 100, 2)}%"
|
| 116 |
|
|
|
|
| 117 |
return proba_dict
|
| 118 |
|
| 119 |
-
# Representative examples
|
|
|
|
| 120 |
EXAMPLES = [
|
| 121 |
[2.0, 3.0, 3, "Good", 2200, 8.0],
|
| 122 |
[5.0, 6.0, 5, "Fair", 100, 6.0],
|
|
|
|
| 1 |
|
| 2 |
+
import os # For filesystem operations
|
| 3 |
+
import shutil # For directory cleanup
|
| 4 |
+
import zipfile # For extracting model archives
|
| 5 |
+
import pathlib # For path manipulations
|
| 6 |
+
import pandas # For tabular data handling
|
| 7 |
+
import gradio # For interactive UI
|
| 8 |
+
import huggingface_hub # For downloading model assets
|
| 9 |
+
import autogluon.tabular # For loading and running AutoGluon predictors
|
| 10 |
|
| 11 |
# Settings
|
| 12 |
+
MODEL_REPO_ID = "jennifee/classical_automl_model" # Updated to the correct model repo
|
| 13 |
+
ZIP_FILENAME = "autogluon_predictor_dir.zip" # Assuming the zip filename is the same
|
| 14 |
CACHE_DIR = pathlib.Path("hf_assets")
|
| 15 |
+
EXTRACT_DIR = CACHE_DIR / "predictor_native_sleep" # Changed extract directory name
|
| 16 |
|
| 17 |
# Feature column names and target column names based on the provided data structure
|
| 18 |
FEATURE_COLS = [
|
|
|
|
| 23 |
"sleep_time",
|
| 24 |
"sleep_hours",
|
| 25 |
]
|
| 26 |
+
TARGET_COL = "use_before_bed" # Assuming this is the target based on previous context
|
| 27 |
|
| 28 |
+
# Encoding for sleep quality (assuming a categorical mapping is needed for the model)
|
| 29 |
+
# This mapping is an example and may need adjustment based on the actual values in the dataset
|
| 30 |
SLEEP_QUALITY_MAP = {"Poor": 0, "Fair": 1, "Good": 2, "Excellent": 3}
|
| 31 |
|
| 32 |
+
# Encoding for outcome (assuming binary classification for use_before_bed)
|
| 33 |
OUTCOME_LABELS = {
|
| 34 |
0: "Does not use device before bed",
|
| 35 |
1: "Uses device before bed",
|
|
|
|
| 84 |
}
|
| 85 |
X = pandas.DataFrame([row], columns=FEATURE_COLS)
|
| 86 |
|
| 87 |
+
|
| 88 |
pred_series = PREDICTOR.predict(X)
|
| 89 |
raw_pred = pred_series.iloc[0]
|
| 90 |
|
| 91 |
+
|
| 92 |
try:
|
| 93 |
proba = PREDICTOR.predict_proba(X)
|
| 94 |
if isinstance(proba, pandas.Series):
|
| 95 |
proba = proba.to_frame().T
|
| 96 |
+
except Exception as e:
|
| 97 |
proba = None
|
| 98 |
|
| 99 |
+
|
| 100 |
pred_label = _human_label(raw_pred)
|
| 101 |
|
| 102 |
proba_dict = None
|
|
|
|
| 108 |
tmp[key] = float(val) + float(tmp.get(key, 0.0))
|
| 109 |
proba_dict = dict(sorted(tmp.items(), key=lambda kv: kv[1], reverse=True))
|
| 110 |
|
| 111 |
+
|
| 112 |
df_out = pandas.DataFrame([{
|
| 113 |
"Predicted outcome": pred_label,
|
| 114 |
"Confidence (%)": round((proba_dict.get(pred_label, 1.0) if proba_dict else 1.0) * 100, 2),
|
|
|
|
| 119 |
md += f"
|
| 120 |
**Confidence:** {round(proba_dict.get(pred_label, 0.0) * 100, 2)}%"
|
| 121 |
|
| 122 |
+
|
| 123 |
return proba_dict
|
| 124 |
|
| 125 |
+
# Representative examples (these will need to be updated based on the new model's features)
|
| 126 |
+
# These examples are placeholders and should be replaced with actual examples from the dataset if available
|
| 127 |
EXAMPLES = [
|
| 128 |
[2.0, 3.0, 3, "Good", 2200, 8.0],
|
| 129 |
[5.0, 6.0, 5, "Fair", 100, 6.0],
|