Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ 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
|
|
@@ -71,55 +72,83 @@ def _human_label(c):
|
|
| 71 |
|
| 72 |
# This functions takes all of our features, encodes this accordingly, and performs a predictions
|
| 73 |
def do_predict(phone_hours, computer_hours, device_count, sleep_quality_label, sleep_time, sleep_hours):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
FEATURE_COLS[4]: int(sleep_time),
|
| 83 |
-
FEATURE_COLS[5]: float(sleep_hours),
|
| 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 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
key = _human_label(cls)
|
| 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),
|
| 115 |
-
}])
|
| 116 |
-
|
| 117 |
-
md = f"**Prediction:** {pred_label}"
|
| 118 |
-
if proba_dict:
|
| 119 |
-
md += f" \n**Confidence:** {round(proba_dict.get(pred_label, 0.0) * 100, 2)}%"
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
return proba_dict
|
| 123 |
|
| 124 |
# Representative examples (these will need to be updated based on the new model's features)
|
| 125 |
# These examples are placeholders and should be replaced with actual examples from the dataset if available
|
|
@@ -134,9 +163,9 @@ EXAMPLES = [
|
|
| 134 |
with gradio.Blocks() as demo:
|
| 135 |
# Provide an introduction
|
| 136 |
gradio.Markdown("# Device Use Before Sleep Predictor")
|
| 137 |
-
gradio.Markdown("
|
| 138 |
-
|
| 139 |
-
|
| 140 |
|
| 141 |
with gradio.Row():
|
| 142 |
phone_hours = gradio.Slider(0, 10, step=0.1, value=2.0, label=FEATURE_COLS[0])
|
|
|
|
| 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 |
+
import sklearn # Import sklearn to check version
|
| 11 |
|
| 12 |
# Settings
|
| 13 |
MODEL_REPO_ID = "jennifee/classical_automl_model" # Updated to the correct model repo
|
|
|
|
| 72 |
|
| 73 |
# This functions takes all of our features, encodes this accordingly, and performs a predictions
|
| 74 |
def do_predict(phone_hours, computer_hours, device_count, sleep_quality_label, sleep_time, sleep_hours):
|
| 75 |
+
print("Received inputs:")
|
| 76 |
+
print(f" phone_hours: {phone_hours}")
|
| 77 |
+
print(f" computer_hours: {computer_hours}")
|
| 78 |
+
print(f" device_count: {device_count}")
|
| 79 |
+
print(f" sleep_quality_label: {sleep_quality_label}")
|
| 80 |
+
print(f" sleep_time: {sleep_time}")
|
| 81 |
+
print(f" sleep_hours: {sleep_hours}")
|
| 82 |
+
print(f" sklearn version: {sklearn.__version__}") # Print sklearn version
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
try:
|
| 86 |
+
# Encode categorical features
|
| 87 |
+
sleep_quality_code = SLEEP_QUALITY_MAP[sleep_quality_label]
|
| 88 |
+
|
| 89 |
+
row = {
|
| 90 |
+
FEATURE_COLS[0]: float(phone_hours),
|
| 91 |
+
FEATURE_COLS[1]: float(computer_hours),
|
| 92 |
+
FEATURE_COLS[2]: int(device_count),
|
| 93 |
+
FEATURE_COLS[3]: sleep_quality_code,
|
| 94 |
+
FEATURE_COLS[4]: int(sleep_time),
|
| 95 |
+
FEATURE_COLS[5]: float(sleep_hours),
|
| 96 |
+
}
|
| 97 |
+
X = pandas.DataFrame([row], columns=FEATURE_COLS)
|
| 98 |
+
|
| 99 |
+
print("Input DataFrame (X):")
|
| 100 |
+
print(X)
|
| 101 |
+
|
| 102 |
+
pred_series = PREDICTOR.predict(X)
|
| 103 |
+
raw_pred = pred_series.iloc[0]
|
| 104 |
+
|
| 105 |
+
print("Raw prediction (pred_series):")
|
| 106 |
+
print(pred_series)
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
proba = PREDICTOR.predict_proba(X)
|
| 110 |
+
if isinstance(proba, pandas.Series):
|
| 111 |
+
proba = proba.to_frame().T
|
| 112 |
+
print("Prediction probabilities (proba):")
|
| 113 |
+
print(proba)
|
| 114 |
+
except Exception as e:
|
| 115 |
+
proba = None
|
| 116 |
+
print(f"Error getting prediction probabilities: {e}")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
pred_label = _human_label(raw_pred)
|
| 120 |
+
|
| 121 |
+
proba_dict = None
|
| 122 |
+
if proba is not None:
|
| 123 |
+
row0 = proba.iloc[0]
|
| 124 |
+
tmp = {}
|
| 125 |
+
for cls, val in row0.items():
|
| 126 |
+
key = _human_label(cls)
|
| 127 |
+
tmp[key] = float(val) + float(tmp.get(key, 0.0))
|
| 128 |
+
proba_dict = dict(sorted(tmp.items(), key=lambda kv: kv[1], reverse=True))
|
| 129 |
+
print("Probability dictionary (proba_dict):")
|
| 130 |
+
print(proba_dict)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
df_out = pandas.DataFrame([{
|
| 134 |
+
"Predicted outcome": pred_label,
|
| 135 |
+
"Confidence (%)": round((proba_dict.get(pred_label, 1.0) if proba_dict else 1.0) * 100, 2),
|
| 136 |
+
}])
|
| 137 |
+
|
| 138 |
+
md = f"**Prediction:** {pred_label}"
|
| 139 |
+
if proba_dict:
|
| 140 |
+
md += f"
|
| 141 |
+
**Confidence:** {round(proba_dict.get(pred_label, 0.0) * 100, 2)}%"
|
| 142 |
+
print("Markdown output (md):")
|
| 143 |
+
print(md)
|
| 144 |
+
|
| 145 |
+
return proba_dict
|
| 146 |
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print(f"An error occurred during prediction: {e}")
|
| 149 |
+
import traceback
|
| 150 |
+
traceback.print_exc()
|
| 151 |
+
return None # Return None or an empty dictionary in case of an error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
# Representative examples (these will need to be updated based on the new model's features)
|
| 154 |
# These examples are placeholders and should be replaced with actual examples from the dataset if available
|
|
|
|
| 163 |
with gradio.Blocks() as demo:
|
| 164 |
# Provide an introduction
|
| 165 |
gradio.Markdown("# Device Use Before Sleep Predictor")
|
| 166 |
+
gradio.Markdown("
|
| 167 |
+
This app predicts whether a student uses their device before sleep based on their device usage and sleeping habits.
|
| 168 |
+
")
|
| 169 |
|
| 170 |
with gradio.Row():
|
| 171 |
phone_hours = gradio.Slider(0, 10, step=0.1, value=2.0, label=FEATURE_COLS[0])
|