Spaces:
Sleeping

byte-vortex commited on
Commit
73b12ae
ยท
verified ยท
1 Parent(s): 5b6725d

Deploy Myco from CI

Browse files
Files changed (1) hide show
  1. ui/copy_gradio_app.py +256 -0
ui/copy_gradio_app.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
14
+ from ui.renderers import (
15
+ dex_markdown, forest_scene, game_intro_markdown,
16
+ game_status_markdown, hero_markdown, home_forest_markdown,
17
+ mushroom_card, play_hook_markdown, progress_markdown,
18
+ world_map_markdown,
19
+ )
20
+
21
+ EMPTY_COLLECTION = None
22
+ START_POSITION = (1, 1)
23
+
24
+ HOME_MARKDOWN = """
25
+ ## Welcome to the Myco forest
26
+
27
+ Myco is a tiny AI-powered forest game. Open **๐ŸŽฎ Play** to search for mushrooms,
28
+ visit **๐Ÿ“– MycoDex** to review your collection, and use **๐Ÿงญ Guide** for help.
29
+
30
+ **Goal:** solve one mystery โ€” why does Myco remember a forest that vanished before it was born?
31
+ Follow clues until the Impossible Mushroom blooms inside the MycoDex.
32
+ """
33
+
34
+ GUIDE_MARKDOWN = """
35
+ ## How to play
36
+
37
+ 1. Go to **๐ŸŽฎ Play** and click **๐ŸŽฎ Search Clearing** to discover a mushroom.
38
+ 2. **Myco (the AI) reacts** โ€” read what it senses about the mushroom.
39
+ 3. Choose your action:
40
+ - **๐Ÿ” Study** โ€” Myco examines the mushroom and updates its magic field.
41
+ - **๐Ÿ„ Pick** โ€” grab it as a game item. Poisonous picks = Game Over!
42
+ - **๐Ÿ“– Collect** โ€” add it to the MycoDex for safe keeping and score.
43
+ - **๐ŸŒŒ Follow Whisper** โ€” reveal mystery fragments. Risky on unstudied mushrooms!
44
+ - **๐Ÿฝ๏ธ Eat?** โ€” Myco will stop you every time (for good reason).
45
+ 4. **Ask Myco** anything in the chat โ€” it knows the forest, the mystery, and the mushrooms.
46
+ 5. Use **โฌ†๏ธโฌ‡๏ธโฌ…๏ธโžก๏ธ** to move through the 3ร—3 forest grid.
47
+
48
+ **Scoring:** Common = 10 spores ยท Rare = 35 ยท Legendary = 100 ยท Poison = โˆ’25 + Game Over
49
+
50
+ **Poisonous mushrooms:** Ghost Gill ยท Pepper Pixie ยท Ruby Knuckle ยท Clockwork Chanterelle
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
+ def search_forest(collection, position):
64
+ mushroom, current, history = discover_mushroom(collection)
65
+ pos = _safe_pos(position)
66
+ return (
67
+ forest_scene(current, collection, pos),
68
+ game_status_markdown(current, collection, pos),
69
+ mushroom_card_from_state(current),
70
+ current,
71
+ history,
72
+ dex_markdown(collection),
73
+ progress_markdown(collection),
74
+ )
75
+
76
+ def ask_companion(message, history, current, collection, position):
77
+ return myco_reply(message, history, current, collection, position)
78
+
79
+ def collect_discovery(current, collection, history, position):
80
+ updated_coll, updated_hist = collect_current(current, collection, history)
81
+ pos = _safe_pos(position)
82
+ return (
83
+ updated_coll,
84
+ forest_scene(current, updated_coll, pos),
85
+ game_status_markdown(current, updated_coll, pos),
86
+ dex_markdown(updated_coll),
87
+ world_map_markdown(updated_coll),
88
+ updated_hist,
89
+ progress_markdown(updated_coll),
90
+ )
91
+
92
+ def pick_discovery(current, collection, history, position):
93
+ updated_coll, updated_current, updated_hist = pick_current(current, collection, history)
94
+ pos = _safe_pos(position)
95
+ return (
96
+ updated_coll,
97
+ updated_current,
98
+ forest_scene(updated_current, updated_coll, pos),
99
+ game_status_markdown(updated_current, updated_coll, pos),
100
+ mushroom_card_from_state(updated_current),
101
+ dex_markdown(updated_coll),
102
+ world_map_markdown(updated_coll),
103
+ updated_hist,
104
+ progress_markdown(updated_coll),
105
+ )
106
+
107
+ def follow_story_whisper(current, collection, history, position):
108
+ updated_current, updated_hist = follow_whisper(current, collection, history)
109
+ pos = _safe_pos(position)
110
+ return (
111
+ updated_current,
112
+ forest_scene(updated_current, collection, pos),
113
+ game_status_markdown(updated_current, collection, pos),
114
+ mushroom_card_from_state(updated_current),
115
+ updated_hist,
116
+ )
117
+
118
+ def study_discovery(current, history, collection, position):
119
+ updated_current, updated_hist = study_current(current, history)
120
+ pos = _safe_pos(position)
121
+ return (
122
+ updated_current,
123
+ forest_scene(updated_current, collection, pos),
124
+ game_status_markdown(updated_current, collection, pos),
125
+ mushroom_card_from_state(updated_current),
126
+ updated_hist,
127
+ )
128
+
129
+ def move_player(position, direction, current, collection):
130
+ x, y = _safe_pos(position)
131
+ if direction == "north": y -= 1
132
+ elif direction == "south": y += 1
133
+ elif direction == "west": x -= 1
134
+ elif direction == "east": x += 1
135
+ pos = _safe_pos((x, y))
136
+ return pos, forest_scene(current, collection, pos), game_status_markdown(current, collection, pos)
137
+
138
+ def block_unsafe_eating(current, history):
139
+ return eat_current(current, history)
140
+
141
+ def open_play_tab():
142
+ return gr.update(selected="play")
143
+
144
+ def _safe_pos(position):
145
+ if position is None:
146
+ return START_POSITION
147
+ x, y = position
148
+ return (max(0, min(2, int(x))), max(0, min(2, int(y))))
149
+
150
+ def build_app():
151
+ with gr.Blocks(title="Myco โ€” AI Forest Game", theme=gr.themes.Soft()) as demo:
152
+ # Added 'contain' CSS to prevent layout jumping/flashing
153
+ gr.HTML("""
154
+ <style>
155
+ .gradio-container { background: #1a1a2e !important; }
156
+ #forest-scene, #discovery-card { contain: layout style; content-visibility: auto; }
157
+ .progress-level, .progress-level-inner, .generating { display: none !important; }
158
+ </style>
159
+ """)
160
+
161
+ current_mushroom = gr.State(None)
162
+ collection = gr.State([])
163
+ player_position = gr.State(START_POSITION)
164
+
165
+ gr.HTML(hero_markdown())
166
+
167
+ with gr.Tabs(selected="home") as tabs:
168
+ with gr.Tab("๐Ÿ  Home", id="home"):
169
+ gr.Markdown(HOME_MARKDOWN)
170
+ gr.HTML(home_forest_markdown())
171
+ start_play_button = gr.Button("๐Ÿ„ Enter the Forest", variant="primary", size="lg")
172
+ gr.Markdown(f"*{hf_companion_status()}*")
173
+
174
+ with gr.Tab("๐ŸŽฎ Play", id="play"):
175
+ gr.HTML(game_intro_markdown())
176
+ scene = gr.HTML(forest_scene(None, EMPTY_COLLECTION, START_POSITION), elem_id="forest-scene")
177
+
178
+ with gr.Row():
179
+ with gr.Column(scale=1):
180
+ search_button = gr.Button("๐ŸŽฎ Search Clearing", variant="primary", size="lg")
181
+ with gr.Column(scale=1):
182
+ gr.Markdown("**Move Myco**")
183
+ with gr.Row():
184
+ gr.HTML("<div></div>")
185
+ north_button = gr.Button("โฌ†๏ธ", size="sm")
186
+ gr.HTML("<div></div>")
187
+ with gr.Row():
188
+ west_button = gr.Button("โฌ…๏ธ", size="sm")
189
+ pick_button = gr.Button("๐Ÿ„ Pick", variant="primary", size="sm")
190
+ east_button = gr.Button("โžก๏ธ", size="sm")
191
+ with gr.Row():
192
+ gr.HTML("<div></div>")
193
+ south_button = gr.Button("โฌ‡๏ธ", size="sm")
194
+ gr.HTML("<div></div>")
195
+
196
+ status = gr.Markdown(game_status_markdown(None, EMPTY_COLLECTION, START_POSITION))
197
+
198
+ with gr.Row(equal_height=True, elem_id="discovery-card"):
199
+ with gr.Column(scale=2):
200
+ gr.Markdown("### ๐Ÿ„ Current Discovery")
201
+ discovery = gr.Markdown(mushroom_card(None))
202
+ with gr.Row():
203
+ study_button = gr.Button("๐Ÿ” Study", variant="secondary")
204
+ collect_button = gr.Button("๐Ÿ“– Collect", variant="secondary")
205
+ with gr.Row():
206
+ whisper_button = gr.Button("๐ŸŒŒ Follow Whisper", variant="secondary")
207
+ eat_button = gr.Button("๐Ÿฝ๏ธ Eat?", variant="stop")
208
+
209
+ with gr.Column(scale=3):
210
+ gr.Markdown("### ๐Ÿ’ฌ Myco Chat")
211
+ chat = gr.Chatbot(value=cast(Any, welcome_history()), label="Myco", height=400, show_label=False)
212
+ with gr.Row():
213
+ prompt = gr.Textbox(placeholder="Ask Myco anything about the forest...", show_label=False, scale=4, lines=1)
214
+ ask_button = gr.Button("Ask ๐Ÿ„", variant="primary", scale=1)
215
+
216
+ with gr.Tab("๐Ÿ“– MycoDex"):
217
+ with gr.Row():
218
+ progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION))
219
+ dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION))
220
+ with gr.Tab("๐Ÿ—บ๏ธ World Map"):
221
+ world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
222
+ with gr.Tab("๐Ÿงญ Guide"):
223
+ gr.Markdown(GUIDE_MARKDOWN)
224
+
225
+ # โ”€โ”€ Event wiring (show_progress="hidden" applied to all for zero-flash) โ”€โ”€
226
+ start_play_button.click(open_play_tab, outputs=[tabs])
227
+
228
+ search_button.click(search_forest, inputs=[collection, player_position],
229
+ outputs=[scene, status, discovery, current_mushroom, chat, dex, progress], show_progress="hidden")
230
+
231
+ ask_button.click(ask_companion, inputs=[prompt, chat, current_mushroom, collection, player_position],
232
+ outputs=[prompt, chat], show_progress="hidden")
233
+ prompt.submit(ask_companion, inputs=[prompt, chat, current_mushroom, collection, player_position],
234
+ outputs=[prompt, chat], show_progress="hidden")
235
+
236
+ for direction, button in [("north", north_button), ("south", south_button), ("west", west_button), ("east", east_button)]:
237
+ button.click(move_player, inputs=[player_position, gr.State(direction), current_mushroom, collection],
238
+ outputs=[player_position, scene, status], show_progress="hidden")
239
+
240
+ study_button.click(study_discovery, inputs=[current_mushroom, chat, collection, player_position],
241
+ outputs=[current_mushroom, scene, status, discovery, chat], show_progress="hidden")
242
+
243
+ collect_button.click(collect_discovery, inputs=[current_mushroom, collection, chat, player_position],
244
+ outputs=[collection, scene, status, dex, world_map, chat, progress], show_progress="hidden")
245
+
246
+ pick_button.click(pick_discovery, inputs=[current_mushroom, collection, chat, player_position],
247
+ outputs=[collection, current_mushroom, scene, status, discovery, dex, world_map, chat, progress], show_progress="hidden")
248
+
249
+ whisper_button.click(follow_story_whisper, inputs=[current_mushroom, collection, chat, player_position],
250
+ outputs=[current_mushroom, scene, status, discovery, chat], show_progress="hidden")
251
+
252
+ eat_button.click(block_unsafe_eating, inputs=[current_mushroom, chat], outputs=[chat], show_progress="hidden")
253
+
254
+ demo.queue()
255
+ return demo
256
+