Spaces:
Sleeping

byte-vortex commited on
Commit
dcf7912
ยท
verified ยท
1 Parent(s): 3e3ad64

Deploy Myco from CI

Browse files
Files changed (1) hide show
  1. ui/gradio_app.py +269 -270
ui/gradio_app.py CHANGED
@@ -4,21 +4,21 @@ from typing import Any, cast
4
  import gradio as gr
5
 
6
  from game.engine import (
7
- ย  ย  collect_current, discover_mushroom, eat_current,
8
- ย  ย  follow_whisper, hf_companion_status, myco_reply,
9
- ย  ย  pick_current, study_current,
10
  )
11
  from game.state import welcome_history
12
  from models.mushroom import Mushroom
13
  from ui.renderers import (
14
- ย  ย  dex_markdown, forest_scene, game_intro_markdown,
15
- ย  ย  game_status_markdown, hero_markdown, home_forest_markdown,
16
- ย  ย  mushroom_card, play_hook_markdown, progress_markdown,
17
- ย  ย  world_map_markdown,
18
  )
19
 
20
  EMPTY_COLLECTION = None
21
- START_POSITIONย  ย = (1, 1)
22
 
23
  HOME_MARKDOWN = """
24
  ## Welcome to the Myco forest
@@ -36,11 +36,11 @@ GUIDE_MARKDOWN = """
36
  1. Go to **๐ŸŽฎ Play** and click **๐ŸŽฎ Search Clearing** to discover a mushroom.
37
  2. **Myco (the AI) reacts** โ€” read what it senses about the mushroom.
38
  3. Choose your action:
39
- ย  ย - **๐Ÿ” Study** โ€” Myco examines the mushroom and updates its magic field.
40
- ย  ย - **๐Ÿ„ Pick** โ€” grab it as a game item. Poisonous picks = Game Over!
41
- ย  ย - **๐Ÿ“– Collect** โ€” add it to the MycoDex for safe keeping and score.
42
- ย  ย - **๐ŸŒŒ Follow Whisper** โ€” reveal mystery fragments. Risky on unstudied mushrooms!
43
- ย  ย - **๐Ÿฝ๏ธ Eat?** โ€” Myco will stop you every time (for good reason).
44
  4. **Ask Myco** anything in the chat โ€” it knows the forest, the mystery, and the mushrooms.
45
  5. Use **โฌ†๏ธโฌ‡๏ธโฌ…๏ธโžก๏ธ** to move through the 3ร—3 forest grid.
46
 
@@ -51,293 +51,292 @@ GUIDE_MARKDOWN = """
51
 
52
 
53
  def mushroom_card_from_state(current):
54
- ย  ย  if current is None:
55
- ย  ย  ย  ย  return mushroom_card(None)
56
- ย  ย  return mushroom_card(
57
- ย  ย  ย  ย  Mushroom.from_dict(current),
58
- ย  ย  ย  ย  current.get("clue"),
59
- ย  ย  ย  ย  current.get("secret"),
60
- ย  ย  ย  ย  current,
61
- ย  ย  )
62
 
63
 
64
  def search_forest(collection, position):
65
- ย  ย  mushroom, current, history = discover_mushroom(collection)
66
- ย  ย  pos = _safe_pos(position)
67
- ย  ย  return (
68
- ย  ย  ย  ย  forest_scene(current, collection, pos),
69
- ย  ย  ย  ย  game_status_markdown(current, collection, pos),
70
- ย  ย  ย  ย  mushroom_card_from_state(current),
71
- ย  ย  ย  ย  current,
72
- ย  ย  ย  ย  history,
73
- ย  ย  ย  ย  dex_markdown(collection),
74
- ย  ย  ย  ย  progress_markdown(collection),
75
- ย  ย  )
76
 
77
 
78
  def ask_companion(message, history, current, collection, position):
79
- ย  ย  return myco_reply(message, history, current, collection, position)
80
 
81
 
82
  def collect_discovery(current, collection, history, position):
83
- ย  ย  updated_coll, updated_hist = collect_current(current, collection, history)
84
- ย  ย  pos = _safe_pos(position)
85
- ย  ย  return (
86
- ย  ย  ย  ย  updated_coll,
87
- ย  ย  ย  ย  forest_scene(current, updated_coll, pos),
88
- ย  ย  ย  ย  game_status_markdown(current, updated_coll, pos),
89
- ย  ย  ย  ย  dex_markdown(updated_coll),
90
- ย  ย  ย  ย  world_map_markdown(updated_coll),
91
- ย  ย  ย  ย  updated_hist,
92
- ย  ย  ย  ย  progress_markdown(updated_coll),
93
- ย  ย  )
94
 
95
 
96
  def pick_discovery(current, collection, history, position):
97
- ย  ย  updated_coll, updated_current, updated_hist = pick_current(current, collection, history)
98
- ย  ย  pos = _safe_pos(position)
99
- ย  ย  return (
100
- ย  ย  ย  ย  updated_coll,
101
- ย  ย  ย  ย  updated_current,
102
- ย  ย  ย  ย  forest_scene(updated_current, updated_coll, pos),
103
- ย  ย  ย  ย  game_status_markdown(updated_current, updated_coll, pos),
104
- ย  ย  ย  ย  mushroom_card_from_state(updated_current),
105
- ย  ย  ย  ย  dex_markdown(updated_coll),
106
- ย  ย  ย  ย  world_map_markdown(updated_coll),
107
- ย  ย  ย  ย  updated_hist,
108
- ย  ย  ย  ย  progress_markdown(updated_coll),
109
- ย  ย  )
110
 
111
 
112
  def follow_story_whisper(current, collection, history, position):
113
- ย  ย  updated_current, updated_hist = follow_whisper(current, collection, history)
114
- ย  ย  pos = _safe_pos(position)
115
- ย  ย  return (
116
- ย  ย  ย  ย  updated_current,
117
- ย  ย  ย  ย  forest_scene(updated_current, collection, pos),
118
- ย  ย  ย  ย  game_status_markdown(updated_current, collection, pos),
119
- ย  ย  ย  ย  mushroom_card_from_state(updated_current),
120
- ย  ย  ย  ย  updated_hist,
121
- ย  ย  )
122
 
123
 
124
  def study_discovery(current, history, collection, position):
125
- ย  ย  updated_current, updated_hist = study_current(current, history)
126
- ย  ย  pos = _safe_pos(position)
127
- ย  ย  return (
128
- ย  ย  ย  ย  updated_current,
129
- ย  ย  ย  ย  forest_scene(updated_current, collection, pos),
130
- ย  ย  ย  ย  game_status_markdown(updated_current, collection, pos),
131
- ย  ย  ย  ย  mushroom_card_from_state(updated_current),
132
- ย  ย  ย  ย  updated_hist,
133
- ย  ย  )
134
 
135
 
136
  def move_player(position, direction, current, collection):
137
- ย  ย  x, y = _safe_pos(position)
138
- ย  ย  if direction == "north": y -= 1
139
- ย  ย  elif direction == "south": y += 1
140
- ย  ย  elif direction == "west":ย  x -= 1
141
- ย  ย  elif direction == "east":ย  x += 1
142
- ย  ย  pos = _safe_pos((x, y))
143
- ย  ย  return pos, forest_scene(current, collection, pos), game_status_markdown(current, collection, pos)
144
 
145
 
146
  def block_unsafe_eating(current, history):
147
- ย  ย  return eat_current(current, history)
148
 
149
 
150
  def open_play_tab():
151
- ย  ย  return gr.update(selected="play")
152
 
153
 
154
  def _safe_pos(position):
155
- ย  ย  if position is None:
156
- ย  ย  ย  ย  return START_POSITION
157
- ย  ย  x, y = position
158
- ย  ย  return (max(0, min(2, int(x))), max(0, min(2, int(y))))
159
 
160
 
161
  def build_app():
162
- ย  ย  with gr.Blocks(title="Myco โ€” AI Forest Game", theme=gr.themes.Soft()) as demo:
163
- ย  ย  ย  ย  gr.HTML("""
164
- ย  ย  ย  ย  <style>
165
- ย  ย  ย  ย  .gradio-container { background: #1a1a2e !important; }
166
- ย  ย  ย  ย  .gr-html, iframe, .html-container {ย 
167
- ย  ย  ย  ย  ย  ย  transition: none !important;ย 
168
- ย  ย  ย  ย  ย  ย  animation: none !important;
169
- ย  ย  ย  ย  }
170
- ย  ย  ย  ย  /* Prevent white flash on component updates */
171
- ย  ย  ย  ย  * {ย 
172
- ย  ย  ย  ย  ย  ย  transition: opacity 0.15s ease !important;
173
- ย  ย  ย  ย  }
174
- ย  ย  ย  ย  .pending, .generating {
175
- ย  ย  ย  ย  ย  ย  opacity: 0.85 !important;
176
- ย  ย  ย  ย  ย  ย  background: transparent !important;
177
- ย  ย  ย  ย  }
178
- ย  ย  ย  ย  /* Hide the white overlay Gradio adds during updates */
179
- ย  ย  ย  ย  .progress-level, .progress-level-inner {
180
- ย  ย  ย  ย  ย  ย  display: none !important;
181
- ย  ย  ย  ย  }
182
- ย  ย  ย  ย  </style>
183
- ย  ย  ย  ย  """)
184
-
185
- ย  ย  ย  ย  # โ”€โ”€ State โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
186
- ย  ย  ย  ย  current_mushroom = gr.State(None)
187
- ย  ย  ย  ย  collectionย  ย  ย  ย = gr.State([])
188
- ย  ย  ย  ย  player_positionย  = gr.State(START_POSITION)
189
-
190
- ย  ย  ย  ย  # โ”€โ”€ Hero โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
191
- ย  ย  ย  ย  gr.HTML(hero_markdown())
192
-
193
- ย  ย  ย  ย  with gr.Tabs(selected="home") as tabs:
194
-
195
- ย  ย  ย  ย  ย  ย  # โ”€โ”€ HOME โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
196
- ย  ย  ย  ย  ย  ย  with gr.Tab("๐Ÿ  Home", id="home"):
197
- ย  ย  ย  ย  ย  ย  ย  ย  gr.Markdown(HOME_MARKDOWN)
198
- ย  ย  ย  ย  ย  ย  ย  ย  gr.HTML(home_forest_markdown())
199
- ย  ย  ย  ย  ย  ย  ย  ย  start_play_button = gr.Button(
200
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  "๐Ÿ„ Enter the Forest", variant="primary", size="lg"
201
- ย  ย  ย  ย  ย  ย  ย  ย  )
202
- ย  ย  ย  ย  ย  ย  ย  ย  gr.Markdown(f"*{hf_companion_status()}*")
203
-
204
- ย  ย  ย  ย  ย  ย  # โ”€โ”€ PLAY โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
205
- ย  ย  ย  ย  ย  ย  with gr.Tab("๐ŸŽฎ Play", id="play"):
206
- ย  ย  ย  ย  ย  ย  ย  ย  gr.HTML(game_intro_markdown())
207
-
208
- ย  ย  ย  ย  ย  ย  ย  ย  # Forest scene + controls
209
- ย  ย  ย  ย  ย  ย  ย  ย  scene = gr.HTML(
210
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  forest_scene(None, EMPTY_COLLECTION, START_POSITION),
211
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  elem_id="forest-scene",
212
- ย  ย  ย  ย  ย  ย  ย  ย  )
213
-
214
- ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
215
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Column(scale=1):
216
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  search_button = gr.Button("๐ŸŽฎ Search Clearing", variant="primary", size="lg")
217
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Column(scale=1):
218
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  gr.Markdown("**Move Myco**")
219
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
220
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  gr.HTML("<div></div>")
221
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  north_button = gr.Button("โฌ†๏ธ", size="sm")
222
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  gr.HTML("<div></div>")
223
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
224
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  west_buttonย  = gr.Button("โฌ…๏ธ", size="sm")
225
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  pick_buttonย  = gr.Button("๐Ÿ„ Pick", variant="primary", size="sm")
226
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  east_buttonย  = gr.Button("โžก๏ธ", size="sm")
227
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
228
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  gr.HTML("<div></div>")
229
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  south_button = gr.Button("โฌ‡๏ธ", size="sm")
230
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  gr.HTML("<div></div>")
231
-
232
- ย  ย  ย  ย  ย  ย  ย  ย  # Quest log
233
- ย  ย  ย  ย  ย  ย  ย  ย  status = gr.Markdown(
234
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  game_status_markdown(None, EMPTY_COLLECTION, START_POSITION)
235
- ย  ย  ย  ย  ย  ย  ย  ย  )
236
-
237
- ย  ย  ย  ย  ย  ย  ย  ย  # Discovery card + action buttons + chat
238
- ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row(equal_height=True):
239
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Column(scale=2):
240
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  gr.Markdown("### ๐Ÿ„ Current Discovery")
241
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  discovery = gr.Markdown(mushroom_card(None))
242
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
243
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  study_buttonย  ย = gr.Button("๐Ÿ” Study",ย  ย  ย  ย  ย variant="secondary")
244
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  collect_button = gr.Button("๐Ÿ“– Collect",ย  ย  ย  ย variant="secondary")
245
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
246
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  whisper_button = gr.Button("๐ŸŒŒ Follow Whisper", variant="secondary")
247
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  eat_buttonย  ย  ย = gr.Button("๐Ÿฝ๏ธ Eat?",ย  ย  ย  ย  ย  variant="stop")
248
-
249
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Column(scale=3):
250
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  gr.Markdown("### ๐Ÿ’ฌ Myco Chat")
251
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  chat = gr.Chatbot(
252
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  value=cast(Any, welcome_history()),
253
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  label="Myco",
254
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  height=400,
255
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  show_label=False,
256
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  )
257
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
258
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  prompt = gr.Textbox(
259
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  placeholder="Ask Myco anything about the forest...",
260
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  show_label=False,
261
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  scale=4,
262
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  lines=1,
263
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  )
264
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ask_button = gr.Button("Ask ๐Ÿ„", variant="primary", scale=1)
265
-
266
- ย  ย  ย  ย  ย  ย  # โ”€โ”€ MYCODEX โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
267
- ย  ย  ย  ย  ย  ย  with gr.Tab("๐Ÿ“– MycoDex"):
268
- ย  ย  ย  ย  ย  ย  ย  ย  with gr.Row():
269
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION))
270
- ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  dexย  ย  ย  = gr.Markdown(dex_markdown(EMPTY_COLLECTION))
271
-
272
- ย  ย  ย  ย  ย  ย  # โ”€โ”€ WORLD MAP โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
273
- ย  ย  ย  ย  ย  ย  with gr.Tab("๐Ÿ—บ๏ธ World Map"):
274
- ย  ย  ย  ย  ย  ย  ย  ย  world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
275
-
276
- ย  ย  ย  ย  ย  ย  # โ”€โ”€ GUIDE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
277
- ย  ย  ย  ย  ย  ย  with gr.Tab("๐Ÿงญ Guide"):
278
- ย  ย  ย  ย  ย  ย  ย  ย  gr.Markdown(GUIDE_MARKDOWN)
279
-
280
- ย  ย  ย  ย  # โ”€โ”€ Event wiring โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
281
- ย  ย  ย  ย  start_play_button.click(open_play_tab, outputs=[tabs])
282
-
283
- ย  ย  ย  ย  search_button.click(
284
- ย  ย  ย  ย  ย  ย  search_forest,
285
- ย  ย  ย  ย  ย  ย  inputs=[collection, player_position],
286
- ย  ย  ย  ย  ย  ย  outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
287
- ย  ย  ย  ย  ย  ย  show_progress="hidden",
288
- ย  ย  ย  ย  )
289
-
290
- ย  ย  ย  ย  ask_button.click(
291
- ย  ย  ย  ย  ย  ย  ask_companion,
292
- ย  ย  ย  ย  ย  ย  inputs=[prompt, chat, current_mushroom, collection, player_position],
293
- ย  ย  ย  ย  ย  ย  outputs=[prompt, chat],
294
- ย  ย  ย  ย  ย  ย  show_progress="hidden",
295
- ย  ย  ย  ย  )
296
- ย  ย  ย  ย  prompt.submit(
297
- ย  ย  ย  ย  ย  ย  ask_companion,
298
- ย  ย  ย  ย  ย  ย  inputs=[prompt, chat, current_mushroom, collection, player_position],
299
- ย  ย  ย  ย  ย  ย  outputs=[prompt, chat],
300
- ย  ย  ย  ย  ย  ย  show_progress="hidden",
301
- ย  ย  ย  ย  )
302
-
303
- ย  ย  ย  ย  for direction, button in [
304
- ย  ย  ย  ย  ย  ย  ("north", north_button), ("south", south_button),
305
- ย  ย  ย  ย  ย  ย  ("west",ย  west_button),ย  ("east",ย  east_button),
306
- ย  ย  ย  ย  ]:
307
- ย  ย  ย  ย  ย  ย  button.click(
308
- ย  ย  ย  ย  ย  ย  ย  ย  move_player,
309
- ย  ย  ย  ย  ย  ย  ย  ย  inputs=[player_position, gr.State(direction), current_mushroom, collection],
310
- ย  ย  ย  ย  ย  ย  ย  ย  outputs=[player_position, scene, status],
311
- ย  ย  ย  ย  ย  ย  ย  ย  show_progress="hidden",
312
- ย  ย  ย  ย  ย  ย  )
313
-
314
- ย  ย  ย  ย  study_button.click(
315
- ย  ย  ย  ย  ย  ย  study_discovery,
316
- ย  ย  ย  ย  ย  ย  inputs=[current_mushroom, chat, collection, player_position],
317
- ย  ย  ย  ย  ย  ย  outputs=[current_mushroom, scene, status, discovery, chat],
318
- ย  ย  ย  ย  )
319
- ย  ย  ย  ย  collect_button.click(
320
- ย  ย  ย  ย  ย  ย  collect_discovery,
321
- ย  ย  ย  ย  ย  ย  inputs=[current_mushroom, collection, chat, player_position],
322
- ย  ย  ย  ย  ย  ย  outputs=[collection, scene, status, dex, world_map, chat, progress],
323
- ย  ย  ย  ย  )
324
- ย  ย  ย  ย  pick_button.click(
325
- ย  ย  ย  ย  ย  ย  pick_discovery,
326
- ย  ย  ย  ย  ย  ย  inputs=[current_mushroom, collection, chat, player_position],
327
- ย  ย  ย  ย  ย  ย  outputs=[collection, current_mushroom, scene, status, discovery, dex, world_map, chat, progress],
328
- ย  ย  ย  ย  ย  ย  show_progress="hidden",
329
- ย  ย  ย  ย  )
330
- ย  ย  ย  ย  whisper_button.click(
331
- ย  ย  ย  ย  ย  ย  follow_story_whisper,
332
- ย  ย  ย  ย  ย  ย  inputs=[current_mushroom, collection, chat, player_position],
333
- ย  ย  ย  ย  ย  ย  outputs=[current_mushroom, scene, status, discovery, chat],
334
- ย  ย  ย  ย  )
335
- ย  ย  ย  ย  eat_button.click(
336
- ย  ย  ย  ย  ย  ย  block_unsafe_eating,
337
- ย  ย  ย  ย  ย  ย  inputs=[current_mushroom, chat],
338
- ย  ย  ย  ย  ย  ย  outputs=[chat],
339
- ย  ย  ย  ย  )
340
-
341
- ย  ย  ย  ย  demo.queue()
342
- ย  ย  return demo
343
- ย  ย ย 
 
4
  import gradio as gr
5
 
6
  from game.engine import (
7
+ collect_current, discover_mushroom, eat_current,
8
+ follow_whisper, hf_companion_status, myco_reply,
9
+ pick_current, study_current,
10
  )
11
  from game.state import welcome_history
12
  from models.mushroom import Mushroom
13
  from ui.renderers import (
14
+ dex_markdown, forest_scene, game_intro_markdown,
15
+ game_status_markdown, hero_markdown, home_forest_markdown,
16
+ mushroom_card, play_hook_markdown, progress_markdown,
17
+ world_map_markdown,
18
  )
19
 
20
  EMPTY_COLLECTION = None
21
+ START_POSITION = (1, 1)
22
 
23
  HOME_MARKDOWN = """
24
  ## Welcome to the Myco forest
 
36
  1. Go to **๐ŸŽฎ Play** and click **๐ŸŽฎ Search Clearing** to discover a mushroom.
37
  2. **Myco (the AI) reacts** โ€” read what it senses about the mushroom.
38
  3. Choose your action:
39
+ - **๐Ÿ” Study** โ€” Myco examines the mushroom and updates its magic field.
40
+ - **๐Ÿ„ Pick** โ€” grab it as a game item. Poisonous picks = Game Over!
41
+ - **๐Ÿ“– Collect** โ€” add it to the MycoDex for safe keeping and score.
42
+ - **๐ŸŒŒ Follow Whisper** โ€” reveal mystery fragments. Risky on unstudied mushrooms!
43
+ - **๐Ÿฝ๏ธ Eat?** โ€” Myco will stop you every time (for good reason).
44
  4. **Ask Myco** anything in the chat โ€” it knows the forest, the mystery, and the mushrooms.
45
  5. Use **โฌ†๏ธโฌ‡๏ธโฌ…๏ธโžก๏ธ** to move through the 3ร—3 forest grid.
46
 
 
51
 
52
 
53
  def mushroom_card_from_state(current):
54
+ if current is None:
55
+ return mushroom_card(None)
56
+ return mushroom_card(
57
+ Mushroom.from_dict(current),
58
+ current.get("clue"),
59
+ current.get("secret"),
60
+ current,
61
+ )
62
 
63
 
64
  def search_forest(collection, position):
65
+ mushroom, current, history = discover_mushroom(collection)
66
+ pos = _safe_pos(position)
67
+ return (
68
+ forest_scene(current, collection, pos),
69
+ game_status_markdown(current, collection, pos),
70
+ mushroom_card_from_state(current),
71
+ current,
72
+ history,
73
+ dex_markdown(collection),
74
+ progress_markdown(collection),
75
+ )
76
 
77
 
78
  def ask_companion(message, history, current, collection, position):
79
+ return myco_reply(message, history, current, collection, position)
80
 
81
 
82
  def collect_discovery(current, collection, history, position):
83
+ updated_coll, updated_hist = collect_current(current, collection, history)
84
+ pos = _safe_pos(position)
85
+ return (
86
+ updated_coll,
87
+ forest_scene(current, updated_coll, pos),
88
+ game_status_markdown(current, updated_coll, pos),
89
+ dex_markdown(updated_coll),
90
+ world_map_markdown(updated_coll),
91
+ updated_hist,
92
+ progress_markdown(updated_coll),
93
+ )
94
 
95
 
96
  def pick_discovery(current, collection, history, position):
97
+ updated_coll, updated_current, updated_hist = pick_current(current, collection, history)
98
+ pos = _safe_pos(position)
99
+ return (
100
+ updated_coll,
101
+ updated_current,
102
+ forest_scene(updated_current, updated_coll, pos),
103
+ game_status_markdown(updated_current, updated_coll, pos),
104
+ mushroom_card_from_state(updated_current),
105
+ dex_markdown(updated_coll),
106
+ world_map_markdown(updated_coll),
107
+ updated_hist,
108
+ progress_markdown(updated_coll),
109
+ )
110
 
111
 
112
  def follow_story_whisper(current, collection, history, position):
113
+ updated_current, updated_hist = follow_whisper(current, collection, history)
114
+ pos = _safe_pos(position)
115
+ return (
116
+ updated_current,
117
+ forest_scene(updated_current, collection, pos),
118
+ game_status_markdown(updated_current, collection, pos),
119
+ mushroom_card_from_state(updated_current),
120
+ updated_hist,
121
+ )
122
 
123
 
124
  def study_discovery(current, history, collection, position):
125
+ updated_current, updated_hist = study_current(current, history)
126
+ pos = _safe_pos(position)
127
+ return (
128
+ updated_current,
129
+ forest_scene(updated_current, collection, pos),
130
+ game_status_markdown(updated_current, collection, pos),
131
+ mushroom_card_from_state(updated_current),
132
+ updated_hist,
133
+ )
134
 
135
 
136
  def move_player(position, direction, current, collection):
137
+ x, y = _safe_pos(position)
138
+ if direction == "north": y -= 1
139
+ elif direction == "south": y += 1
140
+ elif direction == "west": x -= 1
141
+ elif direction == "east": x += 1
142
+ pos = _safe_pos((x, y))
143
+ return pos, forest_scene(current, collection, pos), game_status_markdown(current, collection, pos)
144
 
145
 
146
  def block_unsafe_eating(current, history):
147
+ return eat_current(current, history)
148
 
149
 
150
  def open_play_tab():
151
+ return gr.update(selected="play")
152
 
153
 
154
  def _safe_pos(position):
155
+ if position is None:
156
+ return START_POSITION
157
+ x, y = position
158
+ return (max(0, min(2, int(x))), max(0, min(2, int(y))))
159
 
160
 
161
  def build_app():
162
+ with gr.Blocks(title="Myco โ€” AI Forest Game", theme=gr.themes.Soft()) as demo:
163
+ gr.HTML("""
164
+ <style>
165
+ .gradio-container { background: #1a1a2e !important; }
166
+ .gr-html, iframe, .html-container {
167
+ transition: none !important;
168
+ animation: none !important;
169
+ }
170
+ /* Prevent white flash on component updates */
171
+ * {
172
+ transition: opacity 0.15s ease !important;
173
+ }
174
+ .pending, .generating {
175
+ opacity: 0.85 !important;
176
+ background: transparent !important;
177
+ }
178
+ /* Hide the white overlay Gradio adds during updates */
179
+ .progress-level, .progress-level-inner {
180
+ display: none !important;
181
+ }
182
+ </style>
183
+ """)
184
+
185
+ # โ”€โ”€ State โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
186
+ current_mushroom = gr.State(None)
187
+ collection = gr.State([])
188
+ player_position = gr.State(START_POSITION)
189
+
190
+ # โ”€โ”€ Hero โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
191
+ gr.HTML(hero_markdown())
192
+
193
+ with gr.Tabs(selected="home") as tabs:
194
+
195
+ # โ”€โ”€ HOME โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
196
+ with gr.Tab("๐Ÿ  Home", id="home"):
197
+ gr.Markdown(HOME_MARKDOWN)
198
+ gr.HTML(home_forest_markdown())
199
+ start_play_button = gr.Button(
200
+ "๐Ÿ„ Enter the Forest", variant="primary", size="lg"
201
+ )
202
+ gr.Markdown(f"*{hf_companion_status()}*")
203
+
204
+ # โ”€โ”€ PLAY โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
205
+ with gr.Tab("๐ŸŽฎ Play", id="play"):
206
+ gr.HTML(game_intro_markdown())
207
+
208
+ # Forest scene + controls
209
+ scene = gr.HTML(
210
+ forest_scene(None, EMPTY_COLLECTION, START_POSITION),
211
+ elem_id="forest-scene",
212
+ )
213
+
214
+ with gr.Row():
215
+ with gr.Column(scale=1):
216
+ search_button = gr.Button("๐ŸŽฎ Search Clearing", variant="primary", size="lg")
217
+ with gr.Column(scale=1):
218
+ gr.Markdown("**Move Myco**")
219
+ with gr.Row():
220
+ gr.HTML("<div></div>")
221
+ north_button = gr.Button("โฌ†๏ธ", size="sm")
222
+ gr.HTML("<div></div>")
223
+ with gr.Row():
224
+ west_button = gr.Button("โฌ…๏ธ", size="sm")
225
+ pick_button = gr.Button("๐Ÿ„ Pick", variant="primary", size="sm")
226
+ east_button = gr.Button("โžก๏ธ", size="sm")
227
+ with gr.Row():
228
+ gr.HTML("<div></div>")
229
+ south_button = gr.Button("โฌ‡๏ธ", size="sm")
230
+ gr.HTML("<div></div>")
231
+
232
+ # Quest log
233
+ status = gr.Markdown(
234
+ game_status_markdown(None, EMPTY_COLLECTION, START_POSITION)
235
+ )
236
+
237
+ # Discovery card + action buttons + chat
238
+ with gr.Row(equal_height=True):
239
+ with gr.Column(scale=2):
240
+ gr.Markdown("### ๐Ÿ„ Current Discovery")
241
+ discovery = gr.Markdown(mushroom_card(None))
242
+ with gr.Row():
243
+ study_button = gr.Button("๐Ÿ” Study", variant="secondary")
244
+ collect_button = gr.Button("๐Ÿ“– Collect", variant="secondary")
245
+ with gr.Row():
246
+ whisper_button = gr.Button("๐ŸŒŒ Follow Whisper", variant="secondary")
247
+ eat_button = gr.Button("๐Ÿฝ๏ธ Eat?", variant="stop")
248
+
249
+ with gr.Column(scale=3):
250
+ gr.Markdown("### ๐Ÿ’ฌ Myco Chat")
251
+ chat = gr.Chatbot(
252
+ value=cast(Any, welcome_history()),
253
+ label="Myco",
254
+ height=400,
255
+ show_label=False,
256
+ )
257
+ with gr.Row():
258
+ prompt = gr.Textbox(
259
+ placeholder="Ask Myco anything about the forest...",
260
+ show_label=False,
261
+ scale=4,
262
+ lines=1,
263
+ )
264
+ ask_button = gr.Button("Ask ๐Ÿ„", variant="primary", scale=1)
265
+
266
+ # โ”€โ”€ MYCODEX โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
267
+ with gr.Tab("๐Ÿ“– MycoDex"):
268
+ with gr.Row():
269
+ progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION))
270
+ dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION))
271
+
272
+ # โ”€โ”€ WORLD MAP โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
273
+ with gr.Tab("๐Ÿ—บ๏ธ World Map"):
274
+ world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
275
+
276
+ # โ”€โ”€ GUIDE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
277
+ with gr.Tab("๐Ÿงญ Guide"):
278
+ gr.Markdown(GUIDE_MARKDOWN)
279
+
280
+ # โ”€โ”€ Event wiring โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
281
+ start_play_button.click(open_play_tab, outputs=[tabs])
282
+
283
+ search_button.click(
284
+ search_forest,
285
+ inputs=[collection, player_position],
286
+ outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
287
+ show_progress="hidden",
288
+ )
289
+
290
+ ask_button.click(
291
+ ask_companion,
292
+ inputs=[prompt, chat, current_mushroom, collection, player_position],
293
+ outputs=[prompt, chat],
294
+ show_progress="hidden",
295
+ )
296
+ prompt.submit(
297
+ ask_companion,
298
+ inputs=[prompt, chat, current_mushroom, collection, player_position],
299
+ outputs=[prompt, chat],
300
+ show_progress="hidden",
301
+ )
302
+
303
+ for direction, button in [
304
+ ("north", north_button), ("south", south_button),
305
+ ("west", west_button), ("east", east_button),
306
+ ]:
307
+ button.click(
308
+ move_player,
309
+ inputs=[player_position, gr.State(direction), current_mushroom, collection],
310
+ outputs=[player_position, scene, status],
311
+ show_progress="hidden",
312
+ )
313
+
314
+ study_button.click(
315
+ study_discovery,
316
+ inputs=[current_mushroom, chat, collection, player_position],
317
+ outputs=[current_mushroom, scene, status, discovery, chat],
318
+ )
319
+ collect_button.click(
320
+ collect_discovery,
321
+ inputs=[current_mushroom, collection, chat, player_position],
322
+ outputs=[collection, scene, status, dex, world_map, chat, progress],
323
+ )
324
+ pick_button.click(
325
+ pick_discovery,
326
+ inputs=[current_mushroom, collection, chat, player_position],
327
+ outputs=[collection, current_mushroom, scene, status, discovery, dex, world_map, chat, progress],
328
+ show_progress="hidden",
329
+ )
330
+ whisper_button.click(
331
+ follow_story_whisper,
332
+ inputs=[current_mushroom, collection, chat, player_position],
333
+ outputs=[current_mushroom, scene, status, discovery, chat],
334
+ )
335
+ eat_button.click(
336
+ block_unsafe_eating,
337
+ inputs=[current_mushroom, chat],
338
+ outputs=[chat],
339
+ )
340
+
341
+ demo.queue()
342
+ return demo