Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/engine.py +79 -0
- ui/gradio_app.py +68 -0
game/engine.py
CHANGED
|
@@ -1,5 +1,33 @@
|
|
| 1 |
"""Core Myco gameplay actions independent from Gradio."""
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import random
|
|
@@ -69,6 +97,38 @@ def myco_reply(
|
|
| 69 |
return "", [*next_history, _assistant_message(answer)]
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def collect_current(
|
| 73 |
current: MushroomState | None,
|
| 74 |
collection: CollectionState | None,
|
|
@@ -112,6 +172,17 @@ def study_current(
|
|
| 112 |
f"I studied the {current['name']} closely. New MycoDex note: "
|
| 113 |
f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
|
| 114 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
return studied, [*current_history, {"role": "assistant", "content": message}]
|
| 116 |
|
| 117 |
|
|
@@ -134,6 +205,10 @@ def eat_current(current: MushroomState | None, history: ChatHistory | None) -> C
|
|
| 134 |
f"Its edible field is still **{mushroom.edible}**, "
|
| 135 |
"so the winning move is to study or collect it instead of eating it."
|
| 136 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
next_history.append({"role": "assistant", "content": message})
|
| 138 |
return next_history
|
| 139 |
|
|
@@ -143,21 +218,25 @@ def _assistant_message(content: str) -> dict:
|
|
| 143 |
return {"role": "assistant", "content": content}
|
| 144 |
|
| 145 |
|
|
|
|
| 146 |
def _append_assistant(history: ChatHistory, content: str) -> ChatHistory:
|
| 147 |
"""Return chat history with one assistant message appended."""
|
| 148 |
return [*_safe_history(history), _assistant_message(content)]
|
| 149 |
|
| 150 |
|
|
|
|
| 151 |
def _safe_collection(collection: CollectionState | None) -> CollectionState:
|
| 152 |
"""Return a copy of the collection, tolerating an uninitialized Gradio state."""
|
| 153 |
return list(collection or [])
|
| 154 |
|
| 155 |
|
|
|
|
| 156 |
def _safe_history(history: ChatHistory | None) -> ChatHistory:
|
| 157 |
"""Return a copy of chat history, tolerating an uninitialized Gradio state."""
|
| 158 |
return list(history or welcome_history())
|
| 159 |
|
| 160 |
|
|
|
|
| 161 |
def _study_hint(rarity: str) -> str:
|
| 162 |
"""Return a premium-feeling but cautious study result by rarity."""
|
| 163 |
if rarity == "Legendary":
|
|
|
|
| 1 |
"""Core Myco gameplay actions independent from Gradio."""
|
| 2 |
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
from game.catalog import load_mushrooms
|
| 6 |
+
from game.state import collection_contains, mushroom_from_state, welcome_history
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def choose_mushroom(catalog=None):
|
| 10 |
+
"""Choose a mushroom discovery from the catalog."""
|
| 11 |
+
mushrooms = tuple(load_mushrooms() if catalog is None else catalog)
|
| 12 |
+
if not mushrooms:
|
| 13 |
+
raise ValueError("Myco needs at least one mushroom in the catalog.")
|
| 14 |
+
return random.choice(mushrooms)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def discover_mushroom(collection=None, catalog=None):
|
| 18 |
+
"""Discover a mushroom and return the updated discovery state."""
|
| 19 |
+
current_collection = _safe_collection(collection)
|
| 20 |
+
mushroom = choose_mushroom(catalog)
|
| 21 |
+
current = mushroom.to_dict()
|
| 22 |
+
message = (
|
| 23 |
+
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
|
| 24 |
+
f"eat, collect, or study it. Your MycoDex has {len(current_collection)} entries."
|
| 25 |
+
)
|
| 26 |
+
history = _append_assistant(welcome_history(), message)
|
| 27 |
+
return mushroom, current, history
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def myco_reply(message=None, history=None, current=None):
|
| 31 |
from __future__ import annotations
|
| 32 |
|
| 33 |
import random
|
|
|
|
| 97 |
return "", [*next_history, _assistant_message(answer)]
|
| 98 |
|
| 99 |
|
| 100 |
+
def collect_current(current=None, collection=None, history=None):
|
| 101 |
+
"""Add the current mushroom to the MycoDex if it is not already collected."""
|
| 102 |
+
current_collection = _safe_collection(collection)
|
| 103 |
+
current_history = _safe_history(history)
|
| 104 |
+
|
| 105 |
+
if current is None:
|
| 106 |
+
return current_collection, _append_assistant(
|
| 107 |
+
current_history,
|
| 108 |
+
"Let's discover a mushroom before updating the MycoDex.",
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
if collection_contains(current_collection, current["name"]):
|
| 112 |
+
return current_collection, _append_assistant(
|
| 113 |
+
current_history,
|
| 114 |
+
f"{current['name']} is already safe in your MycoDex.",
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
updated_collection = [*current_collection, current]
|
| 118 |
+
return updated_collection, _append_assistant(
|
| 119 |
+
current_history,
|
| 120 |
+
f"Added {current['name']} to the MycoDex. Nice find!",
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def study_current(current=None, history=None):
|
| 125 |
+
"""Study the current mushroom to reveal one cautious field observation."""
|
| 126 |
+
current_history = _safe_history(history)
|
| 127 |
+
if current is None:
|
| 128 |
+
return None, _append_assistant(
|
| 129 |
+
current_history,
|
| 130 |
+
"Let's find a mushroom before we study anything.",
|
| 131 |
+
)
|
| 132 |
def collect_current(
|
| 133 |
current: MushroomState | None,
|
| 134 |
collection: CollectionState | None,
|
|
|
|
| 172 |
f"I studied the {current['name']} closely. New MycoDex note: "
|
| 173 |
f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
|
| 174 |
)
|
| 175 |
+
return studied, _append_assistant(current_history, message)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def eat_current(current=None, history=None):
|
| 179 |
+
"""Handle the Eat choice safely with Myco's companion personality."""
|
| 180 |
+
current_history = _safe_history(history)
|
| 181 |
+
if current is None:
|
| 182 |
+
return _append_assistant(
|
| 183 |
+
current_history,
|
| 184 |
+
"Let's discover a mushroom before making snack decisions.",
|
| 185 |
+
)
|
| 186 |
return studied, [*current_history, {"role": "assistant", "content": message}]
|
| 187 |
|
| 188 |
|
|
|
|
| 205 |
f"Its edible field is still **{mushroom.edible}**, "
|
| 206 |
"so the winning move is to study or collect it instead of eating it."
|
| 207 |
)
|
| 208 |
+
return _append_assistant(current_history, message)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def _assistant_message(content):
|
| 212 |
next_history.append({"role": "assistant", "content": message})
|
| 213 |
return next_history
|
| 214 |
|
|
|
|
| 218 |
return {"role": "assistant", "content": content}
|
| 219 |
|
| 220 |
|
| 221 |
+
def _append_assistant(history, content):
|
| 222 |
def _append_assistant(history: ChatHistory, content: str) -> ChatHistory:
|
| 223 |
"""Return chat history with one assistant message appended."""
|
| 224 |
return [*_safe_history(history), _assistant_message(content)]
|
| 225 |
|
| 226 |
|
| 227 |
+
def _safe_collection(collection):
|
| 228 |
def _safe_collection(collection: CollectionState | None) -> CollectionState:
|
| 229 |
"""Return a copy of the collection, tolerating an uninitialized Gradio state."""
|
| 230 |
return list(collection or [])
|
| 231 |
|
| 232 |
|
| 233 |
+
def _safe_history(history):
|
| 234 |
def _safe_history(history: ChatHistory | None) -> ChatHistory:
|
| 235 |
"""Return a copy of chat history, tolerating an uninitialized Gradio state."""
|
| 236 |
return list(history or welcome_history())
|
| 237 |
|
| 238 |
|
| 239 |
+
def _study_hint(rarity):
|
| 240 |
def _study_hint(rarity: str) -> str:
|
| 241 |
"""Return a premium-feeling but cautious study result by rarity."""
|
| 242 |
if rarity == "Legendary":
|
ui/gradio_app.py
CHANGED
|
@@ -4,6 +4,10 @@ Callbacks intentionally avoid complex type annotations because Hugging Face Spac
|
|
| 4 |
was serving an older broken annotated version of this file.
|
| 5 |
"""
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from __future__ import annotations
|
| 8 |
|
| 9 |
import gradio as gr
|
|
@@ -15,6 +19,7 @@ from ui.renderers import dex_markdown, forest_scene, hero_markdown, mushroom_car
|
|
| 15 |
|
| 16 |
|
| 17 |
# Keep callback signatures plain for Hugging Face Spaces parser stability.
|
|
|
|
| 18 |
def explore(collection: CollectionState) -> tuple[str, str, MushroomState, list[dict[str, str]], str, str]:
|
| 19 |
"""Gradio callback for the Start Exploring button."""
|
| 20 |
mushroom, current, history = discover_mushroom(collection)
|
|
@@ -28,6 +33,7 @@ def explore(collection: CollectionState) -> tuple[str, str, MushroomState, list[
|
|
| 28 |
)
|
| 29 |
|
| 30 |
|
|
|
|
| 31 |
def ask_myco(
|
| 32 |
message: str,
|
| 33 |
history: list[dict[str, str]],
|
|
@@ -37,6 +43,7 @@ def ask_myco(
|
|
| 37 |
return myco_reply(message, history, current)
|
| 38 |
|
| 39 |
|
|
|
|
| 40 |
def add_to_dex(
|
| 41 |
current: MushroomState | None,
|
| 42 |
collection: CollectionState,
|
|
@@ -53,6 +60,7 @@ def add_to_dex(
|
|
| 53 |
)
|
| 54 |
|
| 55 |
|
|
|
|
| 56 |
def study_mushroom(
|
| 57 |
current: MushroomState | None,
|
| 58 |
history: list[dict[str, str]],
|
|
@@ -68,6 +76,7 @@ def study_mushroom(
|
|
| 68 |
)
|
| 69 |
|
| 70 |
|
|
|
|
| 71 |
def eat_mushroom(
|
| 72 |
current: MushroomState | None,
|
| 73 |
history: list[dict[str, str]],
|
|
@@ -76,6 +85,7 @@ def eat_mushroom(
|
|
| 76 |
return eat_current(current, history)
|
| 77 |
|
| 78 |
|
|
|
|
| 79 |
def mushroom_card_from_state(current: MushroomState | None) -> str:
|
| 80 |
"""Render a mushroom card from Gradio state."""
|
| 81 |
if current is None:
|
|
@@ -83,6 +93,7 @@ def mushroom_card_from_state(current: MushroomState | None) -> str:
|
|
| 83 |
return mushroom_card(Mushroom.from_dict(current))
|
| 84 |
|
| 85 |
|
|
|
|
| 86 |
def build_app() -> gr.Blocks:
|
| 87 |
"""Build the Gradio interface for Hugging Face Spaces."""
|
| 88 |
with gr.Blocks(title="Myco") as demo:
|
|
@@ -91,6 +102,63 @@ def build_app() -> gr.Blocks:
|
|
| 91 |
|
| 92 |
gr.HTML(hero_markdown())
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
scene = gr.HTML(forest_scene(None, []))
|
| 95 |
|
| 96 |
with gr.Row():
|
|
|
|
| 4 |
was serving an older broken annotated version of this file.
|
| 5 |
"""
|
| 6 |
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
from game.engine import collect_current, discover_mushroom, eat_current, myco_reply, study_current
|
| 10 |
+
from game.state import welcome_history
|
| 11 |
from __future__ import annotations
|
| 12 |
|
| 13 |
import gradio as gr
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
# Keep callback signatures plain for Hugging Face Spaces parser stability.
|
| 22 |
+
def start_exploring(collection):
|
| 23 |
def explore(collection: CollectionState) -> tuple[str, str, MushroomState, list[dict[str, str]], str, str]:
|
| 24 |
"""Gradio callback for the Start Exploring button."""
|
| 25 |
mushroom, current, history = discover_mushroom(collection)
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
|
| 36 |
+
def ask_myco(message, history, current):
|
| 37 |
def ask_myco(
|
| 38 |
message: str,
|
| 39 |
history: list[dict[str, str]],
|
|
|
|
| 43 |
return myco_reply(message, history, current)
|
| 44 |
|
| 45 |
|
| 46 |
+
def add_to_dex(current, collection, history):
|
| 47 |
def add_to_dex(
|
| 48 |
current: MushroomState | None,
|
| 49 |
collection: CollectionState,
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
|
| 63 |
+
def study_mushroom(current, history, collection):
|
| 64 |
def study_mushroom(
|
| 65 |
current: MushroomState | None,
|
| 66 |
history: list[dict[str, str]],
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
|
| 79 |
+
def eat_mushroom(current, history):
|
| 80 |
def eat_mushroom(
|
| 81 |
current: MushroomState | None,
|
| 82 |
history: list[dict[str, str]],
|
|
|
|
| 85 |
return eat_current(current, history)
|
| 86 |
|
| 87 |
|
| 88 |
+
def mushroom_card_from_state(current):
|
| 89 |
def mushroom_card_from_state(current: MushroomState | None) -> str:
|
| 90 |
"""Render a mushroom card from Gradio state."""
|
| 91 |
if current is None:
|
|
|
|
| 93 |
return mushroom_card(Mushroom.from_dict(current))
|
| 94 |
|
| 95 |
|
| 96 |
+
def build_app():
|
| 97 |
def build_app() -> gr.Blocks:
|
| 98 |
"""Build the Gradio interface for Hugging Face Spaces."""
|
| 99 |
with gr.Blocks(title="Myco") as demo:
|
|
|
|
| 102 |
|
| 103 |
gr.HTML(hero_markdown())
|
| 104 |
|
| 105 |
+
with gr.Tabs():
|
| 106 |
+
with gr.Tab("๐ Home"):
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"""
|
| 109 |
+
## Welcome to the Myco forest
|
| 110 |
+
|
| 111 |
+
Myco now plays more like a tiny game portal: open **๐ฎ Play** to explore,
|
| 112 |
+
visit **๐ MycoDex** to review progress, and use **๐งญ Guide** when you need a reminder.
|
| 113 |
+
|
| 114 |
+
**Goal:** discover strange mushrooms, ask Myco what they might be, study them safely,
|
| 115 |
+
and build a magical MycoDex collection.
|
| 116 |
+
"""
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Tab("๐ฎ Play"):
|
| 120 |
+
scene = gr.HTML(forest_scene(None, []))
|
| 121 |
+
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column(scale=2):
|
| 124 |
+
discovery = gr.Markdown(mushroom_card(None), elem_id="discovery-card")
|
| 125 |
+
with gr.Row():
|
| 126 |
+
explore_button = gr.Button("Start Exploring", variant="primary")
|
| 127 |
+
with gr.Row():
|
| 128 |
+
eat_button = gr.Button("Eat")
|
| 129 |
+
collect_button = gr.Button("Collect")
|
| 130 |
+
study_button = gr.Button("Study")
|
| 131 |
+
with gr.Column(scale=3):
|
| 132 |
+
chat = gr.Chatbot(value=welcome_history(), label="Myco Chat", height=360)
|
| 133 |
+
with gr.Row():
|
| 134 |
+
textbox_component = gr.Textbox
|
| 135 |
+
prompt = textbox_component(
|
| 136 |
+
label="Ask Myco",
|
| 137 |
+
placeholder="Ask Myco about this mushroom",
|
| 138 |
+
scale=4,
|
| 139 |
+
)
|
| 140 |
+
ask_button = gr.Button("Ask Myco", scale=1)
|
| 141 |
+
|
| 142 |
+
with gr.Tab("๐ MycoDex"):
|
| 143 |
+
with gr.Row():
|
| 144 |
+
progress = gr.Markdown(progress_markdown([]), elem_id="progress-panel")
|
| 145 |
+
dex = gr.Markdown(dex_markdown([]), elem_id="mycodex")
|
| 146 |
+
|
| 147 |
+
with gr.Tab("๐งญ Guide"):
|
| 148 |
+
gr.Markdown(
|
| 149 |
+
"""
|
| 150 |
+
## How to play
|
| 151 |
+
|
| 152 |
+
1. Go to **๐ฎ Play**.
|
| 153 |
+
2. Click **Start Exploring** to reveal a mushroom in the forest scene.
|
| 154 |
+
3. Ask Myco about the mushroom.
|
| 155 |
+
4. Choose **Study** for a safer hint, **Collect** for the MycoDex, or **Eat** to see Myco stop you.
|
| 156 |
+
5. Check **๐ MycoDex** to track discoveries and area unlocks.
|
| 157 |
+
"""
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
explore_button.click(
|
| 161 |
+
start_exploring,
|
| 162 |
scene = gr.HTML(forest_scene(None, []))
|
| 163 |
|
| 164 |
with gr.Row():
|