aslan-ng commited on
Commit
a4d358d
·
verified ·
1 Parent(s): f566862

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -46,36 +46,34 @@ PREDICTOR_DIR = _prepare_predictor_dir()
46
  PREDICTOR = autogluon.tabular.TabularPredictor.load(PREDICTOR_DIR, require_py_version_match=False)
47
 
48
  def do_predict(flower_diameter_cm, petal_length_cm, petal_width_cm, petal_count, stem_height_cm, top_k):
49
- row = {
50
- FEATURE_COLS[0]: float(flower_diameter_cm),
51
- FEATURE_COLS[1]: float(petal_length_cm),
52
- FEATURE_COLS[2]: float(petal_width_cm),
53
- FEATURE_COLS[3]: int(petal_count),
54
- FEATURE_COLS[4]: float(stem_height_cm),
55
- }
56
- X = pandas.DataFrame([row], columns=FEATURE_COLS)
57
-
58
- # Predicted label (string)
59
- pred_series = PREDICTOR.predict(X)
60
- pred_label = str(pred_series.iloc[0])
61
-
62
- # Probabilities: dict[class] -> float
63
- proba_dict = None
64
  try:
65
- proba = PREDICTOR.predict_proba(X)
66
- # AutoGluon can return Series for binary; normalize to DataFrame row
67
- if isinstance(proba, pandas.Series):
68
- proba = proba.to_frame().T
69
- row0 = proba.iloc[0].sort_values(ascending=False)
70
- if isinstance(top_k, (int, float)) and top_k > 0:
71
- row0 = row0.head(int(top_k))
72
- proba_dict = {str(cls): float(val) for cls, val in row0.items()}
73
- except Exception:
74
- # If proba not available, just put 1.0 on the predicted class
75
- proba_dict = {pred_label: 1.0}
76
-
77
- # Return TWO outputs: (predicted color string, dict of numeric probs)
78
- return proba_dict
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # ----------------
81
  # Example records
@@ -107,12 +105,13 @@ with gradio.Blocks() as demo:
107
 
108
  # Separate outputs: Textbox for label, Label for probs (dict must be numeric)
109
  proba_pretty = gradio.Label(num_top_classes=10, label="Class probabilities")
110
-
 
111
  inputs = [flower_diameter_cm, petal_length_cm, petal_width_cm, petal_count, stem_height_cm, top_k]
112
 
113
  # Trigger on any change
114
  for comp in inputs:
115
- comp.change(fn=do_predict, inputs=inputs, outputs=[proba_pretty])
116
 
117
  # Examples: only pass the first 5 inputs (excluding top_k) to match example rows
118
  gradio.Examples(
 
46
  PREDICTOR = autogluon.tabular.TabularPredictor.load(PREDICTOR_DIR, require_py_version_match=False)
47
 
48
  def do_predict(flower_diameter_cm, petal_length_cm, petal_width_cm, petal_count, stem_height_cm, top_k):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  try:
50
+ row = {
51
+ FEATURE_COLS[0]: float(flower_diameter_cm),
52
+ FEATURE_COLS[1]: float(petal_length_cm),
53
+ FEATURE_COLS[2]: float(petal_width_cm),
54
+ FEATURE_COLS[3]: int(petal_count),
55
+ FEATURE_COLS[4]: float(stem_height_cm),
56
+ }
57
+ X = pandas.DataFrame([row], columns=FEATURE_COLS)
58
+
59
+ pred_series = PREDICTOR.predict(X)
60
+ pred_label = str(pred_series.iloc[0])
61
+
62
+ try:
63
+ proba = PREDICTOR.predict_proba(X)
64
+ if isinstance(proba, pandas.Series):
65
+ proba = proba.to_frame().T
66
+ row0 = proba.iloc[0].sort_values(ascending=False)
67
+ if isinstance(top_k, (int, float)) and top_k > 0:
68
+ row0 = row0.head(int(top_k))
69
+ proba_dict = {str(cls): float(val) for cls, val in row0.items()}
70
+ except Exception:
71
+ proba_dict = {pred_label: 1.0}
72
+
73
+ return proba_dict, "" # second output is debug text
74
+ except Exception as e:
75
+ import traceback
76
+ return {}, f"{e}\n\n{traceback.format_exc()}"
77
 
78
  # ----------------
79
  # Example records
 
105
 
106
  # Separate outputs: Textbox for label, Label for probs (dict must be numeric)
107
  proba_pretty = gradio.Label(num_top_classes=10, label="Class probabilities")
108
+ debug_box = gradio.Textbox(label="debug", visible=True)
109
+
110
  inputs = [flower_diameter_cm, petal_length_cm, petal_width_cm, petal_count, stem_height_cm, top_k]
111
 
112
  # Trigger on any change
113
  for comp in inputs:
114
+ comp.change(fn=do_predict, inputs=inputs, outputs=[proba_pretty, debug_box])
115
 
116
  # Examples: only pass the first 5 inputs (excluding top_k) to match example rows
117
  gradio.Examples(