Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
|
4 |
-
# Simulated AI models for checkers
|
|
|
|
|
5 |
class BasicAI:
|
6 |
def move(self, board):
|
7 |
# Basic AI makes a random valid move
|
@@ -22,10 +24,12 @@ class AdvancedAI:
|
|
22 |
# Simulate valid moves as random pairs of coordinates
|
23 |
return [(random.randint(0, 7), random.randint(0, 7)) for _ in range(random.randint(1, 5))]
|
24 |
|
25 |
-
#
|
26 |
ai_models = {
|
27 |
"Basic AI": BasicAI(),
|
28 |
-
"Advanced AI": AdvancedAI()
|
|
|
|
|
29 |
}
|
30 |
|
31 |
def play_ai_vs_ai(ai1_name, ai2_name, rounds):
|
@@ -36,12 +40,23 @@ def play_ai_vs_ai(ai1_name, ai2_name, rounds):
|
|
36 |
history = []
|
37 |
|
38 |
for i in range(rounds):
|
39 |
-
ai1_move = ai1.move(board)
|
40 |
-
ai2_move = ai2.move(board)
|
41 |
history.append(f"Round {i + 1}: {ai1_name} -> {ai1_move}, {ai2_name} -> {ai2_move}")
|
42 |
|
43 |
return "\n".join(history)
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# Gradio Interface
|
46 |
def build_interface():
|
47 |
with gr.Blocks() as interface:
|
@@ -60,17 +75,26 @@ def build_interface():
|
|
60 |
start_button = gr.Button("Start AI vs AI")
|
61 |
|
62 |
output_box = gr.Textbox(label="Game History", lines=10)
|
|
|
63 |
|
64 |
start_button.click(
|
65 |
play_ai_vs_ai,
|
66 |
inputs=[ai1_dropdown, ai2_dropdown, rounds_slider],
|
67 |
-
outputs=output_box
|
68 |
)
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
gr.Markdown("""
|
71 |
### How It Works
|
72 |
- **Basic AI**: Makes random valid moves.
|
73 |
- **Advanced AI**: Uses a placeholder strategy to prioritize specific moves.
|
|
|
74 |
|
75 |
Select models, adjust the rounds, and press "Start AI vs AI" to see the results!
|
76 |
""")
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
|
4 |
+
# Simulated AI models for checkers (replace these with actual AI model imports later)
|
5 |
+
from transformers import pipeline # Example for using real AI models
|
6 |
+
|
7 |
class BasicAI:
|
8 |
def move(self, board):
|
9 |
# Basic AI makes a random valid move
|
|
|
24 |
# Simulate valid moves as random pairs of coordinates
|
25 |
return [(random.randint(0, 7), random.randint(0, 7)) for _ in range(random.randint(1, 5))]
|
26 |
|
27 |
+
# Replace BasicAI and AdvancedAI with actual models (example Hugging Face pipelines)
|
28 |
ai_models = {
|
29 |
"Basic AI": BasicAI(),
|
30 |
+
"Advanced AI": AdvancedAI(),
|
31 |
+
"HuggingFace AI 1": pipeline("text-generation", model="gpt2"),
|
32 |
+
"HuggingFace AI 2": pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
|
33 |
}
|
34 |
|
35 |
def play_ai_vs_ai(ai1_name, ai2_name, rounds):
|
|
|
40 |
history = []
|
41 |
|
42 |
for i in range(rounds):
|
43 |
+
ai1_move = ai1.move(board) if hasattr(ai1, 'move') else "Simulated AI 1 Move"
|
44 |
+
ai2_move = ai2.move(board) if hasattr(ai2, 'move') else "Simulated AI 2 Move"
|
45 |
history.append(f"Round {i + 1}: {ai1_name} -> {ai1_move}, {ai2_name} -> {ai2_move}")
|
46 |
|
47 |
return "\n".join(history)
|
48 |
|
49 |
+
def render_board():
|
50 |
+
board_html = "<table style='border-collapse: collapse; width: 100px; height: 100px;'>"
|
51 |
+
for i in range(8):
|
52 |
+
board_html += "<tr>"
|
53 |
+
for j in range(8):
|
54 |
+
color = "#f0d9b5" if (i + j) % 2 == 0 else "#b58863"
|
55 |
+
board_html += f"<td style='width: 40px; height: 40px; background-color: {color}; border: 1px solid black;'></td>"
|
56 |
+
board_html += "</tr>"
|
57 |
+
board_html += "</table>"
|
58 |
+
return board_html
|
59 |
+
|
60 |
# Gradio Interface
|
61 |
def build_interface():
|
62 |
with gr.Blocks() as interface:
|
|
|
75 |
start_button = gr.Button("Start AI vs AI")
|
76 |
|
77 |
output_box = gr.Textbox(label="Game History", lines=10)
|
78 |
+
board_output = gr.HTML(render_board(), label="Checkers Board")
|
79 |
|
80 |
start_button.click(
|
81 |
play_ai_vs_ai,
|
82 |
inputs=[ai1_dropdown, ai2_dropdown, rounds_slider],
|
83 |
+
outputs=[output_box]
|
84 |
)
|
85 |
|
86 |
+
gr.HTML("""
|
87 |
+
<div style='margin-top: 20px;'>
|
88 |
+
<h3>Game Board</h3>
|
89 |
+
<div>""" + render_board() + """</div>
|
90 |
+
</div>
|
91 |
+
""")
|
92 |
+
|
93 |
gr.Markdown("""
|
94 |
### How It Works
|
95 |
- **Basic AI**: Makes random valid moves.
|
96 |
- **Advanced AI**: Uses a placeholder strategy to prioritize specific moves.
|
97 |
+
- **HuggingFace AI Models**: These use text generation pipelines for creative move generation.
|
98 |
|
99 |
Select models, adjust the rounds, and press "Start AI vs AI" to see the results!
|
100 |
""")
|