Spaces:
Sleeping

byte-vortex commited on
Commit
06ef573
ยท
verified ยท
1 Parent(s): 508c221

Deploy Myco from CI

Browse files
Files changed (1) hide show
  1. ui/gradio_app.py +198 -92
ui/gradio_app.py CHANGED
@@ -1,149 +1,255 @@
1
- import gradio as gr
 
2
  from typing import Any, cast
 
3
 
4
- # Engine imports
5
  from game.engine import (
6
  collect_current, discover_mushroom, eat_current,
7
- follow_whisper, myco_reply, pick_current, study_current,
 
8
  )
9
  from game.state import welcome_history
10
  from models.mushroom import Mushroom
11
-
12
- # Renderer imports (Ensure this file doesn't import ui.renderers!)
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, progress_markdown, world_map_markdown,
 
17
  )
18
 
19
- # Constants
20
  EMPTY_COLLECTION = None
21
- START_POSITION = (1, 1)
22
 
23
- # โ”€โ”€ Controller Functions โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
24
- def _safe_pos(position):
25
- if position is None: return START_POSITION
26
- x, y = position
27
- return (max(0, min(2, int(x))), max(0, min(2, int(y))))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def mushroom_card_from_state(current):
30
- if current is None: return mushroom_card(None)
31
- return mushroom_card(Mushroom.from_dict(current), current.get("clue"), current.get("secret"), current)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def open_play_tab():
34
- return gr.Tabs(selected="play")
 
 
 
 
 
 
35
 
36
- # โ”€โ”€ App Builder โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
37
  def build_app():
38
- with gr.Blocks(title="Myco โ€” AI Forest Game") as demo:
39
- # โ”€โ”€ Arcade / Game Feel JS & CSS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
40
  gr.HTML("""
41
  <style>
42
- @keyframes pop {
43
- 0% { transform: scale(0.8); opacity: 0; }
44
- 80% { transform: scale(1.1); }
45
- 100% { transform: scale(1); opacity: 1; }
46
- }
47
- .mushroom-spawn { animation: pop 0.5s ease-out; }
48
  .gradio-container { background: #1a1a2e !important; }
 
 
49
  </style>
50
- <script>
51
- document.addEventListener('keydown', function(event) {
52
- if (['INPUT', 'TEXTAREA'].includes(event.target.nodeName)) return;
53
- const keyMap = {
54
- 'w':'btn-n', 'ArrowUp':'btn-n',
55
- 'a':'btn-w', 'ArrowLeft':'btn-w',
56
- 's':'btn-s', 'ArrowDown':'btn-s',
57
- 'd':'btn-e', 'ArrowRight':'btn-e',
58
- 'e':'btn-pick'
59
- };
60
- const btn = document.getElementById(keyMap[event.key]);
61
- if (btn) btn.click();
62
- });
63
- </script>
64
  """)
65
 
66
- # โ”€โ”€ State โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
67
  current_mushroom = gr.State(None)
68
- collection = gr.State([])
69
- player_position = gr.State(START_POSITION)
70
 
71
- # โ”€โ”€ Layout โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
72
  gr.HTML(hero_markdown())
73
 
74
  with gr.Tabs(selected="home") as tabs:
75
  with gr.Tab("๐Ÿ  Home", id="home"):
 
76
  gr.HTML(home_forest_markdown())
77
  start_play_button = gr.Button("๐Ÿ„ Enter the Forest", variant="primary", size="lg")
78
-
 
79
  with gr.Tab("๐ŸŽฎ Play", id="play"):
80
  gr.HTML(game_intro_markdown())
81
-
82
- # Scene
83
- scene = gr.HTML(forest_scene(None, EMPTY_COLLECTION, START_POSITION),
84
- elem_id="forest-scene", elem_classes="mushroom-spawn")
85
 
86
  with gr.Row():
87
  with gr.Column(scale=1):
88
- gr.Markdown("### ๐Ÿงญ Move")
89
- north_button = gr.Button("โฌ†๏ธ", size="sm", elem_id="btn-n")
 
90
  with gr.Row():
91
- west_button = gr.Button("โฌ…๏ธ", size="sm", elem_id="btn-w")
92
- pick_button = gr.Button("๐Ÿ„ Pick (E)", variant="primary", size="sm", elem_id="btn-pick")
93
- east_button = gr.Button("โžก๏ธ", size="sm", elem_id="btn-e")
94
- south_button = gr.Button("โฌ‡๏ธ", size="sm", elem_id="btn-s")
95
- search_button = gr.Button("๐ŸŽฎ Search Clearing", variant="secondary")
 
 
 
 
 
 
96
 
 
 
 
97
  with gr.Column(scale=2):
98
- status = gr.Markdown(game_status_markdown(None, EMPTY_COLLECTION, START_POSITION))
99
  discovery = gr.Markdown(mushroom_card(None))
100
-
101
  with gr.Row():
102
- study_button = gr.Button("๐Ÿ” Study")
103
- collect_button = gr.Button("๐Ÿ“– Collect")
104
- whisper_button = gr.Button("๐ŸŒŒ Whisper")
105
- eat_button = gr.Button("๐Ÿฝ๏ธ Eat?", variant="stop")
 
106
 
107
- with gr.Column(scale=2):
108
- chat = gr.Chatbot(value=cast(Any, welcome_history()), label="Myco", height=400)
 
109
  with gr.Row():
110
- prompt = gr.Textbox(placeholder="Ask Myco...", show_label=False)
111
- ask_button = gr.Button("Ask ๐Ÿ„")
112
 
113
  with gr.Tab("๐Ÿ“– MycoDex"):
114
- progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION))
115
- dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION))
116
-
117
- with gr.Tab("๐Ÿ—บ๏ธ Map"):
118
  world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
 
 
119
 
120
- # โ”€โ”€ Event Wiring โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
121
- # All click events now correctly reference defined variables
122
  start_play_button.click(open_play_tab, outputs=[tabs])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- # Movement logic
125
- def move_logic(pos, direction, curr, coll):
126
- x, y = _safe_pos(pos)
127
- if direction == "north": y -= 1
128
- elif direction == "south": y += 1
129
- elif direction == "west": x -= 1
130
- elif direction == "east": x += 1
131
- new_pos = _safe_pos((x, y))
132
- return new_pos, forest_scene(curr, coll, new_pos), game_status_markdown(curr, coll, new_pos)
133
-
134
- for dir, btn in [("north", north_button), ("south", south_button), ("west", west_button), ("east", east_button)]:
135
- btn.click(move_logic, inputs=[player_position, gr.State(dir), current_mushroom, collection],
136
- outputs=[player_position, scene, status])
137
-
138
- # Search / Actions
139
- search_button.click(discover_mushroom, inputs=[collection],
140
- outputs=[current_mushroom, scene, status, discovery, chat, dex, progress])
141
 
142
- ask_button.click(myco_reply, inputs=[prompt, chat, current_mushroom, collection, player_position],
143
- outputs=[prompt, chat])
144
 
145
- pick_button.click(pick_current, inputs=[current_mushroom, collection, chat],
146
- outputs=[collection, current_mushroom, scene, status, discovery, dex, world_map, chat, progress])
147
 
 
148
  return demo
149
 
 
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.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
25
+
26
+ Myco is a tiny AI-powered forest game. Open **๐ŸŽฎ Play** to search for mushrooms,
27
+ visit **๐Ÿ“– MycoDex** to review your collection, and use **๐Ÿงญ Guide** for help.
28
+
29
+ **Goal:** solve one mystery โ€” why does Myco remember a forest that vanished before it was born?
30
+ Follow clues until the Impossible Mushroom blooms inside the MycoDex.
31
+ """
32
+
33
+ GUIDE_MARKDOWN = """
34
+ ## How to play
35
+
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
+
47
+ **Scoring:** Common = 10 spores ยท Rare = 35 ยท Legendary = 100 ยท Poison = โˆ’25 + Game Over
48
+
49
+ **Poisonous mushrooms:** Ghost Gill ยท Pepper Pixie ยท Ruby Knuckle ยท Clockwork Chanterelle
50
+ """
51
 
52
  def mushroom_card_from_state(current):
53
+ if current is None:
54
+ return mushroom_card(None)
55
+ return mushroom_card(
56
+ Mushroom.from_dict(current),
57
+ current.get("clue"),
58
+ current.get("secret"),
59
+ current,
60
+ )
61
+
62
+ def search_forest(collection, position):
63
+ mushroom, current, history = discover_mushroom(collection)
64
+ pos = _safe_pos(position)
65
+ return (
66
+ forest_scene(current, collection, pos),
67
+ game_status_markdown(current, collection, pos),
68
+ mushroom_card_from_state(current),
69
+ current,
70
+ history,
71
+ dex_markdown(collection),
72
+ progress_markdown(collection),
73
+ )
74
+
75
+ def ask_companion(message, history, current, collection, position):
76
+ return myco_reply(message, history, current, collection, position)
77
+
78
+ def collect_discovery(current, collection, history, position):
79
+ updated_coll, updated_hist = collect_current(current, collection, history)
80
+ pos = _safe_pos(position)
81
+ return (
82
+ updated_coll,
83
+ forest_scene(current, updated_coll, pos),
84
+ game_status_markdown(current, updated_coll, pos),
85
+ dex_markdown(updated_coll),
86
+ world_map_markdown(updated_coll),
87
+ updated_hist,
88
+ progress_markdown(updated_coll),
89
+ )
90
+
91
+ def pick_discovery(current, collection, history, position):
92
+ updated_coll, updated_current, updated_hist = pick_current(current, collection, history)
93
+ pos = _safe_pos(position)
94
+ return (
95
+ updated_coll,
96
+ updated_current,
97
+ forest_scene(updated_current, updated_coll, pos),
98
+ game_status_markdown(updated_current, updated_coll, pos),
99
+ mushroom_card_from_state(updated_current),
100
+ dex_markdown(updated_coll),
101
+ world_map_markdown(updated_coll),
102
+ updated_hist,
103
+ progress_markdown(updated_coll),
104
+ )
105
+
106
+ def follow_story_whisper(current, collection, history, position):
107
+ updated_current, updated_hist = follow_whisper(current, collection, history)
108
+ pos = _safe_pos(position)
109
+ return (
110
+ updated_current,
111
+ forest_scene(updated_current, collection, pos),
112
+ game_status_markdown(updated_current, collection, pos),
113
+ mushroom_card_from_state(updated_current),
114
+ updated_hist,
115
+ )
116
+
117
+ def study_discovery(current, history, collection, position):
118
+ updated_current, updated_hist = study_current(current, history)
119
+ pos = _safe_pos(position)
120
+ return (
121
+ updated_current,
122
+ forest_scene(updated_current, collection, pos),
123
+ game_status_markdown(updated_current, collection, pos),
124
+ mushroom_card_from_state(updated_current),
125
+ updated_hist,
126
+ )
127
+
128
+ def move_player(position, direction, current, collection):
129
+ x, y = _safe_pos(position)
130
+ if direction == "north": y -= 1
131
+ elif direction == "south": y += 1
132
+ elif direction == "west": x -= 1
133
+ elif direction == "east": x += 1
134
+ pos = _safe_pos((x, y))
135
+ return pos, forest_scene(current, collection, pos), game_status_markdown(current, collection, pos)
136
+
137
+ def block_unsafe_eating(current, history):
138
+ return eat_current(current, history)
139
 
140
  def open_play_tab():
141
+ return gr.update(selected="play")
142
+
143
+ def _safe_pos(position):
144
+ if position is None:
145
+ return START_POSITION
146
+ x, y = position
147
+ return (max(0, min(2, int(x))), max(0, min(2, int(y))))
148
 
 
149
  def build_app():
150
+ with gr.Blocks(title="Myco โ€” AI Forest Game", theme=gr.themes.Soft()) as demo:
151
+ # Added 'contain' CSS to prevent layout jumping/flashing
152
  gr.HTML("""
153
  <style>
 
 
 
 
 
 
154
  .gradio-container { background: #1a1a2e !important; }
155
+ #forest-scene, #discovery-card { contain: layout style; content-visibility: auto; }
156
+ .progress-level, .progress-level-inner, .generating { display: none !important; }
157
  </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  """)
159
 
 
160
  current_mushroom = gr.State(None)
161
+ collection = gr.State([])
162
+ player_position = gr.State(START_POSITION)
163
 
 
164
  gr.HTML(hero_markdown())
165
 
166
  with gr.Tabs(selected="home") as tabs:
167
  with gr.Tab("๐Ÿ  Home", id="home"):
168
+ gr.Markdown(HOME_MARKDOWN)
169
  gr.HTML(home_forest_markdown())
170
  start_play_button = gr.Button("๐Ÿ„ Enter the Forest", variant="primary", size="lg")
171
+ gr.Markdown(f"*{hf_companion_status()}*")
172
+
173
  with gr.Tab("๐ŸŽฎ Play", id="play"):
174
  gr.HTML(game_intro_markdown())
175
+ scene = gr.HTML(forest_scene(None, EMPTY_COLLECTION, START_POSITION), elem_id="forest-scene")
 
 
 
176
 
177
  with gr.Row():
178
  with gr.Column(scale=1):
179
+ search_button = gr.Button("๐ŸŽฎ Search Clearing", variant="primary", size="lg")
180
+ with gr.Column(scale=1):
181
+ gr.Markdown("**Move Myco**")
182
  with gr.Row():
183
+ gr.HTML("<div></div>")
184
+ north_button = gr.Button("โฌ†๏ธ", size="sm")
185
+ gr.HTML("<div></div>")
186
+ with gr.Row():
187
+ west_button = gr.Button("โฌ…๏ธ", size="sm")
188
+ pick_button = gr.Button("๐Ÿ„ Pick", variant="primary", size="sm")
189
+ east_button = gr.Button("โžก๏ธ", size="sm")
190
+ with gr.Row():
191
+ gr.HTML("<div></div>")
192
+ south_button = gr.Button("โฌ‡๏ธ", size="sm")
193
+ gr.HTML("<div></div>")
194
 
195
+ status = gr.Markdown(game_status_markdown(None, EMPTY_COLLECTION, START_POSITION))
196
+
197
+ with gr.Row(equal_height=True, elem_id="discovery-card"):
198
  with gr.Column(scale=2):
199
+ gr.Markdown("### ๐Ÿ„ Current Discovery")
200
  discovery = gr.Markdown(mushroom_card(None))
 
201
  with gr.Row():
202
+ study_button = gr.Button("๐Ÿ” Study", variant="secondary")
203
+ collect_button = gr.Button("๐Ÿ“– Collect", variant="secondary")
204
+ with gr.Row():
205
+ whisper_button = gr.Button("๐ŸŒŒ Follow Whisper", variant="secondary")
206
+ eat_button = gr.Button("๐Ÿฝ๏ธ Eat?", variant="stop")
207
 
208
+ with gr.Column(scale=3):
209
+ gr.Markdown("### ๐Ÿ’ฌ Myco Chat")
210
+ chat = gr.Chatbot(value=cast(Any, welcome_history()), label="Myco", height=400, show_label=False)
211
  with gr.Row():
212
+ prompt = gr.Textbox(placeholder="Ask Myco anything about the forest...", show_label=False, scale=4, lines=1)
213
+ ask_button = gr.Button("Ask ๐Ÿ„", variant="primary", scale=1)
214
 
215
  with gr.Tab("๐Ÿ“– MycoDex"):
216
+ with gr.Row():
217
+ progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION))
218
+ dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION))
219
+ with gr.Tab("๐Ÿ—บ๏ธ World Map"):
220
  world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
221
+ with gr.Tab("๐Ÿงญ Guide"):
222
+ gr.Markdown(GUIDE_MARKDOWN)
223
 
224
+ # โ”€โ”€ Event wiring (show_progress="hidden" applied to all for zero-flash) โ”€โ”€
 
225
  start_play_button.click(open_play_tab, outputs=[tabs])
226
+
227
+ search_button.click(search_forest, inputs=[collection, player_position],
228
+ outputs=[scene, status, discovery, current_mushroom, chat, dex, progress], show_progress="hidden")
229
+
230
+ ask_button.click(ask_companion, inputs=[prompt, chat, current_mushroom, collection, player_position],
231
+ outputs=[prompt, chat], show_progress="hidden")
232
+ prompt.submit(ask_companion, inputs=[prompt, chat, current_mushroom, collection, player_position],
233
+ outputs=[prompt, chat], show_progress="hidden")
234
+
235
+ for direction, button in [("north", north_button), ("south", south_button), ("west", west_button), ("east", east_button)]:
236
+ button.click(move_player, inputs=[player_position, gr.State(direction), current_mushroom, collection],
237
+ outputs=[player_position, scene, status], show_progress="hidden")
238
+
239
+ study_button.click(study_discovery, inputs=[current_mushroom, chat, collection, player_position],
240
+ outputs=[current_mushroom, scene, status, discovery, chat], show_progress="hidden")
241
+
242
+ collect_button.click(collect_discovery, inputs=[current_mushroom, collection, chat, player_position],
243
+ outputs=[collection, scene, status, dex, world_map, chat, progress], show_progress="hidden")
244
 
245
+ pick_button.click(pick_discovery, inputs=[current_mushroom, collection, chat, player_position],
246
+ outputs=[collection, current_mushroom, scene, status, discovery, dex, world_map, chat, progress], show_progress="hidden")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
+ whisper_button.click(follow_story_whisper, inputs=[current_mushroom, collection, chat, player_position],
249
+ outputs=[current_mushroom, scene, status, discovery, chat], show_progress="hidden")
250
 
251
+ eat_button.click(block_unsafe_eating, inputs=[current_mushroom, chat], outputs=[chat], show_progress="hidden")
 
252
 
253
+ demo.queue()
254
  return demo
255