Spaces:
Sleeping
Sleeping
Commit
Β·
8a0bc9d
1
Parent(s):
821a5f5
refactor(ui): improve layout and code formatting
Browse files- Simplify layout by placing belief distribution and game log in right column
- Improve code formatting with consistent string quotes and line breaks
- Better visual hierarchy with status, round info, chart, and log in vertical flow
- Maintain existing functionality while improving user experience
- ui/gradio_interface.py +35 -23
ui/gradio_interface.py
CHANGED
|
@@ -29,7 +29,9 @@ class GradioInterface:
|
|
| 29 |
self.game = BayesianGame(dice_sides=dice_sides, max_rounds=max_rounds)
|
| 30 |
return self._get_interface_state()
|
| 31 |
|
| 32 |
-
def start_new_game(
|
|
|
|
|
|
|
| 33 |
"""Start a new game.
|
| 34 |
|
| 35 |
Args:
|
|
@@ -70,7 +72,7 @@ class GradioInterface:
|
|
| 70 |
belief_chart,
|
| 71 |
game_log,
|
| 72 |
)
|
| 73 |
-
|
| 74 |
if self.game.game_state.phase != GamePhase.PLAYING:
|
| 75 |
return (
|
| 76 |
"β Game not in playing phase. Start a new game first.",
|
|
@@ -107,7 +109,9 @@ class GradioInterface:
|
|
| 107 |
if state.phase == GamePhase.FINISHED:
|
| 108 |
# Show comprehensive final results
|
| 109 |
summary = self.game.get_game_summary()
|
| 110 |
-
final_correct =
|
|
|
|
|
|
|
| 111 |
round_info = f"""
|
| 112 |
**π Final Game Results:**
|
| 113 |
- True Target: {state.target_value}
|
|
@@ -149,8 +153,8 @@ class GradioInterface:
|
|
| 149 |
Matplotlib figure showing belief distribution
|
| 150 |
"""
|
| 151 |
# Close any existing figures to prevent memory leaks
|
| 152 |
-
plt.close(
|
| 153 |
-
|
| 154 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 155 |
|
| 156 |
if self.game.game_state.current_beliefs:
|
|
@@ -175,14 +179,16 @@ class GradioInterface:
|
|
| 175 |
|
| 176 |
ax.set_xlabel("Target Value")
|
| 177 |
ax.set_ylabel("Belief Probability")
|
| 178 |
-
|
| 179 |
# Enhanced title based on game state
|
| 180 |
if self.game.game_state.phase == GamePhase.FINISHED:
|
| 181 |
-
correct_indicator =
|
|
|
|
|
|
|
| 182 |
ax.set_title(f"Final Belief Distribution {correct_indicator}")
|
| 183 |
else:
|
| 184 |
ax.set_title("Player 2's Belief Distribution")
|
| 185 |
-
|
| 186 |
ax.set_xticks(targets)
|
| 187 |
ax.set_ylim(0, 1)
|
| 188 |
ax.grid(True, alpha=0.3)
|
|
@@ -221,13 +227,13 @@ class GradioInterface:
|
|
| 221 |
|
| 222 |
def _create_empty_chart(self) -> plt.Figure:
|
| 223 |
"""Create an empty chart for error states.
|
| 224 |
-
|
| 225 |
Returns:
|
| 226 |
Matplotlib figure with error message
|
| 227 |
"""
|
| 228 |
# Close any existing figures to prevent memory leaks
|
| 229 |
-
plt.close(
|
| 230 |
-
|
| 231 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 232 |
ax.text(
|
| 233 |
0.5,
|
|
@@ -237,7 +243,7 @@ class GradioInterface:
|
|
| 237 |
ha="center",
|
| 238 |
va="center",
|
| 239 |
fontsize=14,
|
| 240 |
-
color="red"
|
| 241 |
)
|
| 242 |
ax.set_xlim(0, 1)
|
| 243 |
ax.set_ylim(0, 1)
|
|
@@ -268,20 +274,30 @@ class GradioInterface:
|
|
| 268 |
if self.game.game_state.phase == GamePhase.FINISHED:
|
| 269 |
log_lines.append("")
|
| 270 |
log_lines.append("**π Game Completed!**")
|
| 271 |
-
|
| 272 |
if self.game.was_final_guess_correct():
|
| 273 |
-
log_lines.append(
|
|
|
|
|
|
|
| 274 |
else:
|
| 275 |
-
log_lines.append(
|
| 276 |
-
|
|
|
|
|
|
|
| 277 |
# Add some Bayesian insights
|
| 278 |
final_accuracy = self.game.get_final_guess_accuracy()
|
| 279 |
if final_accuracy > 0.5:
|
| 280 |
-
log_lines.append(
|
|
|
|
|
|
|
| 281 |
elif final_accuracy > 0.3:
|
| 282 |
-
log_lines.append(
|
|
|
|
|
|
|
| 283 |
else:
|
| 284 |
-
log_lines.append(
|
|
|
|
|
|
|
| 285 |
|
| 286 |
return "\n".join(log_lines)
|
| 287 |
|
|
@@ -331,11 +347,7 @@ def create_interface() -> gr.Interface:
|
|
| 331 |
with gr.Column(scale=2):
|
| 332 |
status_output = gr.Textbox(label="Game Status", interactive=False)
|
| 333 |
round_info = gr.Markdown("Start a new game to begin.")
|
| 334 |
-
|
| 335 |
-
with gr.Row():
|
| 336 |
-
with gr.Column():
|
| 337 |
belief_plot = gr.Plot(label="Belief Distribution")
|
| 338 |
-
with gr.Column():
|
| 339 |
game_log = gr.Markdown("Game log will appear here.")
|
| 340 |
|
| 341 |
# Event handlers
|
|
|
|
| 29 |
self.game = BayesianGame(dice_sides=dice_sides, max_rounds=max_rounds)
|
| 30 |
return self._get_interface_state()
|
| 31 |
|
| 32 |
+
def start_new_game(
|
| 33 |
+
self, target_value: str = ""
|
| 34 |
+
) -> Tuple[str, str, plt.Figure, str]:
|
| 35 |
"""Start a new game.
|
| 36 |
|
| 37 |
Args:
|
|
|
|
| 72 |
belief_chart,
|
| 73 |
game_log,
|
| 74 |
)
|
| 75 |
+
|
| 76 |
if self.game.game_state.phase != GamePhase.PLAYING:
|
| 77 |
return (
|
| 78 |
"β Game not in playing phase. Start a new game first.",
|
|
|
|
| 109 |
if state.phase == GamePhase.FINISHED:
|
| 110 |
# Show comprehensive final results
|
| 111 |
summary = self.game.get_game_summary()
|
| 112 |
+
final_correct = (
|
| 113 |
+
"β
Correct!" if summary["guess_correct"] else "β Incorrect"
|
| 114 |
+
)
|
| 115 |
round_info = f"""
|
| 116 |
**π Final Game Results:**
|
| 117 |
- True Target: {state.target_value}
|
|
|
|
| 153 |
Matplotlib figure showing belief distribution
|
| 154 |
"""
|
| 155 |
# Close any existing figures to prevent memory leaks
|
| 156 |
+
plt.close("all")
|
| 157 |
+
|
| 158 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 159 |
|
| 160 |
if self.game.game_state.current_beliefs:
|
|
|
|
| 179 |
|
| 180 |
ax.set_xlabel("Target Value")
|
| 181 |
ax.set_ylabel("Belief Probability")
|
| 182 |
+
|
| 183 |
# Enhanced title based on game state
|
| 184 |
if self.game.game_state.phase == GamePhase.FINISHED:
|
| 185 |
+
correct_indicator = (
|
| 186 |
+
"β
" if self.game.was_final_guess_correct() else "β"
|
| 187 |
+
)
|
| 188 |
ax.set_title(f"Final Belief Distribution {correct_indicator}")
|
| 189 |
else:
|
| 190 |
ax.set_title("Player 2's Belief Distribution")
|
| 191 |
+
|
| 192 |
ax.set_xticks(targets)
|
| 193 |
ax.set_ylim(0, 1)
|
| 194 |
ax.grid(True, alpha=0.3)
|
|
|
|
| 227 |
|
| 228 |
def _create_empty_chart(self) -> plt.Figure:
|
| 229 |
"""Create an empty chart for error states.
|
| 230 |
+
|
| 231 |
Returns:
|
| 232 |
Matplotlib figure with error message
|
| 233 |
"""
|
| 234 |
# Close any existing figures to prevent memory leaks
|
| 235 |
+
plt.close("all")
|
| 236 |
+
|
| 237 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 238 |
ax.text(
|
| 239 |
0.5,
|
|
|
|
| 243 |
ha="center",
|
| 244 |
va="center",
|
| 245 |
fontsize=14,
|
| 246 |
+
color="red",
|
| 247 |
)
|
| 248 |
ax.set_xlim(0, 1)
|
| 249 |
ax.set_ylim(0, 1)
|
|
|
|
| 274 |
if self.game.game_state.phase == GamePhase.FINISHED:
|
| 275 |
log_lines.append("")
|
| 276 |
log_lines.append("**π Game Completed!**")
|
| 277 |
+
|
| 278 |
if self.game.was_final_guess_correct():
|
| 279 |
+
log_lines.append(
|
| 280 |
+
"π **Congratulations!** Player 2 correctly identified the target!"
|
| 281 |
+
)
|
| 282 |
else:
|
| 283 |
+
log_lines.append(
|
| 284 |
+
"π **Learning opportunity!** Player 2's beliefs converged but missed the target."
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
# Add some Bayesian insights
|
| 288 |
final_accuracy = self.game.get_final_guess_accuracy()
|
| 289 |
if final_accuracy > 0.5:
|
| 290 |
+
log_lines.append(
|
| 291 |
+
f"π― Strong evidence: {final_accuracy:.1%} confidence in true target"
|
| 292 |
+
)
|
| 293 |
elif final_accuracy > 0.3:
|
| 294 |
+
log_lines.append(
|
| 295 |
+
f"π€ Moderate evidence: {final_accuracy:.1%} confidence in true target"
|
| 296 |
+
)
|
| 297 |
else:
|
| 298 |
+
log_lines.append(
|
| 299 |
+
f"π«οΈ Conflicting evidence: Only {final_accuracy:.1%} confidence in true target"
|
| 300 |
+
)
|
| 301 |
|
| 302 |
return "\n".join(log_lines)
|
| 303 |
|
|
|
|
| 347 |
with gr.Column(scale=2):
|
| 348 |
status_output = gr.Textbox(label="Game Status", interactive=False)
|
| 349 |
round_info = gr.Markdown("Start a new game to begin.")
|
|
|
|
|
|
|
|
|
|
| 350 |
belief_plot = gr.Plot(label="Belief Distribution")
|
|
|
|
| 351 |
game_log = gr.Markdown("Game log will appear here.")
|
| 352 |
|
| 353 |
# Event handlers
|