testrro commited on
Commit
3f5fd75
·
verified ·
1 Parent(s): bc45cf9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -0
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from collections import deque, Counter
3
+
4
+ # =====================
5
+ # STATE
6
+ # =====================
7
+ history = deque(maxlen=400)
8
+ last10 = [None] * 10
9
+ selected_index = {"idx": None}
10
+
11
+ # =====================
12
+ # HELPERS
13
+ # =====================
14
+ def bs(n):
15
+ return "S" if n <= 4 else "B"
16
+
17
+ def run_length(data):
18
+ if len(data) < 2:
19
+ return 1
20
+ r = 1
21
+ for i in range(len(data)-1, 0, -1):
22
+ if data[i] == data[i-1]:
23
+ r += 1
24
+ else:
25
+ break
26
+ return r
27
+
28
+ def bias_ratio(data):
29
+ if not data:
30
+ return 0.5
31
+ c = Counter(data)
32
+ return c["B"] / len(data)
33
+
34
+ # =====================
35
+ # ENGINE
36
+ # =====================
37
+ def predict():
38
+ if len(history) < 15:
39
+ return "WAIT", 0.50, "Not enough data"
40
+
41
+ data = list(history)
42
+ w5, w8, w13 = data[-5:], data[-8:], data[-13:]
43
+
44
+ b5 = bias_ratio(w5)
45
+ b13 = bias_ratio(w13)
46
+ avg_bias = (b5 + bias_ratio(w8) + b13) / 3
47
+ run = run_length(data)
48
+
49
+ pred = "BIG" if avg_bias >= 0.5 else "SMALL"
50
+
51
+ conf = 0.55
52
+ conf += 0.08 if run <= 2 else 0.03 if run == 3 else -0.15
53
+ conf += 0.05 if abs(b5 - b13) < 0.15 else -0.05
54
+ conf = max(0.35, min(conf, 0.75))
55
+
56
+ status = "GO" if conf >= 0.60 else "WARN"
57
+ return pred, round(conf, 2), status
58
+
59
+ # =====================
60
+ # RENDER
61
+ # =====================
62
+ def render():
63
+ boxes = ["" if x is None else str(x) for x in last10]
64
+ pred, conf, status = predict()
65
+ out = f"""Prediction : {pred}
66
+ Confidence : {conf}
67
+ Status : {status}"""
68
+ return boxes + [out]
69
+
70
+ # =====================
71
+ # INPUT
72
+ # =====================
73
+ def add_number(n):
74
+ # fill selected box if exists
75
+ if selected_index["idx"] is not None:
76
+ idx = selected_index["idx"]
77
+ last10[idx] = n
78
+ history.append(bs(n))
79
+ selected_index["idx"] = None
80
+ else:
81
+ # shift left
82
+ last10.pop(0)
83
+ last10.append(n)
84
+ history.append(bs(n))
85
+ return render()
86
+
87
+ def select_box(idx):
88
+ selected_index["idx"] = idx
89
+ return render()
90
+
91
+ def backspace():
92
+ idx = selected_index["idx"]
93
+ if idx is not None and last10[idx] is not None:
94
+ last10[idx] = None
95
+ if history:
96
+ history.pop()
97
+ return render()
98
+
99
+ def reset_all():
100
+ history.clear()
101
+ for i in range(10):
102
+ last10[i] = None
103
+ selected_index["idx"] = None
104
+ return render()
105
+
106
+ # =====================
107
+ # UI
108
+ # =====================
109
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
110
+ gr.Markdown("## 🧠 Big / Small Prediction Engine")
111
+
112
+ # last 10 boxes
113
+ with gr.Row():
114
+ boxes = []
115
+ for i in range(10):
116
+ b = gr.Button("", size="sm")
117
+ b.click(select_box,
118
+ inputs=gr.Number(value=i, visible=False),
119
+ outputs=[])
120
+ boxes.append(b)
121
+
122
+ with gr.Row():
123
+ out_box = gr.Textbox(label="Engine Output", lines=4)
124
+ back_btn = gr.Button("⌫ BACKSPACE", size="sm")
125
+ back_btn.click(backspace, outputs=boxes + [out_box])
126
+
127
+ gr.Markdown("### Quick Input")
128
+ with gr.Row():
129
+ for i in range(5):
130
+ gr.Button(str(i), size="sm").click(
131
+ add_number,
132
+ inputs=gr.Number(value=i, visible=False),
133
+ outputs=boxes + [out_box],
134
+ )
135
+ with gr.Row():
136
+ for i in range(5, 10):
137
+ gr.Button(str(i), size="sm").click(
138
+ add_number,
139
+ inputs=gr.Number(value=i, visible=False),
140
+ outputs=boxes + [out_box],
141
+ )
142
+
143
+ gr.Markdown("---")
144
+ gr.Button("RESET ALL DATA", variant="stop").click(
145
+ reset_all,
146
+ outputs=boxes + [out_box]
147
+ )
148
+
149
+ demo.launch()