Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import chess
|
3 |
+
import chess.svg
|
4 |
+
from gradio_client import Client
|
5 |
+
import cairosvg
|
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()
|
18 |
+
prompt = f"You are playing as Black in a chess game. The current board position in FEN notation is: {fen}. What is your next move? Please respond with the move in UCI notation (e.g., 'e2e4')."
|
19 |
+
|
20 |
+
result = client.predict(
|
21 |
+
message=prompt,
|
22 |
+
system_message="You are a chess engine based on LLaMA 405B. Provide only the next move in UCI notation.",
|
23 |
+
max_tokens=1024,
|
24 |
+
temperature=0.6,
|
25 |
+
top_p=0.9,
|
26 |
+
top_k=50,
|
27 |
+
api_name="/chat"
|
28 |
+
)
|
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 |
+
# Display the current board state
|
46 |
+
st.image(render_board(st.session_state.board))
|
47 |
+
|
48 |
+
# Get user's move
|
49 |
+
user_move = st.text_input("Enter your move (e.g., 'e2e4'):")
|
50 |
+
|
51 |
+
if st.button("Make Move"):
|
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!")
|
68 |
+
st.write(f"Result: {st.session_state.board.result()}")
|
69 |
+
else:
|
70 |
+
st.write("Invalid move. Please try again.")
|
71 |
+
except ValueError:
|
72 |
+
st.write("Invalid input. Please enter a move in UCI notation (e.g., 'e2e4').")
|
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
|
80 |
+
st.write(f"Current turn: {'White' if st.session_state.board.turn else 'Black'}")
|
81 |
+
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()}")
|