Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import numpy as np
|
|
| 5 |
import os
|
| 6 |
import io
|
| 7 |
import time
|
| 8 |
-
import uuid
|
| 9 |
from collections import OrderedDict
|
| 10 |
from PIL import Image, ImageDraw, ImageFont
|
| 11 |
from RL_Chess_Alpha import AlphaChessNet, AlphaMCTS
|
|
@@ -46,7 +45,7 @@ def load_piece_images():
|
|
| 46 |
piece_images = load_piece_images()
|
| 47 |
|
| 48 |
# ==========================================
|
| 49 |
-
# 2. SESSION MANAGEMENT (One per
|
| 50 |
# ==========================================
|
| 51 |
class GameSession:
|
| 52 |
def __init__(self):
|
|
@@ -96,7 +95,9 @@ def render_board(session, status=""):
|
|
| 96 |
|
| 97 |
return img, status
|
| 98 |
|
| 99 |
-
def handle_click(evt: gr.SelectData,
|
|
|
|
|
|
|
| 100 |
session = get_session(sid)
|
| 101 |
x, y = evt.index
|
| 102 |
file, rank = x // 80, 7 - (y // 80)
|
|
@@ -149,22 +150,20 @@ def ai_turn(sid, sims):
|
|
| 149 |
|
| 150 |
return render_board(session, f"AI played {san}")
|
| 151 |
|
| 152 |
-
def reset_all(
|
|
|
|
| 153 |
session = get_session(sid)
|
| 154 |
session.board.reset()
|
| 155 |
session.selected_sq = None
|
| 156 |
-
# Reset MCTS to flush the search tree cache for the new game
|
| 157 |
session.mcts = AlphaMCTS(model, DEVICE)
|
| 158 |
return render_board(session, "Game Reset")
|
| 159 |
|
| 160 |
# ==========================================
|
| 161 |
# 4. GRADIO UI
|
| 162 |
# ==========================================
|
| 163 |
-
|
|
|
|
| 164 |
gr.Markdown("# ♟️ AlphaZero Tactical Engine - Skill level 3")
|
| 165 |
-
|
| 166 |
-
# Generate a unique Session ID for the specific browser tab
|
| 167 |
-
session_id = gr.State(lambda: str(uuid.uuid4()))
|
| 168 |
|
| 169 |
with gr.Row():
|
| 170 |
with gr.Column(scale=2):
|
|
@@ -181,7 +180,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 181 |
auto_go = gr.Checkbox(label="Enable Autoplay", value=True)
|
| 182 |
reset_btn = gr.Button("Reset Game")
|
| 183 |
|
| 184 |
-
def main_loop(is_auto, wm, bm, s,
|
|
|
|
| 185 |
session = get_session(sid)
|
| 186 |
if not is_auto or session.board.is_game_over():
|
| 187 |
return gr.update(), gr.update()
|
|
@@ -192,25 +192,25 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 192 |
|
| 193 |
return gr.update(), gr.update()
|
| 194 |
|
|
|
|
| 195 |
board_img.select(
|
| 196 |
handle_click,
|
| 197 |
-
inputs=[
|
| 198 |
outputs=[board_img, status_txt]
|
| 199 |
)
|
| 200 |
|
| 201 |
reset_btn.click(
|
| 202 |
reset_all,
|
| 203 |
-
inputs=[session_id],
|
| 204 |
outputs=[board_img, status_txt]
|
| 205 |
)
|
| 206 |
|
| 207 |
-
# 1-second timer to drive the AI turn
|
| 208 |
timer = gr.Timer(1.0)
|
| 209 |
timer.tick(
|
| 210 |
main_loop,
|
| 211 |
-
inputs=[auto_go, w_mode, b_mode, sims_val
|
| 212 |
outputs=[board_img, status_txt]
|
| 213 |
)
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
-
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
import io
|
| 7 |
import time
|
|
|
|
| 8 |
from collections import OrderedDict
|
| 9 |
from PIL import Image, ImageDraw, ImageFont
|
| 10 |
from RL_Chess_Alpha import AlphaChessNet, AlphaMCTS
|
|
|
|
| 45 |
piece_images = load_piece_images()
|
| 46 |
|
| 47 |
# ==========================================
|
| 48 |
+
# 2. SESSION MANAGEMENT (One per browser tab)
|
| 49 |
# ==========================================
|
| 50 |
class GameSession:
|
| 51 |
def __init__(self):
|
|
|
|
| 95 |
|
| 96 |
return img, status
|
| 97 |
|
| 98 |
+
def handle_click(evt: gr.SelectData, wm, bm, request: gr.Request):
|
| 99 |
+
# Grabs the unique ID for this browser tab
|
| 100 |
+
sid = request.session_hash
|
| 101 |
session = get_session(sid)
|
| 102 |
x, y = evt.index
|
| 103 |
file, rank = x // 80, 7 - (y // 80)
|
|
|
|
| 150 |
|
| 151 |
return render_board(session, f"AI played {san}")
|
| 152 |
|
| 153 |
+
def reset_all(request: gr.Request):
|
| 154 |
+
sid = request.session_hash
|
| 155 |
session = get_session(sid)
|
| 156 |
session.board.reset()
|
| 157 |
session.selected_sq = None
|
|
|
|
| 158 |
session.mcts = AlphaMCTS(model, DEVICE)
|
| 159 |
return render_board(session, "Game Reset")
|
| 160 |
|
| 161 |
# ==========================================
|
| 162 |
# 4. GRADIO UI
|
| 163 |
# ==========================================
|
| 164 |
+
# Note: Theme is now passed in launch() in Gradio 6.0
|
| 165 |
+
with gr.Blocks() as demo:
|
| 166 |
gr.Markdown("# ♟️ AlphaZero Tactical Engine - Skill level 3")
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
with gr.Row():
|
| 169 |
with gr.Column(scale=2):
|
|
|
|
| 180 |
auto_go = gr.Checkbox(label="Enable Autoplay", value=True)
|
| 181 |
reset_btn = gr.Button("Reset Game")
|
| 182 |
|
| 183 |
+
def main_loop(is_auto, wm, bm, s, request: gr.Request):
|
| 184 |
+
sid = request.session_hash
|
| 185 |
session = get_session(sid)
|
| 186 |
if not is_auto or session.board.is_game_over():
|
| 187 |
return gr.update(), gr.update()
|
|
|
|
| 192 |
|
| 193 |
return gr.update(), gr.update()
|
| 194 |
|
| 195 |
+
# Pass only the UI components to inputs, request object is injected automatically
|
| 196 |
board_img.select(
|
| 197 |
handle_click,
|
| 198 |
+
inputs=[w_mode, b_mode],
|
| 199 |
outputs=[board_img, status_txt]
|
| 200 |
)
|
| 201 |
|
| 202 |
reset_btn.click(
|
| 203 |
reset_all,
|
|
|
|
| 204 |
outputs=[board_img, status_txt]
|
| 205 |
)
|
| 206 |
|
|
|
|
| 207 |
timer = gr.Timer(1.0)
|
| 208 |
timer.tick(
|
| 209 |
main_loop,
|
| 210 |
+
inputs=[auto_go, w_mode, b_mode, sims_val],
|
| 211 |
outputs=[board_img, status_txt]
|
| 212 |
)
|
| 213 |
|
| 214 |
if __name__ == "__main__":
|
| 215 |
+
# Updated launch args for Gradio 6.0 and HF Spaces
|
| 216 |
+
demo.launch(theme=gr.themes.Soft())
|