Update app.py
Browse files
app.py
CHANGED
|
@@ -1,136 +1,115 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 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 |
-
|
| 114 |
-
|
| 115 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|