Spaces:
Sleeping
Sleeping
skanderovitch
commited on
Commit
β’
dbad5ad
1
Parent(s):
4dd79af
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from collections import defaultdict
|
|
3 |
|
4 |
import streamlit as st
|
5 |
import streamlit.components.v1 as components
|
|
|
6 |
import plotly.express as px
|
7 |
import numpy as np
|
8 |
|
@@ -22,14 +23,10 @@ st.markdown("""
|
|
22 |
""", unsafe_allow_html=True)
|
23 |
|
24 |
|
25 |
-
max_history =
|
26 |
-
|
27 |
|
28 |
def set_state(x):
|
29 |
if x == 1: st.toast('The journey begins!', icon='π')
|
30 |
-
if x == 2: st.toast('Welcome!', icon='π')
|
31 |
-
if x == 3: st.toast("Let's play!", icon='π₯°')
|
32 |
-
if x == 4: st.toast("Meet your friends!", icon='π€ͺ')
|
33 |
st.session_state.stage = x
|
34 |
|
35 |
|
@@ -40,11 +37,13 @@ def reset_game():
|
|
40 |
st.session_state.history = []
|
41 |
st.session_state.preds = defaultdict(lambda: defaultdict(int))
|
42 |
st.session_state.pnl = [0]
|
|
|
|
|
43 |
|
44 |
if 'stage' not in st.session_state:
|
45 |
reset_game()
|
46 |
|
47 |
-
st.title('
|
48 |
|
49 |
if st.session_state.stage == 0:
|
50 |
st.button('Begin', on_click=set_state, args=[1],use_container_width=True)
|
@@ -60,49 +59,75 @@ if st.session_state.stage == 0:
|
|
60 |
|
61 |
|
62 |
|
63 |
-
def get_prev_seqs(history):
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
|
74 |
-
def refresh_preds():
|
75 |
-
played = st.session_state.history[-1]
|
76 |
-
seqs = get_prev_seqs(st.session_state.history[:-1])
|
77 |
-
for seq in seqs:
|
78 |
-
# st.write(f'"{seq}"',played)
|
79 |
-
st.session_state.preds[seq][played] += 1
|
80 |
-
|
81 |
-
def make_pred():
|
82 |
-
seqs = get_prev_seqs(st.session_state.history)
|
83 |
-
# st.write('seqs',seqs)
|
84 |
-
scores = defaultdict(float)
|
85 |
denominator = 0
|
86 |
-
for i
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
|
102 |
def update_pnl(user_win):
|
103 |
current_score = st.session_state.pnl[-1]
|
104 |
current_score += -1 if user_win else (st.session_state.n_buttons-1)
|
105 |
st.session_state.pnl.append(current_score)
|
|
|
|
|
|
|
106 |
|
107 |
def user_select(i,choice):
|
108 |
st.session_state.history.append(str(i))
|
@@ -113,7 +138,7 @@ def user_select(i,choice):
|
|
113 |
st.toast('Well done!', icon='π')
|
114 |
update_pnl(user_win=True)
|
115 |
|
116 |
-
refresh_preds()
|
117 |
|
118 |
def compute_perf():
|
119 |
data = np.array(st.session_state.pnl)
|
@@ -135,8 +160,10 @@ if st.session_state.stage == 1:
|
|
135 |
col.button(str(i),on_click=user_select, args=[str(i),choice],
|
136 |
use_container_width=True)
|
137 |
|
138 |
-
st.
|
139 |
-
|
|
|
|
|
140 |
|
141 |
st.button('Start over', on_click=reset_game, args=[],
|
142 |
use_container_width=True)
|
|
|
3 |
|
4 |
import streamlit as st
|
5 |
import streamlit.components.v1 as components
|
6 |
+
import plotly.graph_objects as go
|
7 |
import plotly.express as px
|
8 |
import numpy as np
|
9 |
|
|
|
23 |
""", unsafe_allow_html=True)
|
24 |
|
25 |
|
26 |
+
max_history = 10
|
|
|
27 |
|
28 |
def set_state(x):
|
29 |
if x == 1: st.toast('The journey begins!', icon='π')
|
|
|
|
|
|
|
30 |
st.session_state.stage = x
|
31 |
|
32 |
|
|
|
37 |
st.session_state.history = []
|
38 |
st.session_state.preds = defaultdict(lambda: defaultdict(int))
|
39 |
st.session_state.pnl = [0]
|
40 |
+
st.session_state.min = [0]
|
41 |
+
st.session_state.max = [0]
|
42 |
|
43 |
if 'stage' not in st.session_state:
|
44 |
reset_game()
|
45 |
|
46 |
+
st.title('Can your brain be random?')
|
47 |
|
48 |
if st.session_state.stage == 0:
|
49 |
st.button('Begin', on_click=set_state, args=[1],use_container_width=True)
|
|
|
59 |
|
60 |
|
61 |
|
62 |
+
# def get_prev_seqs(history):
|
63 |
+
# # st.write(history)
|
64 |
+
|
65 |
+
# seqs = []
|
66 |
+
# for h in range(max_history+1):
|
67 |
+
# if len(history) >= h:
|
68 |
+
# previous_seq = ''.join(history[-h:]) if h > 0 else ""
|
69 |
+
# # st.write(previous_seq)
|
70 |
+
# seqs.append(previous_seq) # from small to largest
|
71 |
+
# return seqs
|
72 |
|
73 |
+
# def refresh_preds():
|
74 |
+
# played = st.session_state.history[-1]
|
75 |
+
# seqs = get_prev_seqs(st.session_state.history[:-1])
|
76 |
+
# for seq in seqs:
|
77 |
+
# # st.write(f'"{seq}"',played)
|
78 |
+
# st.session_state.preds[seq][played] += 1
|
79 |
+
|
80 |
+
def make_pred(max_history=max_history,alpha=0.5):
|
81 |
+
history = st.session_state.history
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
denominator = 0
|
84 |
+
scores = {str(i):0 for i in range(st.session_state.n_buttons)}
|
85 |
+
recent = np.array(history[-max_history:])
|
86 |
+
for i in range(1,len(history)):
|
87 |
+
past = np.array(history[-i-max_history:-i])
|
88 |
|
89 |
+
played = history[-i]
|
90 |
+
decay = np.exp(-alpha*np.linspace(1,0,len(past)))
|
91 |
+
similarity = ((past == recent[-len(past):])*decay).sum() / decay.sum()
|
92 |
+
weight = len(history)/(len(history)+i) * similarity
|
93 |
+
scores[played] += weight
|
94 |
+
denominator += weight
|
95 |
+
if not denominator:
|
96 |
+
return get_random_play()
|
97 |
+
result = {str(i):scores[str(i)]/denominator for i in range(st.session_state.n_buttons)}
|
98 |
+
return result
|
99 |
+
|
100 |
+
def get_random_play():
|
101 |
+
return {str(i):1/st.session_state.n_buttons for i in range(st.session_state.n_buttons)}
|
102 |
+
|
103 |
+
# def make_pred():
|
104 |
+
# seqs = get_prev_seqs(st.session_state.history)
|
105 |
+
# # st.write('seqs',seqs)
|
106 |
+
# scores = defaultdict(float)
|
107 |
+
# denominator = 0
|
108 |
+
# for i,seq in enumerate(seqs):
|
109 |
+
# weight = (i+1)**2
|
110 |
+
# preds = st.session_state.preds[seq]
|
111 |
+
# total = sum(preds.values())
|
112 |
+
|
113 |
+
# if total:
|
114 |
+
# for played,value in preds.items():
|
115 |
+
# scores[played] += weight*value/total
|
116 |
+
# denominator += weight
|
117 |
+
# if denominator:
|
118 |
+
# scores = {played:value/denominator for played,value in scores.items()}
|
119 |
+
# return scores
|
120 |
+
# else:
|
121 |
+
# return get_random_play()
|
122 |
|
123 |
|
124 |
def update_pnl(user_win):
|
125 |
current_score = st.session_state.pnl[-1]
|
126 |
current_score += -1 if user_win else (st.session_state.n_buttons-1)
|
127 |
st.session_state.pnl.append(current_score)
|
128 |
+
expected_change = (2**0.5) * ((st.session_state.n_buttons-1)/st.session_state.n_buttons)
|
129 |
+
st.session_state.min.append(st.session_state.min[-1]-expected_change)
|
130 |
+
st.session_state.max.append(st.session_state.max[-1]+expected_change)
|
131 |
|
132 |
def user_select(i,choice):
|
133 |
st.session_state.history.append(str(i))
|
|
|
138 |
st.toast('Well done!', icon='π')
|
139 |
update_pnl(user_win=True)
|
140 |
|
141 |
+
# refresh_preds()
|
142 |
|
143 |
def compute_perf():
|
144 |
data = np.array(st.session_state.pnl)
|
|
|
160 |
col.button(str(i),on_click=user_select, args=[str(i),choice],
|
161 |
use_container_width=True)
|
162 |
|
163 |
+
st.subheader(f'My earnings so far... {compute_perf()} :-)')
|
164 |
+
fig = px.line(st.session_state.pnl)
|
165 |
+
fig.update_layout(showlegend=False)
|
166 |
+
st.plotly_chart(fig, use_container_width=True)
|
167 |
|
168 |
st.button('Start over', on_click=reset_game, args=[],
|
169 |
use_container_width=True)
|