byte-vortex commited on
Commit
102a7a5
·
verified ·
1 Parent(s): 609fc12

Delete ui/spare_gradio_app.py

Browse files
Files changed (1) hide show
  1. ui/spare_gradio_app.py +0 -420
ui/spare_gradio_app.py DELETED
@@ -1,420 +0,0 @@
1
- """Gradio game UI for Myco — LLM-powered forest adventure."""
2
-
3
- from typing import Any, cast
4
- import gradio as gr
5
-
6
- from game.agent_controller import MycoController
7
- from game.engine import (
8
- collect_current, discover_mushroom, eat_current,
9
- follow_whisper, hf_companion_status, myco_reply,
10
- pick_current, study_current,
11
- )
12
- from game.state import welcome_history
13
- from models.mushroom import Mushroom
14
-
15
- from ui.renderers import (
16
- dex_markdown, forest_scene, game_intro_markdown,
17
- game_status_markdown, hero_markdown, home_forest_markdown,
18
- mushroom_card, play_hook_markdown, progress_markdown,
19
- world_map_markdown,
20
- )
21
-
22
- # ---------------------------------------------------------------------------
23
- # Globals
24
- # ---------------------------------------------------------------------------
25
- myco_brain = MycoController()
26
- EMPTY_COLLECTION = None
27
- START_POSITION = (1, 1)
28
-
29
- HOME_MARKDOWN = """
30
- ## Welcome to the Myco forest
31
-
32
- Myco is a tiny AI-powered forest game. Open **🎮 Play** to search for mushrooms,
33
- visit **📖 MycoDex** to review your collection, and use **🧭 Guide** for help.
34
-
35
- **Goal:** solve one mystery — why does Myco remember a forest that vanished before it was born?
36
- Follow clues until the Impossible Mushroom blooms inside the MycoDex.
37
- """
38
-
39
- GUIDE_MARKDOWN = """
40
- ## How to play
41
-
42
- 1. Go to **🎮 Play** and click **🎮 Search Clearing** to discover a mushroom.
43
- 2. **Myco (the AI) reacts** — read what it senses about the mushroom.
44
- 3. Choose your action:
45
- - **🔍 Study** — Myco examines the mushroom and updates its magic field.
46
- - **🍄 Pick** — grab it as a game item. Poisonous picks = Game Over!
47
- - **📖 Collect** — add it to the MycoDex for safe keeping and score.
48
- - **🌌 Follow Whisper** — reveal mystery fragments. Risky on unstudied mushrooms!
49
- - **🍽️ Eat?** — Myco will stop you every time (for good reason).
50
- 4. **Ask Myco** anything in the chat — it knows the forest, the mystery, and the mushrooms.
51
- 5. Use **⬆️⬇️⬅️➡️** to move through the 3×3 forest grid.
52
-
53
- **Scoring:** Common = 10 spores · Rare = 35 · Legendary = 100 · Poison = −25 + Game Over
54
-
55
- **Poisonous mushrooms:** Ghost Gill · Pepper Pixie · Ruby Knuckle · Clockwork Chanterelle
56
- """
57
-
58
-
59
- # ---------------------------------------------------------------------------
60
- # Helpers
61
- # ---------------------------------------------------------------------------
62
- def mushroom_card_from_state(current):
63
- if current is None:
64
- return mushroom_card(None)
65
- return mushroom_card(
66
- Mushroom.from_dict(current),
67
- current.get("clue"),
68
- current.get("secret"),
69
- current,
70
- )
71
-
72
-
73
- def _safe_pos(position):
74
- if position is None:
75
- return START_POSITION
76
- x, y = position
77
- return (max(0, min(2, int(x))), max(0, min(2, int(y))))
78
-
79
-
80
- # ---------------------------------------------------------------------------
81
- # Autonomous tick — routes through run_tick so controller state stays in sync
82
- # ---------------------------------------------------------------------------
83
- def autonomous_tick(current, collection, position):
84
- """
85
- Called every 5 seconds by gr.Timer.
86
- Delegates entirely to myco_brain.run_tick() so self.history and
87
- self.position inside the controller stay up to date.
88
- Returns 7 outputs matching the timer's outputs list.
89
- """
90
- pos = _safe_pos(position)
91
- coll = list(collection or [])
92
- result = myco_brain.run_tick(current, coll)
93
- action = result.get("action_taken")
94
- data = result.get("data", {})
95
-
96
- if action == "search":
97
- # A new mushroom was discovered — update current and scene.
98
- new_current = data.get("current", current)
99
- new_history = data.get("history")
100
- return (
101
- forest_scene(new_current, coll, pos),
102
- game_status_markdown(new_current, coll, pos),
103
- mushroom_card_from_state(new_current),
104
- new_current,
105
- new_history if new_history is not None else gr.update(),
106
- dex_markdown(coll),
107
- progress_markdown(coll),
108
- )
109
-
110
- if action == "collect":
111
- # Collection was updated — refresh dex and progress.
112
- new_coll = data.get("collection", coll)
113
- new_history = data.get("history")
114
- return (
115
- forest_scene(current, new_coll, pos),
116
- game_status_markdown(current, new_coll, pos),
117
- mushroom_card_from_state(current),
118
- current,
119
- new_history if new_history is not None else gr.update(),
120
- dex_markdown(new_coll),
121
- progress_markdown(new_coll),
122
- )
123
-
124
- if action == "study":
125
- new_history = data.get("history")
126
- return (
127
- forest_scene(current, coll, pos),
128
- game_status_markdown(current, coll, pos),
129
- mushroom_card_from_state(current),
130
- current,
131
- new_history if new_history is not None else gr.update(),
132
- dex_markdown(coll),
133
- progress_markdown(coll),
134
- )
135
-
136
- # move / wait — no state changes worth propagating to UI
137
- # Use gr.update() for chat so we never overwrite the player's history.
138
- return (
139
- forest_scene(current, coll, pos),
140
- game_status_markdown(current, coll, pos),
141
- mushroom_card_from_state(current),
142
- current,
143
- gr.update(), # leave chat untouched
144
- dex_markdown(coll),
145
- progress_markdown(coll),
146
- )
147
-
148
-
149
- # ---------------------------------------------------------------------------
150
- # Player-driven actions
151
- # ---------------------------------------------------------------------------
152
- def search_forest(collection, position):
153
- mushroom, current, history = discover_mushroom(collection)
154
- pos = _safe_pos(position)
155
- return (
156
- forest_scene(current, collection, pos),
157
- game_status_markdown(current, collection, pos),
158
- mushroom_card_from_state(current),
159
- current,
160
- history,
161
- dex_markdown(collection),
162
- progress_markdown(collection),
163
- )
164
-
165
-
166
- def ask_companion(message, history, current, collection, position):
167
- return myco_reply(message, history, current, collection, position)
168
-
169
-
170
- def collect_discovery(current, collection, history, position):
171
- updated_coll, updated_hist = collect_current(current, collection, history)
172
- pos = _safe_pos(position)
173
- return (
174
- updated_coll,
175
- forest_scene(current, updated_coll, pos),
176
- game_status_markdown(current, updated_coll, pos),
177
- dex_markdown(updated_coll),
178
- world_map_markdown(updated_coll),
179
- updated_hist,
180
- progress_markdown(updated_coll),
181
- )
182
-
183
-
184
- def pick_discovery(current, collection, history, position):
185
- updated_coll, updated_current, updated_hist = pick_current(current, collection, history)
186
- pos = _safe_pos(position)
187
- return (
188
- updated_coll,
189
- updated_current,
190
- forest_scene(updated_current, updated_coll, pos),
191
- game_status_markdown(updated_current, updated_coll, pos),
192
- mushroom_card_from_state(updated_current),
193
- dex_markdown(updated_coll),
194
- world_map_markdown(updated_coll),
195
- updated_hist,
196
- progress_markdown(updated_coll),
197
- )
198
-
199
-
200
- def follow_story_whisper(current, collection, history, position):
201
- updated_current, updated_hist = follow_whisper(current, collection, history)
202
- pos = _safe_pos(position)
203
- return (
204
- updated_current,
205
- forest_scene(updated_current, collection, pos),
206
- game_status_markdown(updated_current, collection, pos),
207
- mushroom_card_from_state(updated_current),
208
- updated_hist,
209
- )
210
-
211
-
212
- def study_discovery(current, history, collection, position):
213
- updated_current, updated_hist = study_current(current, history)
214
- pos = _safe_pos(position)
215
- return (
216
- updated_current,
217
- forest_scene(updated_current, collection, pos),
218
- game_status_markdown(updated_current, collection, pos),
219
- mushroom_card_from_state(updated_current),
220
- updated_hist,
221
- )
222
-
223
-
224
- def move_player(position, direction, current, collection):
225
- x, y = _safe_pos(position)
226
- if direction == "north": y -= 1
227
- elif direction == "south": y += 1
228
- elif direction == "west": x -= 1
229
- elif direction == "east": x += 1
230
- pos = _safe_pos((x, y))
231
- return pos, forest_scene(current, collection, pos), game_status_markdown(current, collection, pos)
232
-
233
-
234
- def block_unsafe_eating(current, collection, history):
235
- _, updated_hist = eat_current(current, collection, history)
236
- return updated_hist
237
-
238
-
239
- def open_play_tab():
240
- return gr.update(selected="play")
241
-
242
-
243
- # ---------------------------------------------------------------------------
244
- # UI
245
- # ---------------------------------------------------------------------------
246
- def build_app():
247
- with gr.Blocks(title="Myco — AI Forest Game") as demo:
248
- gr.HTML("""
249
- <style>
250
- .gradio-container { background: #1a1a2e !important; }
251
- #forest-scene, #discovery-card { contain: layout style; content-visibility: auto; }
252
- .progress-level, .progress-level-inner, .generating { display: none !important; }
253
- </style>
254
- """)
255
-
256
- current_mushroom = gr.State(None)
257
- collection = gr.State([])
258
- player_position = gr.State(START_POSITION)
259
-
260
- # AI heartbeat — fires every 5 seconds
261
- auto_timer = gr.Timer(5)
262
-
263
- gr.HTML(hero_markdown())
264
-
265
- with gr.Tabs(selected="home") as tabs:
266
- with gr.Tab("🏠 Home", id="home"):
267
- gr.Markdown(HOME_MARKDOWN)
268
- gr.HTML(home_forest_markdown())
269
- start_play_button = gr.Button("🍄 Enter the Forest", variant="primary", size="lg")
270
- gr.Markdown(f"*{hf_companion_status()}*")
271
-
272
- with gr.Tab("🎮 Play", id="play"):
273
- gr.HTML(game_intro_markdown())
274
- scene = gr.HTML(
275
- forest_scene(None, EMPTY_COLLECTION, START_POSITION),
276
- elem_id="forest-scene",
277
- )
278
-
279
- with gr.Row():
280
- with gr.Column(scale=1):
281
- search_button = gr.Button("🎮 Search Clearing", variant="primary", size="lg")
282
- with gr.Column(scale=1):
283
- gr.Markdown("**Move Myco**")
284
- with gr.Row():
285
- gr.HTML("<div></div>")
286
- north_button = gr.Button("⬆️", size="sm")
287
- gr.HTML("<div></div>")
288
- with gr.Row():
289
- west_button = gr.Button("⬅️", size="sm")
290
- pick_button = gr.Button("🍄 Pick", variant="primary", size="sm")
291
- east_button = gr.Button("➡️", size="sm")
292
- with gr.Row():
293
- gr.HTML("<div></div>")
294
- south_button = gr.Button("⬇️", size="sm")
295
- gr.HTML("<div></div>")
296
-
297
- status = gr.Markdown(game_status_markdown(None, EMPTY_COLLECTION, START_POSITION))
298
-
299
- with gr.Row(equal_height=True, elem_id="discovery-card"):
300
- with gr.Column(scale=2):
301
- gr.Markdown("### 🍄 Current Discovery")
302
- discovery = gr.Markdown(mushroom_card(None))
303
- with gr.Row():
304
- study_button = gr.Button("🔍 Study", variant="secondary")
305
- collect_button = gr.Button("📖 Collect", variant="secondary")
306
- with gr.Row():
307
- whisper_button = gr.Button("🌌 Follow Whisper", variant="secondary")
308
- eat_button = gr.Button("🍽️ Eat?", variant="stop")
309
-
310
- with gr.Column(scale=3):
311
- gr.Markdown("### 💬 Myco Chat")
312
- chat = gr.Chatbot(
313
- value=cast(Any, welcome_history()),
314
- label="Myco",
315
- height=400,
316
- show_label=False,
317
- )
318
- with gr.Row():
319
- prompt = gr.Textbox(
320
- placeholder="Ask Myco anything about the forest...",
321
- show_label=False,
322
- scale=4,
323
- lines=1,
324
- )
325
- ask_button = gr.Button("Ask 🍄", variant="primary", scale=1)
326
-
327
- with gr.Tab("📖 MycoDex"):
328
- with gr.Row():
329
- progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION))
330
- dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION))
331
-
332
- with gr.Tab("🗺️ World Map"):
333
- world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
334
-
335
- with gr.Tab("🧭 Guide"):
336
- gr.Markdown(GUIDE_MARKDOWN)
337
-
338
- # ── Event wiring ──────────────────────────────────────────────
339
-
340
- start_play_button.click(open_play_tab, outputs=[tabs])
341
-
342
- # Timer: autonomous AI tick every 5s.
343
- # Uses gr.update() for chat on idle ticks to avoid wiping history.
344
- auto_timer.tick(
345
- fn=autonomous_tick,
346
- inputs=[current_mushroom, collection, player_position],
347
- outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
348
- )
349
-
350
- search_button.click(
351
- search_forest,
352
- inputs=[collection, player_position],
353
- outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
354
- show_progress="hidden",
355
- )
356
-
357
- ask_button.click(
358
- ask_companion,
359
- inputs=[prompt, chat, current_mushroom, collection, player_position],
360
- outputs=[prompt, chat],
361
- show_progress="hidden",
362
- )
363
- prompt.submit(
364
- ask_companion,
365
- inputs=[prompt, chat, current_mushroom, collection, player_position],
366
- outputs=[prompt, chat],
367
- show_progress="hidden",
368
- )
369
-
370
- for direction, button in [
371
- ("north", north_button),
372
- ("south", south_button),
373
- ("west", west_button),
374
- ("east", east_button),
375
- ]:
376
- button.click(
377
- move_player,
378
- inputs=[player_position, gr.State(direction), current_mushroom, collection],
379
- outputs=[player_position, scene, status],
380
- show_progress="hidden",
381
- )
382
-
383
- study_button.click(
384
- study_discovery,
385
- inputs=[current_mushroom, chat, collection, player_position],
386
- outputs=[current_mushroom, scene, status, discovery, chat],
387
- show_progress="hidden",
388
- )
389
-
390
- collect_button.click(
391
- collect_discovery,
392
- inputs=[current_mushroom, collection, chat, player_position],
393
- outputs=[collection, scene, status, dex, world_map, chat, progress],
394
- show_progress="hidden",
395
- )
396
-
397
- pick_button.click(
398
- pick_discovery,
399
- inputs=[current_mushroom, collection, chat, player_position],
400
- outputs=[collection, current_mushroom, scene, status, discovery, dex, world_map, chat, progress],
401
- show_progress="hidden",
402
- )
403
-
404
- whisper_button.click(
405
- follow_story_whisper,
406
- inputs=[current_mushroom, collection, chat, player_position],
407
- outputs=[current_mushroom, scene, status, discovery, chat],
408
- show_progress="hidden",
409
- )
410
-
411
- eat_button.click(
412
- block_unsafe_eating,
413
- inputs=[current_mushroom, collection, chat],
414
- outputs=[chat],
415
- show_progress="hidden",
416
- )
417
-
418
-
419
- demo.queue()
420
- return demo