Benny-Tang commited on
Commit
6f8b57c
·
verified ·
1 Parent(s): cb5e5ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -21
app.py CHANGED
@@ -8,27 +8,27 @@ from datetime import datetime
8
 
9
  from utils import generate_features, pick_top15
10
 
11
- # In-memory history of predictions (max 12 rows for the month)
12
  prediction_history = []
13
 
14
 
15
  def train_and_predict(file_obj):
16
- # --- Load dataset correctly ---
17
  df = pd.read_csv(file_obj.name, header=0)
18
- df = df.iloc[:, :8] # Keep first 8 cols: draw_no, draw_date, n1..n6
19
  df.columns = ["draw_no", "draw_date", "n1", "n2", "n3", "n4", "n5", "n6"]
20
 
21
- # Convert numbers to integers
22
  for col in ["n1", "n2", "n3", "n4", "n5", "n6"]:
23
  df[col] = pd.to_numeric(df[col], errors="coerce")
24
 
25
  debug_log = []
26
- debug_log.append(f"✅ Loaded dataset with {len(df)} draws")
27
  debug_log.append(f"First draw: {df['draw_date'].iloc[0]}, Last draw: {df['draw_date'].iloc[-1]}")
28
 
29
  # --- Generate features ---
30
  features, labels = generate_features(df)
31
- debug_log.append(f"Generated {len(features)} rows, Label distribution: {np.bincount(labels)}")
32
 
33
  top15 = None
34
  auc = None
@@ -44,6 +44,7 @@ def train_and_predict(file_obj):
44
  model = GradientBoostingClassifier(n_estimators=200, max_depth=3, random_state=42)
45
  model.fit(X_train, y_train)
46
 
 
47
  if len(np.unique(y_test)) > 1:
48
  auc = roc_auc_score(y_test, model.predict_proba(X_test)[:, 1])
49
  else:
@@ -52,10 +53,14 @@ def train_and_predict(file_obj):
52
  # Score all numbers 1–50
53
  all_numbers = pd.DataFrame({"number": range(1, 51)})
54
  all_features, _ = generate_features(df, candidate_numbers=all_numbers["number"].tolist())
55
- scores = model.predict_proba(all_features)[:, 1]
56
- all_numbers["score"] = scores
57
 
58
- top15 = pick_top15(all_numbers)
 
 
 
 
 
59
  debug_log.append(f"🎯 ML Top 15: {top15}")
60
  debug_log.append(f"Model AUC: {auc:.3f}")
61
 
@@ -63,10 +68,10 @@ def train_and_predict(file_obj):
63
  debug_log.append(f"⚠️ ML failed: {str(e)}")
64
  used_fallback = True
65
  else:
66
- debug_log.append("⚠️ Only one class found — using fallback")
67
  used_fallback = True
68
 
69
- # --- Fallback: frequency-based ---
70
  if used_fallback or top15 is None:
71
  nums = df[["n1", "n2", "n3", "n4", "n5", "n6"]].values.flatten()
72
  freq = pd.Series(nums).value_counts().reset_index()
@@ -74,28 +79,28 @@ def train_and_predict(file_obj):
74
  top15 = sorted(freq.head(15)["number"].tolist())
75
  debug_log.append(f"👉 Fallback Top 15: {top15}")
76
 
77
- # --- Record in prediction history ---
78
- today = datetime.now().strftime("%Y-%m-%d %H:%M")
79
  prediction_history.append({"date": today, "numbers": top15})
80
- # Keep last 12 only
81
  if len(prediction_history) > 12:
82
  prediction_history.pop(0)
83
 
84
- # Build table view
85
- history_df = pd.DataFrame(prediction_history)
86
- history_df["numbers"] = history_df["numbers"].apply(lambda x: " ".join(str(n).zfill(2) for n in x))
 
87
 
88
- table_view = history_df.to_string(index=False)
89
 
90
- return "\n".join(debug_log) + "\n\n📊 Prediction History (last 12):\n" + table_view
91
 
92
 
93
  demo = gr.Interface(
94
  fn=train_and_predict,
95
  inputs=gr.File(file_types=[".txt", ".csv"], label="Upload Toto650.txt"),
96
- outputs=gr.Textbox(label="Training, Prediction, and History Log", lines=25),
97
  title="Sure Win Club - Star Toto 6/50 Predictor",
98
- description="Upload Toto650.txt after each draw. System trains fresh and shows Top 15 hot numbers + running history (up to 12 rows)."
99
  )
100
 
101
  if __name__ == "__main__":
@@ -106,3 +111,4 @@ if __name__ == "__main__":
106
 
107
 
108
 
 
 
8
 
9
  from utils import generate_features, pick_top15
10
 
11
+ # In-memory history (up to 12 predictions)
12
  prediction_history = []
13
 
14
 
15
  def train_and_predict(file_obj):
16
+ # --- Load dataset ---
17
  df = pd.read_csv(file_obj.name, header=0)
18
+ df = df.iloc[:, :8] # draw_no, draw_date, n1..n6
19
  df.columns = ["draw_no", "draw_date", "n1", "n2", "n3", "n4", "n5", "n6"]
20
 
21
+ # Convert numbers to int
22
  for col in ["n1", "n2", "n3", "n4", "n5", "n6"]:
23
  df[col] = pd.to_numeric(df[col], errors="coerce")
24
 
25
  debug_log = []
26
+ debug_log.append(f"✅ Loaded {len(df)} draws")
27
  debug_log.append(f"First draw: {df['draw_date'].iloc[0]}, Last draw: {df['draw_date'].iloc[-1]}")
28
 
29
  # --- Generate features ---
30
  features, labels = generate_features(df)
31
+ debug_log.append(f"Features: {len(features)} rows | Label distribution: {np.bincount(labels)}")
32
 
33
  top15 = None
34
  auc = None
 
44
  model = GradientBoostingClassifier(n_estimators=200, max_depth=3, random_state=42)
45
  model.fit(X_train, y_train)
46
 
47
+ # AUC check
48
  if len(np.unique(y_test)) > 1:
49
  auc = roc_auc_score(y_test, model.predict_proba(X_test)[:, 1])
50
  else:
 
53
  # Score all numbers 1–50
54
  all_numbers = pd.DataFrame({"number": range(1, 51)})
55
  all_features, _ = generate_features(df, candidate_numbers=all_numbers["number"].tolist())
56
+ probs = model.predict_proba(all_features)[:, 1]
 
57
 
58
+ # Aggregate mean probability per number
59
+ all_features["prob"] = probs
60
+ scored = all_features.groupby("number")["prob"].mean().reset_index()
61
+ scored.columns = ["number", "score"]
62
+
63
+ top15 = pick_top15(scored)
64
  debug_log.append(f"🎯 ML Top 15: {top15}")
65
  debug_log.append(f"Model AUC: {auc:.3f}")
66
 
 
68
  debug_log.append(f"⚠️ ML failed: {str(e)}")
69
  used_fallback = True
70
  else:
71
+ debug_log.append("⚠️ Only one class found — fallback mode")
72
  used_fallback = True
73
 
74
+ # --- Fallback (frequency-based) ---
75
  if used_fallback or top15 is None:
76
  nums = df[["n1", "n2", "n3", "n4", "n5", "n6"]].values.flatten()
77
  freq = pd.Series(nums).value_counts().reset_index()
 
79
  top15 = sorted(freq.head(15)["number"].tolist())
80
  debug_log.append(f"👉 Fallback Top 15: {top15}")
81
 
82
+ # --- Record prediction history ---
83
+ today = datetime.now().strftime("%Y-%m-%d")
84
  prediction_history.append({"date": today, "numbers": top15})
 
85
  if len(prediction_history) > 12:
86
  prediction_history.pop(0)
87
 
88
+ # Format history log
89
+ history_lines = []
90
+ for row in prediction_history:
91
+ history_lines.append(f"{row['date']} Top 15: {row['numbers']}")
92
 
93
+ log_output = "\n".join(debug_log) + "\n\n📊 Prediction History (last 12 runs):\n" + "\n".join(history_lines)
94
 
95
+ return log_output
96
 
97
 
98
  demo = gr.Interface(
99
  fn=train_and_predict,
100
  inputs=gr.File(file_types=[".txt", ".csv"], label="Upload Toto650.txt"),
101
+ outputs=gr.Textbox(label="Training, Prediction & History Log", lines=25),
102
  title="Sure Win Club - Star Toto 6/50 Predictor",
103
+ description="Upload Toto650.txt after each draw. System trains fresh and shows Top 15 hot numbers + running history (up to 12 runs)."
104
  )
105
 
106
  if __name__ == "__main__":
 
111
 
112
 
113
 
114
+