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()