forked from vishalsubbiah/en-passant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_page.py
127 lines (113 loc) · 4.7 KB
/
main_page.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
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
118
119
120
121
122
123
124
125
126
127
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
import streamlit as st
import streamlit.components.v1 as components
import chess
# import streamlit_scrollable_textbox as stx
from gpt_script import ChatApp
from eleven_labs import ElevenVoice
from st_bridge import bridge
from modules.chess import Chess
from modules.utility import set_page
from modules.states import init_states
import os
import datetime as dt
set_page(title='Chess', page_icon="♟️")
init_states()
if "chat_app" not in st.session_state:
st.session_state.prev_data = ""
st.session_state.chat_app = ChatApp()
st.session_state.eleven_voice = ElevenVoice()
st.session_state.board_width = 500
# Get the info from current board after the user made the move.
# The data will return the move, fen and the pgn.
# The move contains the from sq, to square, and others.
data = bridge("my-bridge")
if not st.session_state.get('current_player') or st.session_state['current_player'] == 'Player 2':
st.session_state['current_player'] = 'Player 1'
else:
st.session_state['current_player'] = 'Player 2'
error = ""
print("default", f"{data=}", f"{st.session_state.prev_data=}")
if data is not None:
st.session_state.lastfen = st.session_state.curfen
st.session_state.curfen = data['fen']
st.session_state.curside = data['move']['color'].replace('w','white').replace('b','black')
st.session_state.moves.update(
{
st.session_state.curfen : {
'side':st.session_state.curside,
'curfen':st.session_state.curfen,
'last_fen':st.session_state.lastfen,
'last_move':data['pgn'],
'data': None,
'timestamp': str(dt.datetime.now()),
"current_player": st.session_state.current_player
}
}
)
pgn_entry = data["pgn"]
if st.session_state.prev_data != data["pgn"]:
print("before update", f"{data=}", f"{st.session_state.prev_data=}")
st.session_state.prev_data = data["pgn"]
print("inside if should match", f"{data=}", f"{st.session_state.prev_data=}")
message, persona, output = st.session_state.chat_app.chat(pgn_entry)
from guardrails.hub import NSFWText
from guardrails import Guard
# Use the Guard with the validator
guard = Guard().use(
NSFWText, threshold=0.8, validation_method="sentence", on_fail="exception"
)
# Test passing response
try:
guard.validate(output)
except Exception as e:
error = str(e)
print(e)
if persona=="Gordon Ramsey":
voice_id = os.environ["GORDON_VOICE_ID"]
elif persona=="Aziz Ansari":
voice_id = os.environ["AZIZ_VOICE_ID"]
st.session_state.eleven_voice.generate_and_play_audio(output, voice_id)
if st.button('start new game'):
print("pressing start new game")
puzzle = None
data = None
init_states()
cols = st.columns([1, 1])
with cols[0]:
puzzle = Chess(st.session_state.board_width, "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
components.html(
puzzle.puzzle_board(),
height=st.session_state.board_width + 75,
scrolling=False
)
board = chess.Board(st.session_state.curfen)
outcome = board.outcome()
# st.warning(f'is_check: {board.is_check()}')
# st.warning(f'is_checkmate: {board.is_checkmate()}')
# st.warning(f'is_stalemate: {board.is_stalemate()}')
# st.warning(f'is_insufficient_material: {board.is_insufficient_material()}')
# if outcome:
# st.warning(f"Winner: { {True:'White',False:'Black'}.get(outcome.winner) }")
with cols[1]:
st.markdown(
"""
<div style="margin-top: 7.5px;"> <!-- Adjust the top margin as needed -->
<img src="https://github.com/vishalsubbiah/pearvc_hackathon_chess_commentry/blob/vishal/integ_lichess/images.jpeg?raw=true" alt="Gordon" style="width:100%"> <!-- Ensure the image path is correct -->
</div>
""",
unsafe_allow_html=True
)
# Apply the custom CSS class to the scrollable textbox container
st.markdown(
'<div class="scrollable-textbox-top-margin">',
unsafe_allow_html=True
)
# with st.container():
# records = ["MOVES\n--------------------------------"]
# records.append(error)
# # html( "<p>" + '\n\n'.join(records) + "</p>", scrolling=True)
# for key, value in list(st.session_state['moves'].items())[1:]:
# records.append( ('Player 1' if value['current_player'] == 'Player 2'else 'Player 2') + ' played ' + value['last_move'][-2:])
# stx.scrollableTextbox('\n\n'.join(records), height = 500, border=True)