testrro commited on
Commit
115b540
·
verified ·
1 Parent(s): 0ebb7af

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -0
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from collections import deque, Counter
3
+
4
+ # =====================
5
+ # STATE
6
+ # =====================
7
+ history = deque(maxlen=400) # sliding window
8
+ last10 = deque(maxlen=10)
9
+ edit_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
+ # PREDICTION (Rule + Short Memory)
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
+ if run <= 2:
53
+ conf += 0.08
54
+ elif run == 3:
55
+ conf += 0.03
56
+ else:
57
+ conf -= 0.15
58
+
59
+ if abs(b5 - b13) < 0.15:
60
+ conf += 0.05
61
+ else:
62
+ conf -= 0.05
63
+
64
+ conf = max(0.35, min(conf, 0.75))
65
+ status = "GO" if conf >= 0.60 else "WARN"
66
+ return pred, round(conf, 2), status
67
+
68
+ # =====================
69
+ # CORE INPUT HANDLER
70
+ # =====================
71
+ def add_number(n):
72
+ n = int(n)
73
+
74
+ # normal add
75
+ history.append(bs(n))
76
+ last10.append(str(n))
77
+
78
+ pred, conf, status = predict()
79
+ return render(pred, conf, status)
80
+
81
+ def render(pred, conf, status):
82
+ hist_view = " ".join(last10)
83
+ out = f"""Prediction : {pred}
84
+ Confidence : {conf}
85
+ Status : {status}
86
+ """
87
+ return hist_view, out
88
+
89
+ # =====================
90
+ # EDIT LAST 10
91
+ # =====================
92
+ def select_edit(idx):
93
+ edit_index["idx"] = idx
94
+ return f"Editing index: {idx+1}"
95
+
96
+ def apply_edit(n):
97
+ idx = edit_index["idx"]
98
+ if idx is None or idx >= len(last10):
99
+ return "No index selected", ""
100
+
101
+ # replace last10
102
+ last10_list = list(last10)
103
+ last10_list[idx] = str(n)
104
+ last10.clear()
105
+ for x in last10_list:
106
+ last10.append(x)
107
+
108
+ # also fix history tail
109
+ hist = list(history)
110
+ tail_start = len(hist) - len(last10_list)
111
+ if tail_start >= 0:
112
+ hist[tail_start + idx] = bs(int(n))
113
+ history.clear()
114
+ for x in hist:
115
+ history.append(x)
116
+
117
+ pred, conf, status = predict()
118
+ edit_index["idx"] = None
119
+ return render(pred, conf, status)
120
+
121
+ # =====================
122
+ # UI
123
+ # =====================
124
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
125
+ gr.Markdown("## 🧠 Big / Small Engine (Fast Input + Editable History)")
126
+
127
+ hist_box = gr.Textbox(label="Last 10 Inputs", interactive=False)
128
+ out_box = gr.Textbox(label="Engine Output", lines=4)
129
+
130
+ gr.Markdown("### ➕ Quick Input")
131
+ with gr.Row():
132
+ for i in range(5):
133
+ gr.Button(str(i), size="sm").click(
134
+ add_number,
135
+ inputs=gr.Number(value=i, visible=False),
136
+ outputs=[hist_box, out_box],
137
+ )
138
+ with gr.Row():
139
+ for i in range(5, 10):
140
+ gr.Button(str(i), size="sm").click(
141
+ add_number,
142
+ inputs=gr.Number(value=i, visible=False),
143
+ outputs=[hist_box, out_box],
144
+ )
145
+
146
+ gr.Markdown("### ✏️ Edit Last 10 (Select index, then click number)")
147
+ with gr.Row():
148
+ for i in range(10):
149
+ gr.Button(f"{i+1}", size="sm").click(
150
+ select_edit,
151
+ inputs=gr.Number(value=i, visible=False),
152
+ outputs=out_box
153
+ )
154
+
155
+ with gr.Row():
156
+ for i in range(10):
157
+ gr.Button(str(i), size="sm").click(
158
+ apply_edit,
159
+ inputs=gr.Number(value=i, visible=False),
160
+ outputs=[hist_box, out_box]
161
+ )
162
+
163
+ demo.launch()