byte-vortex commited on
Commit
68735b1
·
verified ·
1 Parent(s): eb3d18a

Deploy Myco from CI

Browse files
Files changed (1) hide show
  1. ui/gradio_app.py +11 -256
ui/gradio_app.py CHANGED
@@ -1,5 +1,3 @@
1
- """Gradio app composition for Myco."""
2
-
3
  import gradio as gr
4
 
5
  from game.engine import (
@@ -42,151 +40,8 @@ GUIDE_MARKDOWN = """
42
  5. Check **📖 MycoDex** to track discoveries and area unlocks.
43
  """
44
 
45
- def mushroom_card_from_state(current):
46
- """Render a mushroom card from Gradio state."""
47
- if current is None:
48
- return mushroom_card(None)
49
- return mushroom_card(Mushroom.from_dict(current))
50
-
51
-
52
- def search_forest(collection):
53
- """Explore the forest and refresh every visible gameplay panel."""
54
- mushroom, current, history = discover_mushroom(collection)
55
- return (
56
- forest_scene(current, collection),
57
- game_status_markdown(current, collection),
58
- scene = forest_scene(current, collection)
59
- status = game_status_markdown(current, collection)
60
- card = mushroom_card(mushroom)
61
- dex = dex_markdown(collection)
62
- progress = progress_markdown(collection)
63
- return scene, status, card, current, history, dex, progress
64
- outputs = [
65
- forest_scene(current, collection),
66
- game_status_markdown(current, collection),
67
- return (
68
- forest_scene(current, collection),
69
- game_status_markdown(current, collection),
70
-
71
- import gradio as gr
72
-
73
- from game.engine import collect_current, discover_mushroom, eat_current, myco_reply, study_current
74
- from game.state import CollectionState, MushroomState, welcome_history
75
- from models.mushroom import Mushroom
76
- from ui.renderers import dex_markdown, forest_scene, hero_markdown, mushroom_card, progress_markdown
77
-
78
-
79
- def mushroom_card_from_state(current):
80
- # Keep callback signatures plain for Hugging Face Spaces parser stability.
81
- def begin_forest_search(collection):
82
- def explore(collection: CollectionState) -> tuple[str, str, MushroomState, list[dict[str, str]], str, str]:
83
- """Gradio callback for the Start Exploring button."""
84
- mushroom, current, history = discover_mushroom(collection)
85
- return (
86
- forest_scene(current, collection),
87
- mushroom_card(mushroom),
88
- current,
89
- history,
90
- dex_markdown(collection),
91
- progress_markdown(collection),
92
- ]
93
- return tuple(outputs)
94
- )
95
-
96
-
97
- def ask_companion(message, history, current):
98
- """Ask Myco about the active discovery."""
99
- return myco_reply(message, history, current)
100
-
101
-
102
- def collect_discovery(current, collection, history):
103
- """Collect the active mushroom and refresh collection-dependent panels."""
104
- updated_collection, updated_history = collect_current(current, collection, history)
105
- scene = forest_scene(current, updated_collection)
106
- status = game_status_markdown(current, updated_collection)
107
- dex = dex_markdown(updated_collection)
108
- progress = progress_markdown(updated_collection)
109
- return updated_collection, scene, status, dex, updated_history, progress
110
- outputs = [
111
- def ask_myco(message, history, current):
112
- def ask_myco(
113
- message: str,
114
- history: list[dict[str, str]],
115
- current: MushroomState | None,
116
- ) -> tuple[str, list[dict[str, str]]]:
117
- """Gradio callback for the companion chat panel."""
118
- return myco_reply(message, history, current)
119
-
120
-
121
- def add_to_dex(current, collection, history):
122
- def add_to_dex(
123
- current: MushroomState | None,
124
- collection: CollectionState,
125
- history: list[dict[str, str]],
126
- ) -> tuple[CollectionState, str, str, list[dict[str, str]], str]:
127
- """Gradio callback for collecting the current mushroom."""
128
- updated_collection, updated_history = collect_current(current, collection, history)
129
- return (
130
- updated_collection,
131
- forest_scene(current, updated_collection),
132
- game_status_markdown(current, updated_collection),
133
- dex_markdown(updated_collection),
134
- updated_history,
135
- progress_markdown(updated_collection),
136
- ]
137
- return tuple(outputs)
138
- )
139
-
140
-
141
- def study_discovery(current, history, collection):
142
- """Study the active mushroom and refresh the discovery card."""
143
- updated_current, updated_history = study_current(current, history)
144
- scene = forest_scene(updated_current, collection)
145
- status = game_status_markdown(updated_current, collection)
146
- card = mushroom_card_from_state(updated_current)
147
- return updated_current, scene, status, card, updated_history
148
- outputs = [
149
- def study_mushroom(current, history, collection):
150
- def study_mushroom(
151
- current: MushroomState | None,
152
- history: list[dict[str, str]],
153
- collection: CollectionState,
154
- ) -> tuple[MushroomState | None, str, str, list[dict[str, str]]]:
155
- """Gradio callback for studying the current mushroom."""
156
- updated_current, updated_history = study_current(current, history)
157
- return (
158
- updated_current,
159
- forest_scene(updated_current, collection),
160
- game_status_markdown(updated_current, collection),
161
- mushroom_card_from_state(updated_current),
162
- updated_history,
163
- ]
164
- return tuple(outputs)
165
- )
166
-
167
-
168
- def block_unsafe_eating(current, history):
169
- """Let Myco prevent unsafe eating while preserving chat state."""
170
- return eat_current(current, history)
171
-
172
-
173
- def build_app():
174
- """Build the Gradio interface for Hugging Face Spaces."""
175
- with gr.Blocks(title="Myco") as demo:
176
- current_mushroom = gr.State(None)
177
- collection = gr.State(EMPTY_COLLECTION)
178
-
179
- def eat_mushroom(current, history):
180
- def eat_mushroom(
181
- current: MushroomState | None,
182
- history: list[dict[str, str]],
183
- ) -> list[dict[str, str]]:
184
- """Gradio callback for the Eat choice."""
185
- return eat_current(current, history)
186
-
187
 
188
  def mushroom_card_from_state(current):
189
- def mushroom_card_from_state(current: MushroomState | None) -> str:
190
  """Render a mushroom card from Gradio state."""
191
  if current is None:
192
  return mushroom_card(None)
@@ -235,11 +90,9 @@ def build_app():
235
  def block_unsafe_eating(current, history):
236
  return eat_current(current, history)
237
 
238
- def build_app() -> gr.Blocks:
239
- """Build the Gradio interface for Hugging Face Spaces."""
240
  with gr.Blocks(title="Myco") as demo:
241
  current_mushroom = gr.State(None)
242
- collection = gr.State([])
243
 
244
  gr.HTML(hero_markdown())
245
 
@@ -260,35 +113,6 @@ def build_app() -> gr.Blocks:
260
  elem_id="discovery-card",
261
  )
262
  search_button = gr.Button("Start Exploring", variant="primary")
263
- status = gr.Markdown(game_status_markdown(None, EMPTY_COLLECTION), elem_id="quest-log")
264
- with gr.Row():
265
- with gr.Column(scale=2):
266
- discovery = gr.Markdown(mushroom_card(None), elem_id="discovery-card")
267
- search_button = gr.Button("Start Exploring", variant="primary")
268
- gr.Markdown(
269
- """
270
- ## Welcome to the Myco forest
271
-
272
- Myco is a tiny forest game portal. Open **🎮 Play** to search for mushrooms,
273
- visit **📖 MycoDex** to review progress, and use **🧭 Guide** for the loop.
274
- Myco now plays more like a tiny game portal: open **🎮 Play** to explore,
275
- visit **📖 MycoDex** to review progress, and use **🧭 Guide** when you need a reminder.
276
-
277
- **Goal:** discover strange mushrooms, ask Myco what they might be, study them safely,
278
- and build a magical MycoDex collection.
279
- """
280
- )
281
-
282
- with gr.Tab("🎮 Play"):
283
- scene = gr.HTML(forest_scene(None, []))
284
- status = gr.Markdown(game_status_markdown(None, []), elem_id="quest-log")
285
-
286
- with gr.Row():
287
- with gr.Column(scale=2):
288
- discovery = gr.Markdown(mushroom_card(None), elem_id="discovery-card")
289
- with gr.Row():
290
- search_button = gr.Button("Start Exploring", variant="primary")
291
- explore_button = gr.Button("Start Exploring", variant="primary")
292
  with gr.Row():
293
  eat_button = gr.Button("Eat")
294
  collect_button = gr.Button("Collect")
@@ -301,12 +125,6 @@ def build_app() -> gr.Blocks:
301
  )
302
  with gr.Row():
303
  prompt = gr.Textbox(
304
- chat = gr.Chatbot(value=welcome_history(), label="Myco Chat", height=360)
305
- with gr.Row():
306
- prompt = gr.Textbox(label="Ask Myco", placeholder="Ask Myco about this mushroom", scale=4)
307
- prompt = gr.Textbox(
308
- textbox_component = gr.Textbox
309
- prompt = textbox_component(
310
  label="Ask Myco",
311
  placeholder="Ask Myco about this mushroom",
312
  scale=4,
@@ -323,102 +141,39 @@ def build_app() -> gr.Blocks:
323
  dex_markdown(EMPTY_COLLECTION),
324
  elem_id="mycodex",
325
  )
326
- progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION), elem_id="progress-panel")
327
- dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION), elem_id="mycodex")
328
 
329
  with gr.Tab("🧭 Guide"):
330
  gr.Markdown(GUIDE_MARKDOWN)
331
 
332
  search_button.click(
333
  search_forest,
334
- inputs=(collection,),
335
- outputs=(scene, status, discovery, current_mushroom, chat, dex, progress),
336
  )
337
  ask_button.click(
338
  ask_companion,
339
- inputs=(prompt, chat, current_mushroom),
340
- outputs=(prompt, chat),
341
  )
342
  prompt.submit(
343
  ask_companion,
344
- inputs=(prompt, chat, current_mushroom),
345
- outputs=(prompt, chat),
346
  )
347
- collect_button.click(
348
- collect_discovery,
349
- inputs=(current_mushroom, collection, chat),
350
- outputs=(collection, scene, status, dex, chat, progress),
351
- )
352
- study_button.click(
353
- study_discovery,
354
- inputs=(current_mushroom, chat, collection),
355
- outputs=(current_mushroom, scene, status, discovery, chat),
356
- )
357
- eat_button.click(
358
- block_unsafe_eating,
359
- inputs=(current_mushroom, chat),
360
- outputs=(chat,),
361
- )
362
- search_button.click(search_forest, inputs=(collection,), outputs=(scene, status, discovery, current_mushroom, chat, dex, progress))
363
- ask_button.click(ask_companion, inputs=(prompt, chat, current_mushroom), outputs=(prompt, chat))
364
- prompt.submit(ask_companion, inputs=(prompt, chat, current_mushroom), outputs=(prompt, chat))
365
- collect_button.click(collect_discovery, inputs=(current_mushroom, collection, chat), outputs=(collection, scene, status, dex, chat, progress))
366
- study_button.click(study_discovery, inputs=(current_mushroom, chat, collection), outputs=(current_mushroom, scene, status, discovery, chat))
367
- eat_button.click(block_unsafe_eating, inputs=(current_mushroom, chat), outputs=(chat,))
368
- progress = gr.Markdown(progress_markdown([]), elem_id="progress-panel")
369
- dex = gr.Markdown(dex_markdown([]), elem_id="mycodex")
370
-
371
- with gr.Tab("🧭 Guide"):
372
- gr.Markdown(
373
- """
374
- ## How to play
375
-
376
- 1. Go to **🎮 Play**.
377
- 2. Click **Start Exploring** to reveal a mushroom in the forest scene.
378
- 3. Ask Myco about the mushroom.
379
- 4. Choose **Study** for a safer hint, **Collect** for the MycoDex, or **Eat** to see Myco stop you.
380
- 5. Check **📖 MycoDex** to track discoveries and area unlocks.
381
- """
382
- )
383
-
384
- search_button.click(
385
- search_forest,
386
- inputs=[collection],
387
- outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
388
- outputs=[scene, discovery, current_mushroom, chat, dex, progress],
389
- )
390
- ask_button.click(ask_companion, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
391
- prompt.submit(ask_companion, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
392
  collect_button.click(
393
  collect_discovery,
394
  inputs=[current_mushroom, collection, chat],
395
  outputs=[collection, scene, status, dex, chat, progress],
396
- explore_button.click(
397
- begin_forest_search,
398
- explore,
399
- inputs=[collection],
400
- outputs=[scene, discovery, current_mushroom, chat, dex, progress],
401
- )
402
- ask_button.click(ask_myco, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
403
- prompt.submit(ask_myco, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
404
- collect_button.click(
405
- add_to_dex,
406
- inputs=[current_mushroom, collection, chat],
407
- outputs=[collection, scene, dex, chat, progress],
408
  )
409
  study_button.click(
410
  study_discovery,
411
  inputs=[current_mushroom, chat, collection],
412
  outputs=[current_mushroom, scene, status, discovery, chat],
413
  )
414
- eat_button.click(block_unsafe_eating, inputs=[current_mushroom, chat], outputs=[chat])
415
- outputs=[current_mushroom, scene, discovery, chat],
416
- )
417
- eat_button.click(block_unsafe_eating, inputs=[current_mushroom, chat], outputs=[chat])
418
- study_mushroom,
419
- inputs=[current_mushroom, chat, collection],
420
- outputs=[current_mushroom, scene, discovery, chat],
421
  )
422
- eat_button.click(eat_mushroom, inputs=[current_mushroom, chat], outputs=[chat])
423
 
424
  return demo
 
 
 
1
  import gradio as gr
2
 
3
  from game.engine import (
 
40
  5. Check **📖 MycoDex** to track discoveries and area unlocks.
41
  """
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  def mushroom_card_from_state(current):
 
45
  """Render a mushroom card from Gradio state."""
46
  if current is None:
47
  return mushroom_card(None)
 
90
  def block_unsafe_eating(current, history):
91
  return eat_current(current, history)
92
 
 
 
93
  with gr.Blocks(title="Myco") as demo:
94
  current_mushroom = gr.State(None)
95
+ collection = gr.State(EMPTY_COLLECTION)
96
 
97
  gr.HTML(hero_markdown())
98
 
 
113
  elem_id="discovery-card",
114
  )
115
  search_button = gr.Button("Start Exploring", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  with gr.Row():
117
  eat_button = gr.Button("Eat")
118
  collect_button = gr.Button("Collect")
 
125
  )
126
  with gr.Row():
127
  prompt = gr.Textbox(
 
 
 
 
 
 
128
  label="Ask Myco",
129
  placeholder="Ask Myco about this mushroom",
130
  scale=4,
 
141
  dex_markdown(EMPTY_COLLECTION),
142
  elem_id="mycodex",
143
  )
 
 
144
 
145
  with gr.Tab("🧭 Guide"):
146
  gr.Markdown(GUIDE_MARKDOWN)
147
 
148
  search_button.click(
149
  search_forest,
150
+ inputs=[collection],
151
+ outputs=[scene, status, discovery, current_mushroom, chat, dex, progress],
152
  )
153
  ask_button.click(
154
  ask_companion,
155
+ inputs=[prompt, chat, current_mushroom],
156
+ outputs=[prompt, chat],
157
  )
158
  prompt.submit(
159
  ask_companion,
160
+ inputs=[prompt, chat, current_mushroom],
161
+ outputs=[prompt, chat],
162
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  collect_button.click(
164
  collect_discovery,
165
  inputs=[current_mushroom, collection, chat],
166
  outputs=[collection, scene, status, dex, chat, progress],
 
 
 
 
 
 
 
 
 
 
 
 
167
  )
168
  study_button.click(
169
  study_discovery,
170
  inputs=[current_mushroom, chat, collection],
171
  outputs=[current_mushroom, scene, status, discovery, chat],
172
  )
173
+ eat_button.click(
174
+ block_unsafe_eating,
175
+ inputs=[current_mushroom, chat],
176
+ outputs=[chat],
 
 
 
177
  )
 
178
 
179
  return demo