forked from terrisbecker/codenames-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
87 lines (65 loc) · 2.83 KB
/
streamlit_app.py
1
2
3
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
import streamlit as st
import pandas as pd
import numpy as np
from game_instance import Codenames
import dataclasses
def reset_game_state():
return True
def color_styling(word, red_words, blue_words, black_word):
if word in red_words:
return 'background-color: #d63636;'
elif word in blue_words:
return 'background-color: #3e55ed;'
elif word in black_word:
return 'background-color: black;'
else:
return 'background-color: #808080;'
@dataclasses.dataclass
class GameState:
color: int
role: int = 0
game_number: int = 0
new_game: bool = False
def get_guess_probability(word, game):
ret = pd.DataFrame(columns=['similarity', 'color'])
guess_word = game.model(word)
nlp = game.model
temp = game.similarity_matrix
ret.loc[:, 'similarity'] = [guess_word.similarity(nlp(str(i))) for i in game.board.flatten()]
ret.index = game.board.flatten()
ret.loc[:, 'color'] = game.color_board.flatten()
return ret.sort_values(by='similarity', ascending=False)
if __name__ == '__main__':
st.title('Welcome to Codenames with a bot!')
st.write('this game is played with an NLP partner and is designed to help you understand word vectors!')
st.write('To see how this works in detail, check out my walkthrough on [deepnote](https://deepnote.com/@travis-barton-d396/Computerizing-Codenames-Deepnote-SpaCy-Competition-S6lZs6DpRym8sh2UFUQilA)')
new_game_button = st.button("new game")
if new_game_button:
GameState.new_game = True
st.write("loading new game")
st.cache(allow_output_mutation=True)
color = st.sidebar.selectbox('Color', (None, 'Red', 'Blue'))
role = st.sidebar.selectbox('Role', (None, 'Spymaster', 'Agent'))
show_prob = st.sidebar.checkbox("show word similarities")
game = Codenames(True if role == "Spymaster" else False)
if GameState.new_game:
game.load_board()
GameState.new_game = False
if color is not None and role is not None:
if role == 'Spymaster':
la = pd.DataFrame(game.board).style.applymap(color_styling,
red_words=game.red_words,
blue_words=game.blue_words,
black_word=game.black_word)
st.dataframe(la, )
guess = [st.text_input("Enter a word")]
else:
hint = game.hint_giver(color=color)
st.dataframe(game.board)
st.write(f'hint: {hint[0][0]}, {hint[1]}')
guess = st.text_input("Enter your guesses separated by a comma")
guess = guess.split(',')
if show_prob:
for g in guess:
st.sidebar.write(f'guess: {g}')
st.sidebar.dataframe(get_guess_probability(g, game))