Benny-Tang commited on
Commit
cb5e5ad
·
verified ·
1 Parent(s): 790c045

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -26
app.py CHANGED
@@ -4,23 +4,31 @@ import numpy as np
4
  from sklearn.ensemble import GradientBoostingClassifier
5
  from sklearn.model_selection import train_test_split
6
  from sklearn.metrics import roc_auc_score
 
7
 
8
- from utils import generate_features, pick_top15, generate_system15_csv
 
 
 
9
 
10
 
11
  def train_and_predict(file_obj):
12
- # Load dataset
13
- df = pd.read_csv(file_obj.name, header=None)
14
- df = df.iloc[:, :8] # Keep only date + 6 main numbers + bonus
15
- df.columns = ["draw_date", "n1", "n2", "n3", "n4", "n5", "n6", "bonus"]
 
 
 
 
16
 
17
  debug_log = []
18
  debug_log.append(f"✅ Loaded dataset with {len(df)} draws")
19
- debug_log.append(f"First draw date: {df['draw_date'].iloc[0]}, Last draw date: {df['draw_date'].iloc[-1]}")
20
 
21
- # Generate features and labels
22
  features, labels = generate_features(df)
23
- debug_log.append(f"Generated {len(features)} feature rows, Labels distribution: {np.bincount(labels)}")
24
 
25
  top15 = None
26
  auc = None
@@ -32,7 +40,6 @@ def train_and_predict(file_obj):
32
  X_train, X_test, y_train, y_test = train_test_split(
33
  features, labels, test_size=0.2, random_state=42, stratify=labels
34
  )
35
- debug_log.append(f"Train size: {len(X_train)}, Test size: {len(X_test)}")
36
 
37
  model = GradientBoostingClassifier(n_estimators=200, max_depth=3, random_state=42)
38
  model.fit(X_train, y_train)
@@ -49,43 +56,46 @@ def train_and_predict(file_obj):
49
  all_numbers["score"] = scores
50
 
51
  top15 = pick_top15(all_numbers)
52
- debug_log.append(f"🎯 ML Top 15 Numbers: {top15}")
53
  debug_log.append(f"Model AUC: {auc:.3f}")
54
 
55
  except Exception as e:
56
- debug_log.append(f"⚠️ ML training failed: {str(e)}")
57
  used_fallback = True
58
  else:
59
- debug_log.append("⚠️ Only one class found in labels skipping ML")
60
  used_fallback = True
61
 
62
- # --- Fallback: Frequency-based ---
63
  if used_fallback or top15 is None:
64
- debug_log.append("👉 Using fallback: frequency-based Top 15")
65
- # Count occurrences of each number in all draws
66
  nums = df[["n1", "n2", "n3", "n4", "n5", "n6"]].values.flatten()
67
  freq = pd.Series(nums).value_counts().reset_index()
68
  freq.columns = ["number", "count"]
69
  top15 = sorted(freq.head(15)["number"].tolist())
70
- debug_log.append(f"🎯 Frequency Top 15 Numbers: {top15}")
 
 
 
 
 
 
 
71
 
72
- # Generate System 15 CSV (5005 combos)
73
- csv_path = "system15.csv"
74
- generate_system15_csv(top15, csv_path)
75
- debug_log.append("📂 system15.csv generated with 5005 combinations")
76
 
77
- return "\n".join(debug_log), csv_path
 
 
78
 
79
 
80
  demo = gr.Interface(
81
  fn=train_and_predict,
82
  inputs=gr.File(file_types=[".txt", ".csv"], label="Upload Toto650.txt"),
83
- outputs=[
84
- gr.Textbox(label="Training & Prediction Log", lines=20),
85
- gr.File(label="Download System15 CSV")
86
- ],
87
  title="Sure Win Club - Star Toto 6/50 Predictor",
88
- description="Upload the latest Toto650.txt dataset every Monday. The system will train a fresh model (or fallback to frequency) and output Top 15 numbers + System15 (5005 tickets)."
89
  )
90
 
91
  if __name__ == "__main__":
@@ -95,3 +105,4 @@ if __name__ == "__main__":
95
 
96
 
97
 
 
 
4
  from sklearn.ensemble import GradientBoostingClassifier
5
  from sklearn.model_selection import train_test_split
6
  from sklearn.metrics import roc_auc_score
7
+ 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
 
40
  X_train, X_test, y_train, y_test = train_test_split(
41
  features, labels, test_size=0.2, random_state=42, stratify=labels
42
  )
 
43
 
44
  model = GradientBoostingClassifier(n_estimators=200, max_depth=3, random_state=42)
45
  model.fit(X_train, y_train)
 
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
 
62
  except Exception as e:
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()
73
  freq.columns = ["number", "count"]
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__":
 
105
 
106
 
107
 
108
+