testrro commited on
Commit
5191a96
·
verified ·
1 Parent(s): c4bd6b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -136
app.py CHANGED
@@ -1,136 +1,115 @@
1
- import streamlit as st
2
- import numpy as np
3
- import pandas as pd
4
- import matplotlib.pyplot as plt
5
-
6
- st.set_page_config(page_title="AVOLD", layout="centered")
7
-
8
- # ------------------------
9
- # SESSION
10
- # ------------------------
11
- if "history" not in st.session_state:
12
- st.session_state.history = []
13
-
14
- if "error_log" not in st.session_state:
15
- st.session_state.error_log = []
16
-
17
- # ------------------------
18
- # TITLE
19
- # ------------------------
20
- st.markdown("<h1 style='text-align:center;'>AVOLD</h1>", unsafe_allow_html=True)
21
-
22
- # ------------------------
23
- # INPUT + RESET
24
- # ------------------------
25
- col1, col2 = st.columns([3,1])
26
-
27
- with col1:
28
- val = st.number_input("", min_value=1.0, step=0.01, placeholder="1.45")
29
-
30
- with col2:
31
- if st.button("RESET"):
32
- st.session_state.history = []
33
- st.session_state.error_log = []
34
-
35
- if st.button("ADD"):
36
- st.session_state.history.append(round(val,2))
37
-
38
- data = st.session_state.history[-50:]
39
-
40
- if len(data) < 5:
41
- st.stop()
42
-
43
- arr = np.array(data)
44
-
45
- # ------------------------
46
- # CORE METRICS
47
- # ------------------------
48
- mean = np.mean(arr)
49
- std = np.std(arr)
50
-
51
- low_ratio = np.sum(arr < 1.5)/len(arr)
52
- high_ratio = np.sum(arr > 3)/len(arr)
53
-
54
- chaos = (std * high_ratio) / (low_ratio + 0.1)
55
-
56
- # ------------------------
57
- # EXPECTED (ONLINE LEARNING EWMA)
58
- # ------------------------
59
- alpha = 0.3
60
- ewma = arr[0]
61
- for x in arr[1:]:
62
- ewma = alpha*x + (1-alpha)*ewma
63
-
64
- median = np.median(arr)
65
-
66
- expected = (ewma*0.6) + (median*0.4)
67
-
68
- # range
69
- lower = max(1.01, expected - std*0.5)
70
- upper = expected + std*0.5
71
-
72
- # ------------------------
73
- # PROBABILITY ENGINE
74
- # ------------------------
75
- prob_2 = np.sum(arr>=2)/len(arr)
76
- prob_3 = np.sum(arr>=3)/len(arr)
77
-
78
- # ------------------------
79
- # ONLINE LEARNING ERROR UPDATE
80
- # ------------------------
81
- if len(arr) > 1:
82
- last_expected = expected
83
- last_actual = arr[-1]
84
- error = abs(last_expected - last_actual)
85
- st.session_state.error_log.append(error)
86
-
87
- avg_error = np.mean(st.session_state.error_log) if len(st.session_state.error_log)>0 else 0
88
-
89
- # adaptive confidence
90
- stability = max(0,1-std/5)
91
- confidence = max(0, min(1, (1-avg_error/3 + stability)/2 ))
92
-
93
- # ------------------------
94
- # DECISION
95
- # ------------------------
96
- if chaos > 1.2:
97
- decision = "SKIP"
98
- elif expected > 2 and confidence > 0.6:
99
- decision = "ENTER+"
100
- elif expected > 1.4 and confidence > 0.5:
101
- decision = "ENTER"
102
- else:
103
- decision = "WAIT"
104
-
105
- # ------------------------
106
- # OUTPUT BOX (ONE LINE)
107
- # ------------------------
108
- c1, c2, c3 = st.columns(3)
109
-
110
- c1.metric("Expected", round(expected,2))
111
- c2.metric("Analysis", f"{round(prob_2,2)} | {round(prob_3,2)}")
112
- c3.metric("Decision", decision)
113
-
114
- # ------------------------
115
- # CHART 1: TREND
116
- # ------------------------
117
- fig1, ax1 = plt.subplots()
118
- ax1.plot(arr, linewidth=2)
119
- ax1.set_title("Trend")
120
- ax1.set_ylabel("Multiplier")
121
- st.pyplot(fig1)
122
-
123
- # ------------------------
124
- # CHART 2: ERROR (ONLINE LEARNING)
125
- # ------------------------
126
- if len(st.session_state.error_log) > 3:
127
- fig2, ax2 = plt.subplots()
128
- ax2.plot(st.session_state.error_log[-50:], linewidth=2)
129
- ax2.set_title("Learning Error")
130
- st.pyplot(fig2)
131
-
132
- # ------------------------
133
- # BOTTOM DATA
134
- # ------------------------
135
- st.markdown("---")
136
- st.write(data)
 
1
+ # app.py
2
+ import gradio as gr
3
+ import random
4
+ from statistics import mean
5
+
6
+ # ---------- Helper functions ----------
7
+ def compute_outputs(rounds):
8
+ """Calculate expected multiplier, analysis, and decision based on recent rounds."""
9
+ if not rounds:
10
+ return "0.0", "No data", "N/A"
11
+
12
+ # Use last 10 rounds (or fewer if not enough data)
13
+ recent = rounds[-10:] if len(rounds) >= 10 else rounds
14
+ avg = mean(recent)
15
+
16
+ # Expected multiplier = average ± small random fluctuation (simulate unpredictability)
17
+ expected = round(avg * random.uniform(0.95, 1.05), 2)
18
+
19
+ # Simple trend analysis
20
+ if len(recent) >= 2:
21
+ trend = recent[-1] - recent[-2]
22
+ if trend > 0.1:
23
+ analysis = "Increasing"
24
+ elif trend < -0.1:
25
+ analysis = "Decreasing"
26
+ else:
27
+ analysis = "Stable"
28
+ else:
29
+ analysis = "Insufficient data"
30
+
31
+ # Decision based on expected multiplier
32
+ if expected > 2.5:
33
+ decision = "High"
34
+ elif expected > 1.5:
35
+ decision = "Medium"
36
+ else:
37
+ decision = "Low"
38
+
39
+ return str(expected), analysis, decision
40
+
41
+ def update_ui(rounds):
42
+ """Prepare the dataframe and the three output values from the current rounds list."""
43
+ # Build table: [Round number, Multiplier]
44
+ table = [[i+1, val] for i, val in enumerate(rounds)]
45
+ exp, ana, dec = compute_outputs(rounds)
46
+ return table, exp, ana, dec
47
+
48
+ def add_round(state, new_val):
49
+ """Append a new multiplier, keep only last 50, and refresh UI."""
50
+ if new_val <= 0:
51
+ # ignore invalid inputs (but you could show a warning – here we just return unchanged)
52
+ return state + update_ui(state)[:3] # need to return state and outputs
53
+ rounds = state.copy()
54
+ rounds.append(new_val)
55
+ if len(rounds) > 50:
56
+ rounds = rounds[-50:]
57
+ table, exp, ana, dec = update_ui(rounds)
58
+ return rounds, table, exp, ana, dec
59
+
60
+ def reset_rounds():
61
+ """Reset to a fresh set of 20 random multipliers."""
62
+ initial = [round(random.uniform(1.0, 3.0), 2) for _ in range(20)]
63
+ table, exp, ana, dec = update_ui(initial)
64
+ return initial, table, exp, ana, dec
65
+
66
+ # ---------- Build the Gradio interface ----------
67
+ with gr.Blocks(theme='dark', css="footer {visibility: hidden}") as demo:
68
+ gr.Markdown("# AVOLD")
69
+
70
+ # State: stores the list of multipliers
71
+ rounds_state = gr.State([round(random.uniform(1.0, 3.0), 2) for _ in range(20)])
72
+
73
+ # Input row
74
+ with gr.Row():
75
+ new_multiplier = gr.Number(label="New Multiplier", value=1.0, step=0.1)
76
+ add_btn = gr.Button("Add Round")
77
+
78
+ # Reset button
79
+ reset_btn = gr.Button("Reset Data")
80
+
81
+ # Three outputs in one row
82
+ with gr.Row():
83
+ expected_out = gr.Textbox(label="Expected Multiplier", interactive=False)
84
+ analysis_out = gr.Textbox(label="Analysis", interactive=False)
85
+ decision_out = gr.Textbox(label="Decision", interactive=False)
86
+
87
+ # Table at the bottom
88
+ rounds_table = gr.Dataframe(
89
+ label="Last 50 Rounds",
90
+ headers=["Round", "Multiplier"],
91
+ row_count=10, # show 10 rows at a time (scrollable)
92
+ )
93
+
94
+ # Wire up the buttons
95
+ add_btn.click(
96
+ add_round,
97
+ inputs=[rounds_state, new_multiplier],
98
+ outputs=[rounds_state, rounds_table, expected_out, analysis_out, decision_out]
99
+ )
100
+
101
+ reset_btn.click(
102
+ reset_rounds,
103
+ outputs=[rounds_state, rounds_table, expected_out, analysis_out, decision_out]
104
+ )
105
+
106
+ # Initial load
107
+ demo.load(
108
+ lambda s: update_ui(s),
109
+ inputs=[rounds_state],
110
+ outputs=[rounds_table, expected_out, analysis_out, decision_out]
111
+ )
112
+
113
+ # Launch the app
114
+ if __name__ == "__main__":
115
+ demo.launch()