Spaces:
Runtime error
Runtime error
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -29,11 +29,6 @@ class SolitaireEnvironment:
|
|
| 29 |
if pile:
|
| 30 |
card = pile[-1]
|
| 31 |
moves.append(f"Move {card} to foundation")
|
| 32 |
-
# Check moves within tableau
|
| 33 |
-
for src_idx, src_pile in enumerate(self.tableau):
|
| 34 |
-
if src_pile:
|
| 35 |
-
card = src_pile[-1]
|
| 36 |
-
# Can we move to another tableau pile?
|
| 37 |
return moves[:5] # Limit to 5 moves for simplicity
|
| 38 |
|
| 39 |
class SolitaireRLTrainer:
|
|
@@ -49,12 +44,6 @@ class SolitaireRLTrainer:
|
|
| 49 |
def train_step(self, state_description: str, action: str, reward: float):
|
| 50 |
# In a real implementation, this would update the model weights
|
| 51 |
return f"Training step completed. Reward: {reward}"
|
| 52 |
-
|
| 53 |
-
def get_reward(self, action: str):
|
| 54 |
-
# Simple reward function for demonstration
|
| 55 |
-
if "foundation" in action:
|
| 56 |
-
return 1.0
|
| 57 |
-
return 0.0
|
| 58 |
|
| 59 |
class MistralSolitaireAgent:
|
| 60 |
def __init__(self):
|
|
@@ -84,10 +73,6 @@ def train_mistral_solitaire(num_episodes: int, learning_rate: float):
|
|
| 84 |
def play_solitaire_game(state_description: str, action: str):
|
| 85 |
"""Execute a move in the Solitaire game"""
|
| 86 |
# Simulate game action
|
| 87 |
-
game_state = {
|
| 88 |
-
"tableau": [[random.randint(1, 13) for _ in range(i+1)] for i in range(7)]
|
| 89 |
-
|
| 90 |
-
# Calculate reward based on action quality
|
| 91 |
if "foundation" in action:
|
| 92 |
reward = 0.8
|
| 93 |
elif "tableau" in action:
|
|
@@ -98,8 +83,7 @@ def play_solitaire_game(state_description: str, action: str):
|
|
| 98 |
return {
|
| 99 |
"action_taken": action,
|
| 100 |
"reward": reward,
|
| 101 |
-
"new_state": f"Game state after {action}"
|
| 102 |
-
"is_valid": True
|
| 103 |
}
|
| 104 |
|
| 105 |
def format_game_state(state: Dict) -> str:
|
|
@@ -132,15 +116,16 @@ def create_solitaire_ui():
|
|
| 132 |
maximum=1000,
|
| 133 |
value=100,
|
| 134 |
step=10
|
| 135 |
-
|
|
|
|
| 136 |
learning_rate = gr.Slider(
|
| 137 |
label="Learning Rate",
|
| 138 |
minimum=0.001,
|
| 139 |
maximum=0.1,
|
| 140 |
value=0.01,
|
| 141 |
step=0.001
|
| 142 |
-
|
| 143 |
-
|
| 144 |
train_btn = gr.Button("Start Training", variant="primary")
|
| 145 |
training_output = gr.JSON(label="Training Progress")
|
| 146 |
|
|
@@ -153,12 +138,11 @@ def create_solitaire_ui():
|
|
| 153 |
|
| 154 |
with gr.Tab("Game Play"):
|
| 155 |
with gr.Row():
|
| 156 |
-
|
| 157 |
label="Current Game State",
|
| 158 |
-
lines=3
|
|
|
|
| 159 |
)
|
| 160 |
-
|
| 161 |
-
with gr.Row():
|
| 162 |
action_input = gr.Textbox(
|
| 163 |
label="Action to Take",
|
| 164 |
placeholder="e.g., Move A♠ to foundation, Draw from deck"
|
|
@@ -169,7 +153,7 @@ def create_solitaire_ui():
|
|
| 169 |
|
| 170 |
play_btn.click(
|
| 171 |
fn=play_solitaire_game,
|
| 172 |
-
inputs=[
|
| 173 |
outputs=[game_result],
|
| 174 |
api_visibility="public"
|
| 175 |
)
|
|
@@ -199,7 +183,7 @@ if __name__ == "__main__":
|
|
| 199 |
demo.launch(
|
| 200 |
theme=gr.themes.Soft(
|
| 201 |
primary_hue="blue",
|
| 202 |
-
secondary_hue="indigo",
|
| 203 |
neutral_hue="slate",
|
| 204 |
font=gr.themes.GoogleFont("Inter"),
|
| 205 |
text_size="lg",
|
|
|
|
| 29 |
if pile:
|
| 30 |
card = pile[-1]
|
| 31 |
moves.append(f"Move {card} to foundation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return moves[:5] # Limit to 5 moves for simplicity
|
| 33 |
|
| 34 |
class SolitaireRLTrainer:
|
|
|
|
| 44 |
def train_step(self, state_description: str, action: str, reward: float):
|
| 45 |
# In a real implementation, this would update the model weights
|
| 46 |
return f"Training step completed. Reward: {reward}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
class MistralSolitaireAgent:
|
| 49 |
def __init__(self):
|
|
|
|
| 73 |
def play_solitaire_game(state_description: str, action: str):
|
| 74 |
"""Execute a move in the Solitaire game"""
|
| 75 |
# Simulate game action
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if "foundation" in action:
|
| 77 |
reward = 0.8
|
| 78 |
elif "tableau" in action:
|
|
|
|
| 83 |
return {
|
| 84 |
"action_taken": action,
|
| 85 |
"reward": reward,
|
| 86 |
+
"new_state": f"Game state after {action}"
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
def format_game_state(state: Dict) -> str:
|
|
|
|
| 116 |
maximum=1000,
|
| 117 |
value=100,
|
| 118 |
step=10
|
| 119 |
+
)
|
| 120 |
+
with gr.Row():
|
| 121 |
learning_rate = gr.Slider(
|
| 122 |
label="Learning Rate",
|
| 123 |
minimum=0.001,
|
| 124 |
maximum=0.1,
|
| 125 |
value=0.01,
|
| 126 |
step=0.001
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
train_btn = gr.Button("Start Training", variant="primary")
|
| 130 |
training_output = gr.JSON(label="Training Progress")
|
| 131 |
|
|
|
|
| 138 |
|
| 139 |
with gr.Tab("Game Play"):
|
| 140 |
with gr.Row():
|
| 141 |
+
game_state_input = gr.Textbox(
|
| 142 |
label="Current Game State",
|
| 143 |
+
lines=3,
|
| 144 |
+
placeholder="Describe current game state..."
|
| 145 |
)
|
|
|
|
|
|
|
| 146 |
action_input = gr.Textbox(
|
| 147 |
label="Action to Take",
|
| 148 |
placeholder="e.g., Move A♠ to foundation, Draw from deck"
|
|
|
|
| 153 |
|
| 154 |
play_btn.click(
|
| 155 |
fn=play_solitaire_game,
|
| 156 |
+
inputs=[game_state_input, action_input],
|
| 157 |
outputs=[game_result],
|
| 158 |
api_visibility="public"
|
| 159 |
)
|
|
|
|
| 183 |
demo.launch(
|
| 184 |
theme=gr.themes.Soft(
|
| 185 |
primary_hue="blue",
|
| 186 |
+
secondary_hue="indigo",
|
| 187 |
neutral_hue="slate",
|
| 188 |
font=gr.themes.GoogleFont("Inter"),
|
| 189 |
text_size="lg",
|