Spaces:
Sleeping
Sleeping
mzimm003
commited on
Commit
·
22beef3
1
Parent(s):
b3c5de3
Clean up GUI
Browse files
app.py
CHANGED
@@ -19,22 +19,76 @@ class HvB(HumanVsBot):
|
|
19 |
extra_model_environment_context = _extra_model_environment_context,
|
20 |
**kwargs)
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
play = HvB.get_hvb_manager(
|
24 |
_model=DCMinMax.from_pretrained("mzimm003/{}".format(bot_select)),
|
25 |
_environment=Chess(render_mode="rgb_array"),
|
26 |
_extra_model_environment_context=lambda env: {"board":env.board}
|
27 |
)
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def main(kwargs=None):
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
if __name__ == "__main__":
|
39 |
-
|
40 |
-
play("DeepChessReplicationMinMax")
|
|
|
19 |
extra_model_environment_context = _extra_model_environment_context,
|
20 |
**kwargs)
|
21 |
|
22 |
+
@st.cache_resource
|
23 |
+
def track_play():
|
24 |
+
class trackPlay:
|
25 |
+
def __init__(self):
|
26 |
+
self.active_game = False
|
27 |
+
self.bot_select = None
|
28 |
+
def reset(self):
|
29 |
+
self.active_game = False
|
30 |
+
self.bot_select = None
|
31 |
+
return trackPlay()
|
32 |
+
|
33 |
+
def information(play, t_p):
|
34 |
+
display = {
|
35 |
+
True:{True:"Your turn!",False:"Bot is thinking..."},
|
36 |
+
False:{"1-0":"White Wins!","0-1":"Black Wins!","1/2-1/2":"It's a Draw!"},
|
37 |
+
}
|
38 |
+
hum_turn = play.get_human_player() == play.get_curr_player()
|
39 |
+
game_over = play.is_done()
|
40 |
+
choose1 = not game_over
|
41 |
+
choose2 = play.get_result() if game_over else hum_turn
|
42 |
+
return display[choose1][choose2]
|
43 |
+
|
44 |
+
def reset(t_p):
|
45 |
+
t_p.reset()
|
46 |
+
st.cache_resource.clear()
|
47 |
+
|
48 |
+
def play(bot_select, t_p):
|
49 |
play = HvB.get_hvb_manager(
|
50 |
_model=DCMinMax.from_pretrained("mzimm003/{}".format(bot_select)),
|
51 |
_environment=Chess(render_mode="rgb_array"),
|
52 |
_extra_model_environment_context=lambda env: {"board":env.board}
|
53 |
)
|
54 |
+
col1, col2 = st.columns([3,1])
|
55 |
+
with col1:
|
56 |
+
st.markdown("# {}".format(information(play, t_p)))
|
57 |
+
st.session_state["board"] = streamlit_image_coordinates(play.render_board(), key="brd", click_and_drag=True)
|
58 |
+
with col2:
|
59 |
+
st.button("Reset", on_click=reset, args=[t_p])
|
60 |
+
st.markdown("#### You are playing as {}!".format("white" if play.get_human_player() == "player_0" else "black"))
|
61 |
+
st.markdown("""
|
62 |
+
1. Play by dragging the piece you want to move.
|
63 |
+
2. Illegal moves will not be registered.
|
64 |
+
""")
|
65 |
+
if not play.is_done():
|
66 |
+
play.run()
|
67 |
|
68 |
def main(kwargs=None):
|
69 |
+
css='''
|
70 |
+
<style>
|
71 |
+
section.main > div {max-width:75rem}
|
72 |
+
</style>
|
73 |
+
'''
|
74 |
+
st.markdown(css, unsafe_allow_html=True)
|
75 |
+
|
76 |
+
t_p = track_play()
|
77 |
+
placeholder = st.empty()
|
78 |
+
|
79 |
+
if not t_p.active_game:
|
80 |
+
# st.cache_resource.clear()
|
81 |
+
with placeholder.container():
|
82 |
+
t_p.bot_select = st.selectbox(
|
83 |
+
label="bot",
|
84 |
+
options=["DeepChessReplicationMinMax"],
|
85 |
+
)
|
86 |
+
t_p.active_game = st.button("Play bot!")
|
87 |
+
if t_p.active_game:
|
88 |
+
st.rerun()
|
89 |
+
else:
|
90 |
+
with placeholder.container():
|
91 |
+
play(t_p.bot_select, t_p)
|
92 |
|
93 |
if __name__ == "__main__":
|
94 |
+
main()
|
|