Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,57 +15,8 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
15 |
)
|
16 |
print("Model loaded successfully!")
|
17 |
|
18 |
-
def analyze_patterns(player_history, opponent_history):
|
19 |
-
"""Perform basic pattern analysis on the game history"""
|
20 |
-
analysis = {}
|
21 |
-
|
22 |
-
# Count frequencies of each move
|
23 |
-
move_counts = collections.Counter(opponent_history)
|
24 |
-
total_moves = len(opponent_history)
|
25 |
-
|
26 |
-
analysis["move_frequencies"] = {
|
27 |
-
"Rock": f"{move_counts.get('Rock', 0)}/{total_moves} ({move_counts.get('Rock', 0)*100/total_moves:.1f}%)",
|
28 |
-
"Paper": f"{move_counts.get('Paper', 0)}/{total_moves} ({move_counts.get('Paper', 0)*100/total_moves:.1f}%)",
|
29 |
-
"Scissors": f"{move_counts.get('Scissors', 0)}/{total_moves} ({move_counts.get('Scissors', 0)*100/total_moves:.1f}%)"
|
30 |
-
}
|
31 |
-
|
32 |
-
# Check response patterns (what opponent plays after player's moves)
|
33 |
-
response_patterns = {
|
34 |
-
"After_Rock": collections.Counter(),
|
35 |
-
"After_Paper": collections.Counter(),
|
36 |
-
"After_Scissors": collections.Counter()
|
37 |
-
}
|
38 |
-
|
39 |
-
for i in range(len(player_history) - 1):
|
40 |
-
player_move = player_history[i]
|
41 |
-
opponent_next = opponent_history[i + 1]
|
42 |
-
response_patterns[f"After_{player_move}"][opponent_next] += 1
|
43 |
-
|
44 |
-
analysis["response_patterns"] = {}
|
45 |
-
for pattern, counter in response_patterns.items():
|
46 |
-
if sum(counter.values()) > 0:
|
47 |
-
most_common = counter.most_common(1)[0]
|
48 |
-
analysis["response_patterns"][pattern] = f"{most_common[0]} ({most_common[1]}/{sum(counter.values())})"
|
49 |
-
|
50 |
-
# Check for repeating sequences
|
51 |
-
last_moves = opponent_history[-3:]
|
52 |
-
repeated_sequences = []
|
53 |
-
|
54 |
-
# Look for this sequence in history
|
55 |
-
for i in range(len(opponent_history) - 3):
|
56 |
-
if opponent_history[i:i+3] == last_moves:
|
57 |
-
if i+3 < len(opponent_history):
|
58 |
-
repeated_sequences.append(opponent_history[i+3])
|
59 |
-
|
60 |
-
if repeated_sequences:
|
61 |
-
counter = collections.Counter(repeated_sequences)
|
62 |
-
most_common = counter.most_common(1)[0]
|
63 |
-
analysis["sequence_prediction"] = f"After sequence {' → '.join(last_moves)}, opponent most often plays {most_common[0]} ({most_common[1]}/{len(repeated_sequences)} times)"
|
64 |
-
|
65 |
-
return analysis
|
66 |
-
|
67 |
def format_rps_game_prompt(game_data):
|
68 |
-
"""Format Rock-Paper-Scissors game data into a prompt for the LLM
|
69 |
try:
|
70 |
# Parse the JSON game data
|
71 |
if isinstance(game_data, str):
|
@@ -79,40 +30,26 @@ def format_rps_game_prompt(game_data):
|
|
79 |
opponent_score = game_data.get("opponent_score", 0)
|
80 |
draws = game_data.get("draws", 0)
|
81 |
|
82 |
-
#
|
83 |
-
|
84 |
-
|
85 |
-
# Format analysis for prompt
|
86 |
-
move_frequencies = pattern_analysis.get("move_frequencies", {})
|
87 |
-
response_patterns = pattern_analysis.get("response_patterns", {})
|
88 |
-
sequence_prediction = pattern_analysis.get("sequence_prediction", "No clear sequence pattern detected")
|
89 |
-
|
90 |
-
# Create a more specific prompt with the analysis
|
91 |
-
prompt = f"""You are an expert Rock-Paper-Scissors strategy advisor helping a player win.
|
92 |
|
93 |
Game State:
|
94 |
- Rounds played: {rounds_played}
|
95 |
- Player score: {player_score}
|
96 |
- Opponent score: {opponent_score}
|
97 |
- Draws: {draws}
|
98 |
-
- Player's
|
99 |
-
- Opponent's
|
100 |
|
101 |
-
|
102 |
-
- Opponent's move frequencies:
|
103 |
-
* Rock: {move_frequencies.get('Rock', 'N/A')}
|
104 |
-
* Paper: {move_frequencies.get('Paper', 'N/A')}
|
105 |
-
* Scissors: {move_frequencies.get('Scissors', 'N/A')}
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
Based on this pattern analysis, what should the player choose next (Rock, Paper, or Scissors)?
|
115 |
-
Explain your reasoning step-by-step, then end with: "Recommendation: [Rock/Paper/Scissors]"
|
116 |
"""
|
117 |
return prompt
|
118 |
except Exception as e:
|
@@ -130,7 +67,7 @@ def generate_advice(game_data):
|
|
130 |
# Set max_length to avoid excessive generation
|
131 |
outputs = model.generate(
|
132 |
inputs["input_ids"],
|
133 |
-
max_new_tokens=
|
134 |
do_sample=True,
|
135 |
temperature=0.7,
|
136 |
top_p=0.9
|
@@ -145,25 +82,19 @@ def generate_advice(game_data):
|
|
145 |
|
146 |
# If model response is too short, add fallback advice
|
147 |
if len(response) < 30:
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
if max_move == "Rock":
|
160 |
-
suggestion = "Paper"
|
161 |
-
elif max_move == "Paper":
|
162 |
-
suggestion = "Scissors"
|
163 |
else:
|
164 |
-
|
165 |
-
|
166 |
-
response += f"\n\nBased on opponent's move frequencies, they play {max_move} most often. \nRecommendation: {suggestion}"
|
167 |
|
168 |
return response
|
169 |
except Exception as e:
|
|
|
15 |
)
|
16 |
print("Model loaded successfully!")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def format_rps_game_prompt(game_data):
|
19 |
+
"""Format Rock-Paper-Scissors game data into a simple prompt for the LLM"""
|
20 |
try:
|
21 |
# Parse the JSON game data
|
22 |
if isinstance(game_data, str):
|
|
|
30 |
opponent_score = game_data.get("opponent_score", 0)
|
31 |
draws = game_data.get("draws", 0)
|
32 |
|
33 |
+
# Create a simple prompt with just the game state
|
34 |
+
prompt = f"""You are a Rock-Paper-Scissors strategy advisor.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
Game State:
|
37 |
- Rounds played: {rounds_played}
|
38 |
- Player score: {player_score}
|
39 |
- Opponent score: {opponent_score}
|
40 |
- Draws: {draws}
|
41 |
+
- Player's moves (oldest to newest): {', '.join(player_history)}
|
42 |
+
- Opponent's moves (oldest to newest): {', '.join(opponent_history)}
|
43 |
|
44 |
+
Analyze the game history and provide advice on what move (Rock, Paper, or Scissors) the player should make next.
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
First, explain your thought process and reasoning in detail:
|
47 |
+
1. Look for patterns in the opponent's moves
|
48 |
+
2. Consider if the opponent seems to be using any strategy
|
49 |
+
3. Evaluate if the player should try to counter the opponent's most likely next move
|
50 |
+
4. Consider any psychological factors that might be relevant
|
51 |
|
52 |
+
After your explanation, end with a clear recommendation: "Recommendation: [Rock/Paper/Scissors]"
|
|
|
|
|
|
|
53 |
"""
|
54 |
return prompt
|
55 |
except Exception as e:
|
|
|
67 |
# Set max_length to avoid excessive generation
|
68 |
outputs = model.generate(
|
69 |
inputs["input_ids"],
|
70 |
+
max_new_tokens=200, # Allow more tokens for detailed reasoning
|
71 |
do_sample=True,
|
72 |
temperature=0.7,
|
73 |
top_p=0.9
|
|
|
82 |
|
83 |
# If model response is too short, add fallback advice
|
84 |
if len(response) < 30:
|
85 |
+
# Simple fallback based on opponent's last move
|
86 |
+
if len(opponent_history) > 0:
|
87 |
+
last_move = opponent_history[-1]
|
88 |
+
if last_move == "Rock":
|
89 |
+
suggestion = "Paper"
|
90 |
+
elif last_move == "Paper":
|
91 |
+
suggestion = "Scissors"
|
92 |
+
else:
|
93 |
+
suggestion = "Rock"
|
94 |
+
|
95 |
+
response += f"\n\nBased on the opponent's last move ({last_move}), a reasonable counter would be:\nRecommendation: {suggestion}"
|
|
|
|
|
|
|
|
|
96 |
else:
|
97 |
+
response += "\n\nWith no game history to analyze, each move has equal probability of success.\nRecommendation: Paper"
|
|
|
|
|
98 |
|
99 |
return response
|
100 |
except Exception as e:
|