Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from game_logic import AaduPulliEnv, NeuralNetwork, AlphaZeroAgent
|
| 5 |
+
|
| 6 |
+
REPO_ID = "AaduPulliAttam/apa-ray"
|
| 7 |
+
MODEL_FILENAME = "alphazero_aadu_pulli_ray.weights.h5"
|
| 8 |
+
HUMAN_PLAYER = 0 # 0 for Goat, 1 for Tiger
|
| 9 |
+
AI_PLAYER = 1
|
| 10 |
+
|
| 11 |
+
ai_agent = None
|
| 12 |
+
try:
|
| 13 |
+
print(f"Downloading model from {REPO_ID}...")
|
| 14 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
|
| 15 |
+
print(f"Model downloaded to: {model_path}")
|
| 16 |
+
|
| 17 |
+
env_for_init = AaduPulliEnv()
|
| 18 |
+
action_space_size = env_for_init.action_space.n
|
| 19 |
+
neural_net = NeuralNetwork(action_space_size)
|
| 20 |
+
|
| 21 |
+
neural_net.model.load_weights(model_path)
|
| 22 |
+
print("Model weights loaded successfully from the Hub.")
|
| 23 |
+
|
| 24 |
+
ai_agent = AlphaZeroAgent(env_for_init, neural_net, simulations_per_move=50)
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Could not load model from the Hub. AI will be disabled. Error: {e}")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def new_game():
|
| 31 |
+
env = AaduPulliEnv()
|
| 32 |
+
env.reset()
|
| 33 |
+
board_image = env.render()
|
| 34 |
+
message = "New game started. You are the Goat (Blue). Make your first placement."
|
| 35 |
+
return env, board_image, message, False
|
| 36 |
+
|
| 37 |
+
def get_action_from_str(env, move_str):
|
| 38 |
+
try:
|
| 39 |
+
is_placement_phase = env.goats_placed_count < env.NUM_GOATS
|
| 40 |
+
if is_placement_phase and env.player_turn == HUMAN_PLAYER:
|
| 41 |
+
pos = int(move_str)
|
| 42 |
+
if not 1 <= pos <= env.BOARD_POSITIONS:
|
| 43 |
+
return None, "Invalid position. Enter a number on the board."
|
| 44 |
+
return pos - 1, None # Convert to 0-indexed action
|
| 45 |
+
else:
|
| 46 |
+
parts = move_str.split(',')
|
| 47 |
+
if len(parts) != 2:
|
| 48 |
+
return None, "Invalid move format. Use 'from,to' (e.g., '12,13')."
|
| 49 |
+
from_pos, to_pos = int(parts[0]), int(parts[1])
|
| 50 |
+
move_tuple = (from_pos, to_pos)
|
| 51 |
+
if move_tuple not in env._move_action_lookup:
|
| 52 |
+
return None, "Invalid move. That path does not exist."
|
| 53 |
+
action = env.placement_actions + env._move_action_lookup[move_tuple]
|
| 54 |
+
return action, None
|
| 55 |
+
except (ValueError, IndexError):
|
| 56 |
+
return None, "Invalid input. Please enter a position number or a 'from,to' move."
|
| 57 |
+
|
| 58 |
+
def check_game_over(info):
|
| 59 |
+
winner = info.get('winner', -1)
|
| 60 |
+
if winner != -1:
|
| 61 |
+
if winner == HUMAN_PLAYER:
|
| 62 |
+
return "Congratulations, you won!", True
|
| 63 |
+
elif winner == AI_PLAYER:
|
| 64 |
+
return "The AI has won. Better luck next time!", True
|
| 65 |
+
else:
|
| 66 |
+
return "The game is a draw!", True
|
| 67 |
+
return None, False
|
| 68 |
+
|
| 69 |
+
def play_turn(move_str, env, game_over):
|
| 70 |
+
if game_over:
|
| 71 |
+
return env, env.render(), "The game is over. Please start a new game.", True
|
| 72 |
+
|
| 73 |
+
if not ai_agent:
|
| 74 |
+
return env, env.render(), "AI Agent is not available. Please check the logs.", True
|
| 75 |
+
|
| 76 |
+
if not move_str:
|
| 77 |
+
return env, env.render(), "Please enter a move.", False
|
| 78 |
+
|
| 79 |
+
# Human's Turn
|
| 80 |
+
action, error_msg = get_action_from_str(env, move_str)
|
| 81 |
+
if error_msg:
|
| 82 |
+
return env, env.render(), error_msg, False
|
| 83 |
+
|
| 84 |
+
is_valid, details = env.is_action_valid(action)
|
| 85 |
+
if not is_valid:
|
| 86 |
+
return env, env.render(), f"Invalid Move: {details.get('error', 'Unknown error')}", False
|
| 87 |
+
|
| 88 |
+
state, _, done, info = env.step(action)
|
| 89 |
+
message, game_over = check_game_over(info)
|
| 90 |
+
if game_over:
|
| 91 |
+
return env, env.render(), message, True
|
| 92 |
+
|
| 93 |
+
# AI's Turn
|
| 94 |
+
ai_action = ai_agent.get_action(state, training=False)
|
| 95 |
+
state, _, done, info = env.step(ai_action)
|
| 96 |
+
|
| 97 |
+
action_type, details_txt = "Placement", ai_action
|
| 98 |
+
if ai_action >= env.placement_actions:
|
| 99 |
+
action_type, details_txt = "Move", env._move_action_map[ai_action - env.placement_actions]
|
| 100 |
+
|
| 101 |
+
message, game_over = check_game_over(info)
|
| 102 |
+
if game_over:
|
| 103 |
+
return env, env.render(), message, True
|
| 104 |
+
|
| 105 |
+
message = f"AI played {action_type} {details_txt}. Your turn."
|
| 106 |
+
return env, env.render(), message, False
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 110 |
+
game_state = gr.State()
|
| 111 |
+
game_over_state = gr.State(False)
|
| 112 |
+
|
| 113 |
+
gr.Markdown("# Aadu Puli Aattam: Human vs AlphaZero AI")
|
| 114 |
+
gr.Markdown("You play as the Goats (Blue). Your goal is to trap the three Tigers (Red). The AI plays as the Tigers, trying to capture 10 of your goats.")
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column(scale=2):
|
| 118 |
+
board_image = gr.Image(label="Game Board", interactive=False)
|
| 119 |
+
with gr.Column(scale=1):
|
| 120 |
+
status_message = gr.Markdown(label="Game Status")
|
| 121 |
+
move_input = gr.Textbox(label="Your Move", placeholder="Enter placement # or 'from,to'")
|
| 122 |
+
submit_button = gr.Button("Submit Move")
|
| 123 |
+
new_game_button = gr.Button("New Game")
|
| 124 |
+
|
| 125 |
+
demo.load(new_game, outputs=[game_state, board_image, status_message, game_over_state])
|
| 126 |
+
|
| 127 |
+
submit_button.click(
|
| 128 |
+
fn=play_turn,
|
| 129 |
+
inputs=[move_input, game_state, game_over_state],
|
| 130 |
+
outputs=[game_state, board_image, status_message, game_over_state]
|
| 131 |
+
).then(
|
| 132 |
+
fn=lambda: "",
|
| 133 |
+
outputs=[move_input]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
new_game_button.click(
|
| 137 |
+
fn=new_game,
|
| 138 |
+
outputs=[game_state, board_image, status_message, game_over_state]
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
demo.launch(share=True)
|