Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,62 @@
|
|
1 |
import gradio as gr
|
2 |
import chess
|
3 |
import chess.svg
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
def make_move(move_san, board_fen):
|
10 |
-
board = chess.Board(fen=board_fen)
|
11 |
try:
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
svg_data = chess.svg.board(board=board, size=400)
|
26 |
-
png_data = cairosvg.svg2png(bytestring=svg_data)
|
27 |
-
image = Image.open(io.BytesIO(png_data))
|
28 |
-
return image, board.fen(), message
|
29 |
else:
|
30 |
-
|
|
|
31 |
except Exception as e:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import chess
|
3 |
import chess.svg
|
4 |
+
import os
|
5 |
+
import google.generativeai as genai
|
6 |
+
|
7 |
+
# Configure the Google Generative AI client
|
8 |
+
genai.configure(api_key=os.environ["API_KEY"])
|
9 |
+
|
10 |
+
board = chess.Board()
|
11 |
+
|
12 |
+
def get_ai_move(fen):
|
13 |
+
"""
|
14 |
+
Gets the AI's next move using Google Gemini via google.generativeai.
|
15 |
+
|
16 |
+
Args:
|
17 |
+
fen (str): The current board position in FEN notation.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
str or None: The AI's move in UCI format, or None if an error occurs.
|
21 |
+
"""
|
22 |
+
# Prepare the prompt for the AI
|
23 |
+
prompt = f"""You are a chess engine. Given the following board position in FEN notation:
|
24 |
+
{fen}
|
25 |
+
It's your turn to move. Provide the best move in UCI format (e.g., 'e2e4'). Only provide the move, without any additional text."""
|
26 |
|
|
|
|
|
27 |
try:
|
28 |
+
# Generate the response from the AI model
|
29 |
+
response = genai.generate_text(
|
30 |
+
model="gemini-1.5-flash", # Use the Gemini model
|
31 |
+
prompt=prompt,
|
32 |
+
temperature=0, # To make the output more deterministic
|
33 |
+
max_output_tokens=5
|
34 |
+
)
|
35 |
+
ai_move = response.result.strip()
|
36 |
+
|
37 |
+
# Validate the move
|
38 |
+
legal_moves = [move.uci() for move in board.legal_moves]
|
39 |
+
if ai_move in legal_moves:
|
40 |
+
return ai_move
|
|
|
|
|
|
|
|
|
41 |
else:
|
42 |
+
print(f"Invalid move received from the AI: {ai_move}")
|
43 |
+
return None
|
44 |
except Exception as e:
|
45 |
+
print(f"Error occurred: {e}")
|
46 |
+
return None
|
47 |
+
|
48 |
+
def board_to_svg(board):
|
49 |
+
return chess.svg.board(board=board)
|
50 |
+
|
51 |
+
def make_move(user_move):
|
52 |
+
global board
|
53 |
+
try:
|
54 |
+
# Player's move
|
55 |
+
board.push_san(user_move)
|
56 |
+
except ValueError:
|
57 |
+
return gr.update(value=board_to_svg(board)), "Invalid move. Try again."
|
58 |
+
|
59 |
+
# Check for game over
|
60 |
+
if board.is_game_over():
|
61 |
+
result = board.result()
|
62 |
+
board_svg = board_to_svg(b
|
|
|
|