Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from collections import deque, Counter
|
| 3 |
+
|
| 4 |
+
# =====================
|
| 5 |
+
# STATE
|
| 6 |
+
# =====================
|
| 7 |
+
history = deque(maxlen=200)
|
| 8 |
+
last10 = deque(maxlen=10)
|
| 9 |
+
|
| 10 |
+
# =====================
|
| 11 |
+
# HELPERS
|
| 12 |
+
# =====================
|
| 13 |
+
def bs(n):
|
| 14 |
+
return "S" if n <= 4 else "B"
|
| 15 |
+
|
| 16 |
+
def run_length(data):
|
| 17 |
+
if len(data) < 2:
|
| 18 |
+
return 1
|
| 19 |
+
r = 1
|
| 20 |
+
for i in range(len(data)-1, 0, -1):
|
| 21 |
+
if data[i] == data[i-1]:
|
| 22 |
+
r += 1
|
| 23 |
+
else:
|
| 24 |
+
break
|
| 25 |
+
return r
|
| 26 |
+
|
| 27 |
+
def bias_ratio(data):
|
| 28 |
+
if not data:
|
| 29 |
+
return 0.5
|
| 30 |
+
c = Counter(data)
|
| 31 |
+
return c["B"] / len(data)
|
| 32 |
+
|
| 33 |
+
# =====================
|
| 34 |
+
# PREDICTION ENGINE
|
| 35 |
+
# =====================
|
| 36 |
+
def predict():
|
| 37 |
+
if len(history) < 15:
|
| 38 |
+
return "WAIT", 0.50, "Not enough data"
|
| 39 |
+
|
| 40 |
+
data = list(history)
|
| 41 |
+
|
| 42 |
+
w5 = data[-5:]
|
| 43 |
+
w8 = data[-8:]
|
| 44 |
+
w13 = data[-13:]
|
| 45 |
+
|
| 46 |
+
b5 = bias_ratio(w5)
|
| 47 |
+
b8 = bias_ratio(w8)
|
| 48 |
+
b13 = bias_ratio(w13)
|
| 49 |
+
|
| 50 |
+
avg_bias = (b5 + b8 + b13) / 3
|
| 51 |
+
run = run_length(data)
|
| 52 |
+
|
| 53 |
+
pred = "BIG" if avg_bias >= 0.5 else "SMALL"
|
| 54 |
+
|
| 55 |
+
# confidence (PDF-inspired)
|
| 56 |
+
conf = 0.55
|
| 57 |
+
|
| 58 |
+
if run <= 2:
|
| 59 |
+
conf += 0.08
|
| 60 |
+
elif run == 3:
|
| 61 |
+
conf += 0.03
|
| 62 |
+
else:
|
| 63 |
+
conf -= 0.15
|
| 64 |
+
|
| 65 |
+
if abs(b5 - b13) < 0.15:
|
| 66 |
+
conf += 0.05
|
| 67 |
+
else:
|
| 68 |
+
conf -= 0.05
|
| 69 |
+
|
| 70 |
+
conf = max(0.35, min(conf, 0.75))
|
| 71 |
+
|
| 72 |
+
status = "GO" if conf >= 0.60 else "WARN"
|
| 73 |
+
return pred, round(conf, 2), status
|
| 74 |
+
|
| 75 |
+
# =====================
|
| 76 |
+
# INPUT HANDLER
|
| 77 |
+
# =====================
|
| 78 |
+
def add_number(n):
|
| 79 |
+
n = int(n)
|
| 80 |
+
history.append(bs(n))
|
| 81 |
+
last10.append(str(n))
|
| 82 |
+
|
| 83 |
+
pred, conf, status = predict()
|
| 84 |
+
|
| 85 |
+
hist_view = " ".join(last10)
|
| 86 |
+
out = f"""
|
| 87 |
+
Prediction : {pred}
|
| 88 |
+
Confidence : {conf}
|
| 89 |
+
Status : {status}
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
return hist_view, out
|
| 93 |
+
|
| 94 |
+
# =====================
|
| 95 |
+
# UI
|
| 96 |
+
# =====================
|
| 97 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 98 |
+
gr.Markdown("## 🧠 Big / Small Engine (Rule + Short Memory)")
|
| 99 |
+
|
| 100 |
+
hist_box = gr.Textbox(label="Last 10 Inputs", interactive=False)
|
| 101 |
+
out_box = gr.Textbox(label="Engine Output", lines=5)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
for i in range(10):
|
| 105 |
+
btn = gr.Button(str(i))
|
| 106 |
+
btn.click(
|
| 107 |
+
fn=add_number,
|
| 108 |
+
inputs=gr.Number(value=i, visible=False),
|
| 109 |
+
outputs=[hist_box, out_box]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
demo.launch()
|