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