Spaces:
Running on Zero
Running on Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import chess
|
| 3 |
+
import chess.svg
|
| 4 |
+
from transformers import AutoModel, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
# โหลดโมเดล
|
| 7 |
+
repo_id = "Datdanboi25/Charles-the-Chess-Bot"
|
| 8 |
+
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
|
| 10 |
+
|
| 11 |
+
board = chess.Board()
|
| 12 |
+
|
| 13 |
+
def make_move(user_move):
|
| 14 |
+
global board
|
| 15 |
+
try:
|
| 16 |
+
# 1. ผู้เล่นเดินหมาก (เช่น e2e4)
|
| 17 |
+
move = chess.Move.from_uci(user_move)
|
| 18 |
+
if move in board.legal_moves:
|
| 19 |
+
board.push(move)
|
| 20 |
+
else:
|
| 21 |
+
return "ตาเดินไม่ถูกต้องตามกฎ!", chess.svg.board(board)
|
| 22 |
+
|
| 23 |
+
# ตรวจสอบการจบเกม
|
| 24 |
+
if board.is_game_over():
|
| 25 |
+
return "เกมจบแล้ว!", chess.svg.board(board)
|
| 26 |
+
|
| 27 |
+
# 2. บอทประมวลผลตาเดินถัดไป
|
| 28 |
+
# (ปรับเปลี่ยนส่วนนี้ให้ตรงกับข้อกำหนด Input/Output ของ Charles-the-Chess-Bot)
|
| 29 |
+
inputs = tokenizer(board.fen(), return_tensors="pt")
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
|
| 32 |
+
# สมมุติให้ bot_move_uci เป็นตาเดินที่บอทเลือก
|
| 33 |
+
# board.push(chess.Move.from_uci(bot_move_uci))
|
| 34 |
+
|
| 35 |
+
svg_board = chess.svg.board(board)
|
| 36 |
+
return "ตาของคุณเดินต่อไป", svg_board
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"เกิดข้อผิดพลาด: {str(e)}", chess.svg.board(board)
|
| 39 |
+
|
| 40 |
+
# สร้าง Interface
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# ซ้อมหมากรุกกับ Charles the Chess Bot")
|
| 43 |
+
board_html = gr.HTML(chess.svg.board(board))
|
| 44 |
+
move_input = gr.Textbox(label="ใส่ตาเดินแบบ UCI (เช่น e2e4, g1f3):")
|
| 45 |
+
submit_btn = gr.Button("เดินหมาก")
|
| 46 |
+
status_text = gr.Textbox(label="สถานะ")
|
| 47 |
+
|
| 48 |
+
submit_btn.click(make_move, inputs=[move_input], outputs=[status_text, board_html])
|
| 49 |
+
|
| 50 |
+
demo.launch()
|