kevinkyi commited on
Commit
e5a3af6
·
verified ·
1 Parent(s): 2873993

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -13,8 +13,6 @@ def _prepare_predictor_dir() -> str:
13
  repo_id=MODEL_REPO_ID,
14
  filename=ZIP_FILENAME,
15
  repo_type="model",
16
- # do NOT pass local_dir_use_symlinks (deprecated)
17
- # let HF pick the cache dir automatically
18
  )
19
  workdir = tempfile.mkdtemp(prefix="ag_predictor_")
20
  with zipfile.ZipFile(local_zip, "r") as zf:
@@ -25,13 +23,39 @@ def _prepare_predictor_dir() -> str:
25
  return workdir
26
 
27
  PREDICTOR_DIR = _prepare_predictor_dir()
28
-
29
- # Match the version the predictor was saved with (1.4.0) -> we'll enforce version match
30
  PREDICTOR = ag.TabularPredictor.load(PREDICTOR_DIR, require_py_version_match=False, require_version_match=True)
31
 
32
  FEATURE_COLS = ["phone_hours","computer_hours","device_count","sleep_quality","sleep_time","sleep_hours"]
33
  LABEL_MAP = {0: "No (does not use phone before bed)", 1: "Yes (uses phone before bed)"}
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def do_predict(phone_hours, computer_hours, device_count, sleep_quality, sleep_time, sleep_hours):
36
  row = {
37
  "phone_hours": int(phone_hours),
@@ -44,13 +68,12 @@ def do_predict(phone_hours, computer_hours, device_count, sleep_quality, sleep_t
44
  X = pd.DataFrame([row], columns=FEATURE_COLS)
45
 
46
  try:
47
- proba = PREDICTOR.predict_proba(X)
48
- if isinstance(proba, pd.Series):
49
- proba = proba.to_frame().T
50
  row0 = proba.iloc[0]
51
  out = {LABEL_MAP.get(cls, str(cls)): float(val) for cls, val in row0.items()}
52
  out = dict(sorted(out.items(), key=lambda kv: kv[1], reverse=True))
53
  except Exception:
 
54
  pred = PREDICTOR.predict(X).iloc[0]
55
  out = {LABEL_MAP.get(pred, str(pred)): 1.0}
56
  return out
 
13
  repo_id=MODEL_REPO_ID,
14
  filename=ZIP_FILENAME,
15
  repo_type="model",
 
 
16
  )
17
  workdir = tempfile.mkdtemp(prefix="ag_predictor_")
18
  with zipfile.ZipFile(local_zip, "r") as zf:
 
23
  return workdir
24
 
25
  PREDICTOR_DIR = _prepare_predictor_dir()
 
 
26
  PREDICTOR = ag.TabularPredictor.load(PREDICTOR_DIR, require_py_version_match=False, require_version_match=True)
27
 
28
  FEATURE_COLS = ["phone_hours","computer_hours","device_count","sleep_quality","sleep_time","sleep_hours"]
29
  LABEL_MAP = {0: "No (does not use phone before bed)", 1: "Yes (uses phone before bed)"}
30
 
31
+ def _non_fastai_models():
32
+ # Filter out potential FastAI/NN models by name to avoid importing fastai if not installed
33
+ try:
34
+ names = PREDICTOR.get_model_names()
35
+ names_upper = {n: n.upper() for n in names}
36
+ keep = [n for n in names if ("FASTAI" not in names_upper[n]) and ("NN" not in names_upper[n])]
37
+ return keep if keep else names
38
+ except Exception:
39
+ return None
40
+
41
+ def _predict_probs_with_fallback(X: pd.DataFrame):
42
+
43
+ try:
44
+ proba = PREDICTOR.predict_proba(X)
45
+ if isinstance(proba, pd.Series):
46
+ proba = proba.to_frame().T
47
+ return proba
48
+ except ModuleNotFoundError as e:
49
+ # If fastai is missing, try excluding those models
50
+ if "fastai" in str(e).lower():
51
+ model_list = _non_fastai_models()
52
+ if model_list:
53
+ proba = PREDICTOR.predict_proba(X, model=model_list)
54
+ if isinstance(proba, pd.Series):
55
+ proba = proba.to_frame().T
56
+ return proba
57
+ raise # bubble up any other error
58
+
59
  def do_predict(phone_hours, computer_hours, device_count, sleep_quality, sleep_time, sleep_hours):
60
  row = {
61
  "phone_hours": int(phone_hours),
 
68
  X = pd.DataFrame([row], columns=FEATURE_COLS)
69
 
70
  try:
71
+ proba = _predict_probs_with_fallback(X)
 
 
72
  row0 = proba.iloc[0]
73
  out = {LABEL_MAP.get(cls, str(cls)): float(val) for cls, val in row0.items()}
74
  out = dict(sorted(out.items(), key=lambda kv: kv[1], reverse=True))
75
  except Exception:
76
+ # Ultimate fallback: hard label via best available model(s)
77
  pred = PREDICTOR.predict(X).iloc[0]
78
  out = {LABEL_MAP.get(pred, str(pred)): 1.0}
79
  return out