Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit_js_eval import streamlit_js_eval
|
3 |
import chess
|
|
|
4 |
from gradio_client import Client
|
|
|
|
|
|
|
5 |
|
6 |
# Initialize the Gradio client
|
7 |
client = Client("xianbao/SambaNova-fast")
|
@@ -23,83 +26,35 @@ def get_ai_move(board):
|
|
23 |
|
24 |
return result.strip()
|
25 |
|
|
|
|
|
|
|
|
|
|
|
26 |
# Streamlit app
|
27 |
st.set_page_config(layout="wide")
|
28 |
-
st.title("
|
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 |
-
st.components.v1.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 |
-
|
48 |
-
function onDragStart (source, piece, position, orientation) {
|
49 |
-
if (game.game_over()) return false
|
50 |
-
if (piece.search(/^b/) !== -1) return false
|
51 |
-
}
|
52 |
-
|
53 |
-
function onDrop (source, target) {
|
54 |
-
var move = game.move({
|
55 |
-
from: source,
|
56 |
-
to: target,
|
57 |
-
promotion: 'q'
|
58 |
-
})
|
59 |
-
|
60 |
-
if (move === null) return 'snapback'
|
61 |
-
|
62 |
-
// Send move to Python
|
63 |
-
window.parent.streamlit.setComponentValue(move.from + move.to)
|
64 |
-
}
|
65 |
-
|
66 |
-
function onSnapEnd () {
|
67 |
-
board.position(game.fen())
|
68 |
-
}
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
onDragStart: onDragStart,
|
74 |
-
onDrop: onDrop,
|
75 |
-
onSnapEnd: onSnapEnd
|
76 |
-
}
|
77 |
-
board = Chessboard('board', config)
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
game.move(move)
|
82 |
-
board.position(game.fen())
|
83 |
-
}
|
84 |
|
85 |
-
|
86 |
-
window.makeMove = makeMove
|
87 |
-
</script>
|
88 |
-
""",
|
89 |
-
height=450,
|
90 |
-
)
|
91 |
-
|
92 |
-
# Display the interactive chessboard
|
93 |
-
chessboard_component()
|
94 |
-
|
95 |
-
# Handle user moves
|
96 |
-
move = streamlit_js_eval(js_expressions='parent.window.streamlit.getComponentValue()', key='get_move')
|
97 |
-
|
98 |
-
if move:
|
99 |
try:
|
100 |
-
|
101 |
-
if
|
102 |
-
st.session_state.board.push(
|
|
|
|
|
|
|
|
|
103 |
|
104 |
if not st.session_state.board.is_game_over():
|
105 |
with st.spinner("AI is thinking..."):
|
@@ -108,8 +63,9 @@ if move:
|
|
108 |
st.session_state.board.push(ai_move_obj)
|
109 |
st.write(f"AI's move: {ai_move}")
|
110 |
|
111 |
-
#
|
112 |
-
|
|
|
113 |
|
114 |
if st.session_state.board.is_game_over():
|
115 |
st.write("Game Over!")
|
@@ -117,18 +73,24 @@ if move:
|
|
117 |
else:
|
118 |
st.write("Invalid move. Please try again.")
|
119 |
except ValueError:
|
120 |
-
st.write("Invalid input. Please
|
121 |
|
122 |
# Reset button
|
123 |
if st.button("Reset Game"):
|
124 |
st.session_state.board = chess.Board()
|
125 |
-
|
126 |
-
st.rerun()
|
127 |
|
128 |
# Display game status
|
129 |
st.write(f"Current turn: {'White' if st.session_state.board.turn else 'Black'}")
|
130 |
st.write(f"Fullmove number: {st.session_state.board.fullmove_number}")
|
131 |
-
st.write(f"
|
132 |
-
st.write(f"Is
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import chess
|
3 |
+
import chess.svg
|
4 |
from gradio_client import Client
|
5 |
+
from cairosvg import svg2png
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
|
9 |
# Initialize the Gradio client
|
10 |
client = Client("xianbao/SambaNova-fast")
|
|
|
26 |
|
27 |
return result.strip()
|
28 |
|
29 |
+
# Function to convert SVG to PNG
|
30 |
+
def svg_to_png(svg_string):
|
31 |
+
png_bytes = svg2png(bytestring=svg_string.encode('utf-8'))
|
32 |
+
return Image.open(io.BytesIO(png_bytes))
|
33 |
+
|
34 |
# Streamlit app
|
35 |
st.set_page_config(layout="wide")
|
36 |
+
st.title("Chess against LLaMA 405B")
|
37 |
|
38 |
# Initialize session state
|
39 |
if 'board' not in st.session_state:
|
40 |
st.session_state.board = chess.Board()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Display the chessboard
|
43 |
+
svg_board = chess.svg.board(board=st.session_state.board)
|
44 |
+
st.image(svg_to_png(svg_board), width=400)
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Input for user's move
|
47 |
+
user_move = st.text_input("Enter your move (e.g., 'e2e4'):")
|
|
|
|
|
|
|
48 |
|
49 |
+
if st.button("Make Move"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
try:
|
51 |
+
move = chess.Move.from_uci(user_move)
|
52 |
+
if move in st.session_state.board.legal_moves:
|
53 |
+
st.session_state.board.push(move)
|
54 |
+
|
55 |
+
# Display updated board
|
56 |
+
svg_board = chess.svg.board(board=st.session_state.board)
|
57 |
+
st.image(svg_to_png(svg_board), width=400)
|
58 |
|
59 |
if not st.session_state.board.is_game_over():
|
60 |
with st.spinner("AI is thinking..."):
|
|
|
63 |
st.session_state.board.push(ai_move_obj)
|
64 |
st.write(f"AI's move: {ai_move}")
|
65 |
|
66 |
+
# Display updated board after AI move
|
67 |
+
svg_board = chess.svg.board(board=st.session_state.board)
|
68 |
+
st.image(svg_to_png(svg_board), width=400)
|
69 |
|
70 |
if st.session_state.board.is_game_over():
|
71 |
st.write("Game Over!")
|
|
|
73 |
else:
|
74 |
st.write("Invalid move. Please try again.")
|
75 |
except ValueError:
|
76 |
+
st.write("Invalid input. Please enter a move in UCI notation (e.g., 'e2e4').")
|
77 |
|
78 |
# Reset button
|
79 |
if st.button("Reset Game"):
|
80 |
st.session_state.board = chess.Board()
|
81 |
+
st.experimental_rerun()
|
|
|
82 |
|
83 |
# Display game status
|
84 |
st.write(f"Current turn: {'White' if st.session_state.board.turn else 'Black'}")
|
85 |
st.write(f"Fullmove number: {st.session_state.board.fullmove_number}")
|
86 |
+
st.write(f"Is check? {'Yes' if st.session_state.board.is_check() else 'No'}")
|
87 |
+
st.write(f"Is game over? {'Yes' if st.session_state.board.is_game_over() else 'No'}")
|
88 |
+
|
89 |
+
# Hide the default Streamlit watermark
|
90 |
+
hide_streamlit_style = """
|
91 |
+
<style>
|
92 |
+
#MainMenu {visibility: hidden;}
|
93 |
+
footer {visibility: hidden;}
|
94 |
+
</style>
|
95 |
+
"""
|
96 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|