Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,12 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import chess
|
3 |
-
import chess.svg
|
4 |
from gradio_client import Client
|
5 |
-
import
|
6 |
-
from PIL import Image
|
7 |
-
import io
|
8 |
|
9 |
# Initialize the Gradio client
|
10 |
client = Client("xianbao/SambaNova-fast")
|
11 |
|
12 |
-
# Initialize the chess board
|
13 |
-
board = chess.Board()
|
14 |
-
|
15 |
# Function to get AI move
|
16 |
def get_ai_move(board):
|
17 |
fen = board.fen()
|
@@ -29,39 +24,134 @@ def get_ai_move(board):
|
|
29 |
|
30 |
return result.strip()
|
31 |
|
32 |
-
# Function to render the chess board
|
33 |
-
def render_board(board):
|
34 |
-
svg = chess.svg.board(board=board)
|
35 |
-
png = cairosvg.svg2png(bytestring=svg.encode('utf-8'))
|
36 |
-
return Image.open(io.BytesIO(png))
|
37 |
-
|
38 |
# Streamlit app
|
39 |
-
st.title("Chess against LLaMA 405B")
|
40 |
|
41 |
# Initialize session state
|
42 |
if 'board' not in st.session_state:
|
43 |
st.session_state.board = chess.Board()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
try:
|
53 |
-
move = chess.Move.from_uci(user_move)
|
54 |
if move in st.session_state.board.legal_moves:
|
55 |
st.session_state.board.push(move)
|
56 |
-
st.image(render_board(st.session_state.board))
|
57 |
|
58 |
if not st.session_state.board.is_game_over():
|
59 |
with st.spinner("AI is thinking..."):
|
60 |
ai_move = get_ai_move(st.session_state.board)
|
61 |
ai_move_obj = chess.Move.from_uci(ai_move)
|
62 |
st.session_state.board.push(ai_move_obj)
|
63 |
-
st.image(render_board(st.session_state.board))
|
64 |
st.write(f"AI's move: {ai_move}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
if st.session_state.board.is_game_over():
|
67 |
st.write("Game Over!")
|
@@ -69,11 +159,14 @@ if st.button("Make Move"):
|
|
69 |
else:
|
70 |
st.write("Invalid move. Please try again.")
|
71 |
except ValueError:
|
72 |
-
st.write("Invalid input. Please
|
|
|
|
|
73 |
|
74 |
# Reset button
|
75 |
if st.button("Reset Game"):
|
76 |
st.session_state.board = chess.Board()
|
|
|
77 |
st.experimental_rerun()
|
78 |
|
79 |
# Display game status
|
@@ -82,4 +175,35 @@ st.write(f"Fullmove number: {st.session_state.board.fullmove_number}")
|
|
82 |
st.write(f"Halfmove clock: {st.session_state.board.halfmove_clock}")
|
83 |
st.write(f"Is check? {st.session_state.board.is_check()}")
|
84 |
st.write(f"Is checkmate? {st.session_state.board.is_checkmate()}")
|
85 |
-
st.write(f"Is stalemate? {st.session_state.board.is_stalemate()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import streamlit.components.v1 as components
|
3 |
import chess
|
|
|
4 |
from gradio_client import Client
|
5 |
+
import json
|
|
|
|
|
6 |
|
7 |
# Initialize the Gradio client
|
8 |
client = Client("xianbao/SambaNova-fast")
|
9 |
|
|
|
|
|
|
|
10 |
# Function to get AI move
|
11 |
def get_ai_move(board):
|
12 |
fen = board.fen()
|
|
|
24 |
|
25 |
return result.strip()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Streamlit app
|
28 |
+
st.title("Interactive Chess against LLaMA 405B")
|
29 |
|
30 |
# Initialize session state
|
31 |
if 'board' not in st.session_state:
|
32 |
st.session_state.board = chess.Board()
|
33 |
+
if 'user_move' not in st.session_state:
|
34 |
+
st.session_state.user_move = None
|
35 |
+
|
36 |
+
# Create a custom component for the chessboard
|
37 |
+
def chessboard_component():
|
38 |
+
components.html(
|
39 |
+
"""
|
40 |
+
<div id="board" style="width: 400px"></div>
|
41 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.3/chess.min.js"></script>
|
42 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/chessboard-js/1.0.0/chessboard-1.0.0.min.js"></script>
|
43 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chessboard-js/1.0.0/chessboard-1.0.0.min.css">
|
44 |
+
<script>
|
45 |
+
var board = null
|
46 |
+
var game = new Chess()
|
47 |
+
var $status = $('#status')
|
48 |
+
var $fen = $('#fen')
|
49 |
+
var $pgn = $('#pgn')
|
50 |
+
|
51 |
+
function onDragStart (source, piece, position, orientation) {
|
52 |
+
if (game.game_over()) return false
|
53 |
+
if (piece.search(/^b/) !== -1) return false
|
54 |
+
}
|
55 |
+
|
56 |
+
function onDrop (source, target) {
|
57 |
+
var move = game.move({
|
58 |
+
from: source,
|
59 |
+
to: target,
|
60 |
+
promotion: 'q'
|
61 |
+
})
|
62 |
+
|
63 |
+
if (move === null) return 'snapback'
|
64 |
+
updateStatus()
|
65 |
+
|
66 |
+
// Send move to Python
|
67 |
+
window.parent.postMessage({
|
68 |
+
type: 'move',
|
69 |
+
move: move.from + move.to
|
70 |
+
}, '*')
|
71 |
+
}
|
72 |
+
|
73 |
+
function onSnapEnd () {
|
74 |
+
board.position(game.fen())
|
75 |
+
}
|
76 |
+
|
77 |
+
function updateStatus () {
|
78 |
+
var status = ''
|
79 |
+
|
80 |
+
var moveColor = 'White'
|
81 |
+
if (game.turn() === 'b') {
|
82 |
+
moveColor = 'Black'
|
83 |
+
}
|
84 |
|
85 |
+
if (game.in_checkmate()) {
|
86 |
+
status = 'Game over, ' + moveColor + ' is in checkmate.'
|
87 |
+
}
|
88 |
+
else if (game.in_draw()) {
|
89 |
+
status = 'Game over, drawn position'
|
90 |
+
}
|
91 |
+
else {
|
92 |
+
status = moveColor + ' to move'
|
93 |
+
if (game.in_check()) {
|
94 |
+
status += ', ' + moveColor + ' is in check'
|
95 |
+
}
|
96 |
+
}
|
97 |
|
98 |
+
$status.html(status)
|
99 |
+
$fen.html(game.fen())
|
100 |
+
$pgn.html(game.pgn())
|
101 |
+
}
|
102 |
|
103 |
+
var config = {
|
104 |
+
draggable: true,
|
105 |
+
position: 'start',
|
106 |
+
onDragStart: onDragStart,
|
107 |
+
onDrop: onDrop,
|
108 |
+
onSnapEnd: onSnapEnd
|
109 |
+
}
|
110 |
+
board = Chessboard('board', config)
|
111 |
+
|
112 |
+
updateStatus()
|
113 |
+
|
114 |
+
// Listen for moves from Python
|
115 |
+
window.addEventListener('message', function(event) {
|
116 |
+
if (event.data.type === 'move') {
|
117 |
+
game.move(event.data.move)
|
118 |
+
board.position(game.fen())
|
119 |
+
updateStatus()
|
120 |
+
}
|
121 |
+
})
|
122 |
+
</script>
|
123 |
+
""",
|
124 |
+
height=450,
|
125 |
+
)
|
126 |
+
|
127 |
+
# Display the interactive chessboard
|
128 |
+
chessboard_component()
|
129 |
+
|
130 |
+
# Handle user moves
|
131 |
+
if st.session_state.user_move:
|
132 |
try:
|
133 |
+
move = chess.Move.from_uci(st.session_state.user_move)
|
134 |
if move in st.session_state.board.legal_moves:
|
135 |
st.session_state.board.push(move)
|
|
|
136 |
|
137 |
if not st.session_state.board.is_game_over():
|
138 |
with st.spinner("AI is thinking..."):
|
139 |
ai_move = get_ai_move(st.session_state.board)
|
140 |
ai_move_obj = chess.Move.from_uci(ai_move)
|
141 |
st.session_state.board.push(ai_move_obj)
|
|
|
142 |
st.write(f"AI's move: {ai_move}")
|
143 |
+
|
144 |
+
# Send AI move to JavaScript
|
145 |
+
st.components.v1.html(
|
146 |
+
f"""
|
147 |
+
<script>
|
148 |
+
window.parent.postMessage({{
|
149 |
+
type: 'move',
|
150 |
+
move: '{ai_move}'
|
151 |
+
}}, '*')
|
152 |
+
</script>
|
153 |
+
"""
|
154 |
+
)
|
155 |
|
156 |
if st.session_state.board.is_game_over():
|
157 |
st.write("Game Over!")
|
|
|
159 |
else:
|
160 |
st.write("Invalid move. Please try again.")
|
161 |
except ValueError:
|
162 |
+
st.write("Invalid input. Please make a move on the board.")
|
163 |
+
|
164 |
+
st.session_state.user_move = None
|
165 |
|
166 |
# Reset button
|
167 |
if st.button("Reset Game"):
|
168 |
st.session_state.board = chess.Board()
|
169 |
+
st.session_state.user_move = None
|
170 |
st.experimental_rerun()
|
171 |
|
172 |
# Display game status
|
|
|
175 |
st.write(f"Halfmove clock: {st.session_state.board.halfmove_clock}")
|
176 |
st.write(f"Is check? {st.session_state.board.is_check()}")
|
177 |
st.write(f"Is checkmate? {st.session_state.board.is_checkmate()}")
|
178 |
+
st.write(f"Is stalemate? {st.session_state.board.is_stalemate()}")
|
179 |
+
|
180 |
+
# JavaScript to Python communication
|
181 |
+
components.html(
|
182 |
+
"""
|
183 |
+
<script>
|
184 |
+
window.addEventListener('message', function(event) {
|
185 |
+
if (event.data.type === 'move') {
|
186 |
+
window.parent.postMessage({
|
187 |
+
type: 'streamlit:set_widget_value',
|
188 |
+
widgetType: 'custom',
|
189 |
+
widgetName: 'user_move',
|
190 |
+
value: event.data.move,
|
191 |
+
}, '*')
|
192 |
+
}
|
193 |
+
})
|
194 |
+
</script>
|
195 |
+
"""
|
196 |
+
)
|
197 |
+
|
198 |
+
# Python to JavaScript communication
|
199 |
+
if st.session_state.user_move:
|
200 |
+
components.html(
|
201 |
+
f"""
|
202 |
+
<script>
|
203 |
+
window.parent.postMessage({{
|
204 |
+
type: 'move',
|
205 |
+
move: '{st.session_state.user_move}'
|
206 |
+
}}, '*')
|
207 |
+
</script>
|
208 |
+
"""
|
209 |
+
)
|