Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from itertools import combinations
|
| 5 |
+
from sklearn.ensemble import GradientBoostingClassifier
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.metrics import roc_auc_score
|
| 8 |
+
import joblib
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
from utils import generate_features, pick_top15, generate_system15_csv
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def train_and_predict(file_obj):
|
| 15 |
+
# Load dataset
|
| 16 |
+
df = pd.read_csv(file_obj.name, header=None)
|
| 17 |
+
# Columns: [draw_id?, draw_date, n1..n6, bonus, ...]
|
| 18 |
+
# Align to schema: we only keep date, n1..n6, bonus
|
| 19 |
+
df = df.iloc[:, :8]
|
| 20 |
+
df.columns = ["draw_date", "n1", "n2", "n3", "n4", "n5", "n6", "bonus"]
|
| 21 |
+
|
| 22 |
+
# Melt numbers into long format for training
|
| 23 |
+
features, labels = generate_features(df)
|
| 24 |
+
|
| 25 |
+
# Split into train/test
|
| 26 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 27 |
+
features, labels, test_size=0.2, random_state=42, stratify=labels
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Train a simple gradient boosting model
|
| 31 |
+
model = GradientBoostingClassifier(n_estimators=200, max_depth=3, random_state=42)
|
| 32 |
+
model.fit(X_train, y_train)
|
| 33 |
+
|
| 34 |
+
# Evaluate
|
| 35 |
+
if len(np.unique(y_test)) > 1:
|
| 36 |
+
auc = roc_auc_score(y_test, model.predict_proba(X_test)[:, 1])
|
| 37 |
+
else:
|
| 38 |
+
auc = 0.5
|
| 39 |
+
|
| 40 |
+
# Score all numbers 1–50
|
| 41 |
+
all_numbers = pd.DataFrame({"number": range(1, 51)})
|
| 42 |
+
all_features, _ = generate_features(df, candidate_numbers=all_numbers["number"].tolist())
|
| 43 |
+
scores = model.predict_proba(all_features)[:, 1]
|
| 44 |
+
all_numbers["score"] = scores
|
| 45 |
+
|
| 46 |
+
# Pick top 15
|
| 47 |
+
top15 = pick_top15(all_numbers)
|
| 48 |
+
|
| 49 |
+
# Generate System 15 CSV (5005 combos)
|
| 50 |
+
csv_path = "system15.csv"
|
| 51 |
+
generate_system15_csv(top15, csv_path)
|
| 52 |
+
|
| 53 |
+
return f"Model AUC: {auc:.3f}\nTop 15 Numbers: {top15}", csv_path
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
demo = gr.Interface(
|
| 57 |
+
fn=train_and_predict,
|
| 58 |
+
inputs=gr.File(file_types=[".txt", ".csv"], label="Upload Toto650.txt"),
|
| 59 |
+
outputs=[
|
| 60 |
+
gr.Textbox(label="Prediction Summary"),
|
| 61 |
+
gr.File(label="Download System15 CSV")
|
| 62 |
+
],
|
| 63 |
+
title="Sure Win Club - Star Toto 6/50 Predictor",
|
| 64 |
+
description="Upload the latest Toto650.txt dataset every Monday. The system will train a fresh model and output Top 15 numbers + System15 (5005 tickets)."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|
| 69 |
+
|
| 70 |
|
| 71 |
|