Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -57,6 +57,7 @@ def generate_response(messages, max_length=512, temperature=0.7, top_p=0.9):
|
|
57 |
def process_input(
|
58 |
analysis_mode, # Mode selector
|
59 |
player_stats,
|
|
|
60 |
system_prompt, # Single system prompt from UI
|
61 |
max_length,
|
62 |
temperature,
|
@@ -70,13 +71,17 @@ def process_input(
|
|
70 |
if system_prompt and system_prompt.strip():
|
71 |
messages.append({"role": "system", "content": system_prompt})
|
72 |
|
73 |
-
# Add
|
74 |
if analysis_mode == "Frequency Only":
|
75 |
user_content = f"Player Move Frequency Stats (Long-Term):\n{player_stats}"
|
76 |
messages.append({"role": "user", "content": user_content})
|
77 |
-
|
78 |
-
|
79 |
-
messages.append({"role": "user", "content":
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# --- Time Measurement Start ---
|
82 |
start_time = time.time()
|
@@ -94,10 +99,10 @@ def process_input(
|
|
94 |
duration = round(end_time - start_time, 2)
|
95 |
|
96 |
# For display purposes - show what was actually sent to the model
|
97 |
-
if
|
98 |
display_prompt = f"Selected Mode: {analysis_mode}\nSystem Prompt:\n{system_prompt}\n\n------\n\nUser Content:\n{user_content}"
|
99 |
else:
|
100 |
-
display_prompt = f"Selected Mode: {analysis_mode}\nSystem Prompt:\n{system_prompt}
|
101 |
|
102 |
print(f"Processing finished in {duration} seconds.")
|
103 |
# Return all results including time and tokens
|
@@ -127,7 +132,7 @@ Step 4: State Final Recommendation.
|
|
127 |
Base your analysis strictly on the provided frequencies and the stated RPS rules.
|
128 |
"""
|
129 |
|
130 |
-
# Updated Markov system prompt
|
131 |
DEFAULT_SYSTEM_PROMPT_MARKOV = """You are analyzing a Rock-Paper-Scissors (RPS) game using a Markov transition matrix.
|
132 |
|
133 |
### TRANSITION MATRIX:
|
@@ -169,18 +174,51 @@ Predicted Next Move: [Move with highest probability]
|
|
169 |
Optimal Counter: [Move that beats the predicted move]
|
170 |
"""
|
171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
# --- Default Input Values ---
|
173 |
DEFAULT_PLAYER_STATS = "Rock: 40%\nPaper: 30%\nScissors: 30%"
|
|
|
174 |
|
175 |
# --- Gradio Interface ---
|
176 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
177 |
gr.Markdown(f"# {MODEL_ID} - RPS Strategy Tester")
|
178 |
-
gr.Markdown("Test model advice using
|
179 |
|
180 |
-
# Mode Selector
|
181 |
analysis_mode_selector = gr.Radio(
|
182 |
label="Select Analysis Mode",
|
183 |
-
choices=["Frequency Only", "Markov Prediction Only"],
|
184 |
value="Frequency Only" # Default mode
|
185 |
)
|
186 |
|
@@ -203,6 +241,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
203 |
gr.Markdown("### Markov Prediction Analysis Inputs")
|
204 |
gr.Markdown("*Use the System Prompt field to directly input your Markov analysis instructions.*")
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
# General Inputs / Parameters / Outputs
|
207 |
with gr.Row():
|
208 |
with gr.Column():
|
@@ -243,18 +289,28 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
243 |
return {
|
244 |
frequency_inputs: gr.update(visible=True),
|
245 |
markov_inputs: gr.update(visible=False),
|
|
|
246 |
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_FREQ) # Load Frequency prompt
|
247 |
}
|
248 |
elif mode == "Markov Prediction Only":
|
249 |
return {
|
250 |
frequency_inputs: gr.update(visible=False),
|
251 |
markov_inputs: gr.update(visible=True),
|
|
|
252 |
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_MARKOV) # Load Markov prompt
|
253 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
else: # Default case
|
255 |
return {
|
256 |
frequency_inputs: gr.update(visible=True),
|
257 |
markov_inputs: gr.update(visible=False),
|
|
|
258 |
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_FREQ)
|
259 |
}
|
260 |
|
@@ -262,7 +318,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
262 |
analysis_mode_selector.change(
|
263 |
fn=update_ui_visibility_and_prompt, # Use the combined update function
|
264 |
inputs=analysis_mode_selector,
|
265 |
-
outputs=[frequency_inputs, markov_inputs, system_prompt_input] # Components to update
|
266 |
)
|
267 |
|
268 |
# Handle button click - Pass the single visible system prompt
|
@@ -271,6 +327,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
271 |
inputs=[
|
272 |
analysis_mode_selector,
|
273 |
player_stats_input,
|
|
|
274 |
system_prompt_input, # Pass the visible system prompt textbox
|
275 |
max_length_slider,
|
276 |
temperature_slider,
|
@@ -280,7 +337,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
280 |
final_prompt_display, response_display,
|
281 |
time_output, tokens_output
|
282 |
],
|
283 |
-
api_name="
|
284 |
)
|
285 |
|
286 |
# --- Launch the demo ---
|
|
|
57 |
def process_input(
|
58 |
analysis_mode, # Mode selector
|
59 |
player_stats,
|
60 |
+
player_behavior_input,
|
61 |
system_prompt, # Single system prompt from UI
|
62 |
max_length,
|
63 |
temperature,
|
|
|
71 |
if system_prompt and system_prompt.strip():
|
72 |
messages.append({"role": "system", "content": system_prompt})
|
73 |
|
74 |
+
# Add content based on analysis mode (no empty content for any mode)
|
75 |
if analysis_mode == "Frequency Only":
|
76 |
user_content = f"Player Move Frequency Stats (Long-Term):\n{player_stats}"
|
77 |
messages.append({"role": "user", "content": user_content})
|
78 |
+
elif analysis_mode == "Behavior Analysis":
|
79 |
+
user_content = player_behavior_input
|
80 |
+
messages.append({"role": "user", "content": user_content})
|
81 |
+
else: # For Markov Prediction only mode
|
82 |
+
# Don't add any user message - let system prompt handle everything
|
83 |
+
user_content = ""
|
84 |
+
# Note: We're not appending an empty user message here
|
85 |
|
86 |
# --- Time Measurement Start ---
|
87 |
start_time = time.time()
|
|
|
99 |
duration = round(end_time - start_time, 2)
|
100 |
|
101 |
# For display purposes - show what was actually sent to the model
|
102 |
+
if user_content:
|
103 |
display_prompt = f"Selected Mode: {analysis_mode}\nSystem Prompt:\n{system_prompt}\n\n------\n\nUser Content:\n{user_content}"
|
104 |
else:
|
105 |
+
display_prompt = f"Selected Mode: {analysis_mode}\nSystem Prompt:\n{system_prompt}"
|
106 |
|
107 |
print(f"Processing finished in {duration} seconds.")
|
108 |
# Return all results including time and tokens
|
|
|
132 |
Base your analysis strictly on the provided frequencies and the stated RPS rules.
|
133 |
"""
|
134 |
|
135 |
+
# Updated Markov system prompt - no changes to content
|
136 |
DEFAULT_SYSTEM_PROMPT_MARKOV = """You are analyzing a Rock-Paper-Scissors (RPS) game using a Markov transition matrix.
|
137 |
|
138 |
### TRANSITION MATRIX:
|
|
|
174 |
Optimal Counter: [Move that beats the predicted move]
|
175 |
"""
|
176 |
|
177 |
+
# New Behavior Analysis prompt
|
178 |
+
DEFAULT_SYSTEM_PROMPT_BEHAVIOR = """You are an RPS assistant analyzing player behavior after wins, losses, and ties. Predict the player's next move and give counter strategy based on the Behavioral probabilities.
|
179 |
+
|
180 |
+
**Behavioral Probabilities P(Change/not change | Win/Loss/Tie):**
|
181 |
+
* P(not change | Win) = 0.70
|
182 |
+
* P(Change | Win) = 0.30
|
183 |
+
* P(not change | Loss) = 0.25
|
184 |
+
* P(Change | Loss) = 0.75
|
185 |
+
* P(not change | Tie) = 0.50
|
186 |
+
* P(Change | Tie) = 0.50
|
187 |
+
|
188 |
+
**Input Provided by User:**
|
189 |
+
* Player's Last Outcome: [Win/Loss/Tie]
|
190 |
+
* Player's Last Move: [Rock/Paper/Scissors]
|
191 |
+
|
192 |
+
**Your Task:**
|
193 |
+
1. Based on the Player's Last Outcome, determine the **Predicted Behavior** by comparing P(not change | Win/Loss/Tie) and P(Change | Win/Loss/Tie).
|
194 |
+
2. Determine the **Player's Predicted Next Move**:
|
195 |
+
* If Predicted Behavior is "not change", predict the same move as Player's Last Move.
|
196 |
+
* If Predicted Behavior is "Change", predict a move different from Player's Last Move (randomly select between the two remaining options with equal probability).
|
197 |
+
3. Recommend the **AI Counter Move** that beats the predicted player move:
|
198 |
+
* Paper beats Rock
|
199 |
+
* Rock beats Scissors
|
200 |
+
* Scissors beats Paper
|
201 |
+
|
202 |
+
**Output Format:**
|
203 |
+
Predicted Behavior: [not change/Change] (Based on P(not change|Outcome)=[Prob], P(Change|Outcome)=[Prob])
|
204 |
+
Prediction Logic: [Brief explanation of your reasoning]
|
205 |
+
Predicted Player Move: [Rock/Paper/Scissors]
|
206 |
+
Recommended AI Counter: [Rock/Paper/Scissors]
|
207 |
+
"""
|
208 |
+
|
209 |
# --- Default Input Values ---
|
210 |
DEFAULT_PLAYER_STATS = "Rock: 40%\nPaper: 30%\nScissors: 30%"
|
211 |
+
DEFAULT_PLAYER_BEHAVIOR = "Player's Last Outcome: Win\nPlayer's Last Move: Rock"
|
212 |
|
213 |
# --- Gradio Interface ---
|
214 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
215 |
gr.Markdown(f"# {MODEL_ID} - RPS Strategy Tester")
|
216 |
+
gr.Markdown("Test model advice using Frequency Stats, Markov Predictions, or Win/Loss/Tie Behavior Analysis.")
|
217 |
|
218 |
+
# Mode Selector - now with three options
|
219 |
analysis_mode_selector = gr.Radio(
|
220 |
label="Select Analysis Mode",
|
221 |
+
choices=["Frequency Only", "Markov Prediction Only", "Behavior Analysis"],
|
222 |
value="Frequency Only" # Default mode
|
223 |
)
|
224 |
|
|
|
241 |
gr.Markdown("### Markov Prediction Analysis Inputs")
|
242 |
gr.Markdown("*Use the System Prompt field to directly input your Markov analysis instructions.*")
|
243 |
|
244 |
+
# New behavior analysis inputs
|
245 |
+
with gr.Group(visible=False) as behavior_inputs:
|
246 |
+
gr.Markdown("### Win/Loss/Tie Behavior Analysis Inputs")
|
247 |
+
player_behavior_input = gr.Textbox(
|
248 |
+
label="Player's Last Outcome and Move", value=DEFAULT_PLAYER_BEHAVIOR, lines=4,
|
249 |
+
info="Enter the last outcome (Win/Loss/Tie) and move (Rock/Paper/Scissors)."
|
250 |
+
)
|
251 |
+
|
252 |
# General Inputs / Parameters / Outputs
|
253 |
with gr.Row():
|
254 |
with gr.Column():
|
|
|
289 |
return {
|
290 |
frequency_inputs: gr.update(visible=True),
|
291 |
markov_inputs: gr.update(visible=False),
|
292 |
+
behavior_inputs: gr.update(visible=False),
|
293 |
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_FREQ) # Load Frequency prompt
|
294 |
}
|
295 |
elif mode == "Markov Prediction Only":
|
296 |
return {
|
297 |
frequency_inputs: gr.update(visible=False),
|
298 |
markov_inputs: gr.update(visible=True),
|
299 |
+
behavior_inputs: gr.update(visible=False),
|
300 |
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_MARKOV) # Load Markov prompt
|
301 |
}
|
302 |
+
elif mode == "Behavior Analysis":
|
303 |
+
return {
|
304 |
+
frequency_inputs: gr.update(visible=False),
|
305 |
+
markov_inputs: gr.update(visible=False),
|
306 |
+
behavior_inputs: gr.update(visible=True),
|
307 |
+
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_BEHAVIOR) # Load Behavior prompt
|
308 |
+
}
|
309 |
else: # Default case
|
310 |
return {
|
311 |
frequency_inputs: gr.update(visible=True),
|
312 |
markov_inputs: gr.update(visible=False),
|
313 |
+
behavior_inputs: gr.update(visible=False),
|
314 |
system_prompt_input: gr.update(value=DEFAULT_SYSTEM_PROMPT_FREQ)
|
315 |
}
|
316 |
|
|
|
318 |
analysis_mode_selector.change(
|
319 |
fn=update_ui_visibility_and_prompt, # Use the combined update function
|
320 |
inputs=analysis_mode_selector,
|
321 |
+
outputs=[frequency_inputs, markov_inputs, behavior_inputs, system_prompt_input] # Components to update
|
322 |
)
|
323 |
|
324 |
# Handle button click - Pass the single visible system prompt
|
|
|
327 |
inputs=[
|
328 |
analysis_mode_selector,
|
329 |
player_stats_input,
|
330 |
+
player_behavior_input,
|
331 |
system_prompt_input, # Pass the visible system prompt textbox
|
332 |
max_length_slider,
|
333 |
temperature_slider,
|
|
|
337 |
final_prompt_display, response_display,
|
338 |
time_output, tokens_output
|
339 |
],
|
340 |
+
api_name="generate_rps_selectable_analysis_v4" # Updated api_name
|
341 |
)
|
342 |
|
343 |
# --- Launch the demo ---
|