Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/engine.py +153 -82
- game/progression.py +0 -2
- game/state.py +0 -2
- models/mushroom.py +0 -2
- requirements.txt +1 -0
- tests/test_myco_core.py +120 -24
- ui/gradio_app.py +70 -53
- ui/renderers.py +65 -17
- ui/styles.py +76 -5
game/engine.py
CHANGED
|
@@ -1,139 +1,211 @@
|
|
| 1 |
"""Core Myco gameplay actions independent from Gradio."""
|
| 2 |
|
|
|
|
| 3 |
import random
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
from game.catalog import load_mushrooms
|
| 7 |
-
from game.state import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
)
|
| 15 |
-
from models.mushroom import Mushroom
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def choose_mushroom(catalog: Sequence[Mushroom] | None = None) -> Mushroom:
|
| 19 |
"""Choose a mushroom discovery from the catalog."""
|
| 20 |
-
mushrooms = tuple(catalog
|
| 21 |
if not mushrooms:
|
| 22 |
raise ValueError("Myco needs at least one mushroom in the catalog.")
|
| 23 |
return random.choice(mushrooms)
|
| 24 |
|
| 25 |
|
| 26 |
-
def discover_mushroom(
|
| 27 |
-
collection: CollectionState | None = None,
|
| 28 |
-
catalog: Sequence[Mushroom] | None = None,
|
| 29 |
-
) -> tuple[Mushroom, MushroomState, ChatHistory]:
|
| 30 |
"""Discover a mushroom and return the updated discovery state."""
|
|
|
|
| 31 |
mushroom = choose_mushroom(catalog)
|
| 32 |
-
discovery_count = len(collection or [])
|
| 33 |
current = mushroom.to_dict()
|
| 34 |
-
|
| 35 |
-
history
|
| 36 |
-
{
|
| 37 |
-
"role": "assistant",
|
| 38 |
-
"content": (
|
| 39 |
-
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
|
| 40 |
-
f"eat, collect, or study it. Your MycoDex has {discovery_count} entries."
|
| 41 |
-
),
|
| 42 |
-
}
|
| 43 |
-
)
|
| 44 |
return mushroom, current, history
|
| 45 |
|
| 46 |
|
| 47 |
-
def myco_reply(
|
| 48 |
-
|
| 49 |
-
history: ChatHistory | None,
|
| 50 |
-
current: MushroomState | None,
|
| 51 |
-
) -> tuple[str, ChatHistory]:
|
| 52 |
-
"""Generate a lightweight companion reply without requiring an external model."""
|
| 53 |
current_history = _safe_history(history)
|
| 54 |
clean_message = (message or "").strip()
|
| 55 |
if not clean_message:
|
| 56 |
return "", current_history
|
| 57 |
|
| 58 |
next_history = [*current_history, {"role": "user", "content": clean_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
if current:
|
| 61 |
mushroom = mushroom_from_state(current)
|
| 62 |
-
|
| 63 |
f"I think this is **{mushroom.name}**. {mushroom.lore} "
|
| 64 |
"The MycoDex still marks edible, magic, and danger as unknown, "
|
| 65 |
"so let's observe it gently instead of tasting anything."
|
| 66 |
)
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
companion_reply = myco_reply
|
| 74 |
|
| 75 |
|
| 76 |
-
def collect_current(
|
| 77 |
-
current: MushroomState | None,
|
| 78 |
-
collection: CollectionState | None,
|
| 79 |
-
history: ChatHistory | None,
|
| 80 |
-
) -> tuple[CollectionState, ChatHistory]:
|
| 81 |
"""Add the current mushroom to the MycoDex if it is not already collected."""
|
| 82 |
current_collection = _safe_collection(collection)
|
| 83 |
current_history = _safe_history(history)
|
| 84 |
|
| 85 |
if current is None:
|
| 86 |
-
return current_collection,
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
if collection_contains(current_collection, current["name"]):
|
| 92 |
-
return current_collection,
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
updated_collection = [*current_collection, current]
|
| 98 |
-
return updated_collection,
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
|
| 104 |
-
def study_current(
|
| 105 |
-
current: MushroomState | None,
|
| 106 |
-
history: ChatHistory | None,
|
| 107 |
-
) -> tuple[MushroomState | None, ChatHistory]:
|
| 108 |
"""Study the current mushroom to reveal one cautious field observation."""
|
| 109 |
current_history = _safe_history(history)
|
| 110 |
-
|
| 111 |
if current is None:
|
| 112 |
-
return None,
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
| 117 |
studied = {**current, "magic": _study_hint(current["rarity"]), "studied": "Yes"}
|
| 118 |
message = (
|
| 119 |
f"I studied the {current['name']} closely. New MycoDex note: "
|
| 120 |
f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
|
| 121 |
)
|
| 122 |
-
return studied,
|
| 123 |
|
| 124 |
|
| 125 |
-
def eat_current(
|
| 126 |
-
current: MushroomState | None,
|
| 127 |
-
history: ChatHistory | None,
|
| 128 |
-
) -> ChatHistory:
|
| 129 |
"""Handle the Eat choice safely with Myco's companion personality."""
|
| 130 |
-
|
| 131 |
-
|
| 132 |
if current is None:
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
)
|
| 136 |
-
return next_history
|
| 137 |
|
| 138 |
mushroom = mushroom_from_state(current)
|
| 139 |
message = (
|
|
@@ -141,31 +213,30 @@ def eat_current(
|
|
| 141 |
f"Its edible field is still **{mushroom.edible}**, "
|
| 142 |
"so the winning move is to study or collect it instead of eating it."
|
| 143 |
)
|
| 144 |
-
|
| 145 |
-
return next_history
|
| 146 |
|
| 147 |
|
| 148 |
-
def _assistant_message(content
|
| 149 |
"""Return one assistant chat message."""
|
| 150 |
return {"role": "assistant", "content": content}
|
| 151 |
|
| 152 |
|
| 153 |
-
def _append_assistant(history
|
| 154 |
"""Return chat history with one assistant message appended."""
|
| 155 |
return [*_safe_history(history), _assistant_message(content)]
|
| 156 |
|
| 157 |
|
| 158 |
-
def _safe_collection(collection
|
| 159 |
"""Return a copy of the collection, tolerating an uninitialized Gradio state."""
|
| 160 |
return list(collection or [])
|
| 161 |
|
| 162 |
|
| 163 |
-
def _safe_history(history
|
| 164 |
"""Return a copy of chat history, tolerating an uninitialized Gradio state."""
|
| 165 |
return list(history or welcome_history())
|
| 166 |
|
| 167 |
|
| 168 |
-
def _study_hint(rarity
|
| 169 |
"""Return a premium-feeling but cautious study result by rarity."""
|
| 170 |
if rarity == "Legendary":
|
| 171 |
return "Strong, mysterious aura"
|
|
|
|
| 1 |
"""Core Myco gameplay actions independent from Gradio."""
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
import random
|
| 5 |
+
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
from game.catalog import load_mushrooms
|
| 9 |
+
from game.state import collection_contains, mushroom_from_state, welcome_history
|
| 10 |
+
|
| 11 |
+
DEFAULT_HF_MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
|
| 12 |
+
HF_TOKEN_ENV_VARS = ("HF_TOKEN", "HF_API_TOKEN", "HF_BUILD_SMALL_HACKATHON_TOKEN")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def choose_mushroom(catalog=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""Choose a mushroom discovery from the catalog."""
|
| 17 |
+
mushrooms = tuple(load_mushrooms() if catalog is None else catalog)
|
| 18 |
if not mushrooms:
|
| 19 |
raise ValueError("Myco needs at least one mushroom in the catalog.")
|
| 20 |
return random.choice(mushrooms)
|
| 21 |
|
| 22 |
|
| 23 |
+
def discover_mushroom(collection=None, catalog=None):
|
|
|
|
|
|
|
|
|
|
| 24 |
"""Discover a mushroom and return the updated discovery state."""
|
| 25 |
+
current_collection = _safe_collection(collection)
|
| 26 |
mushroom = choose_mushroom(catalog)
|
|
|
|
| 27 |
current = mushroom.to_dict()
|
| 28 |
+
message = _discovery_reaction(current, len(current_collection))
|
| 29 |
+
history = _append_assistant(welcome_history(), message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return mushroom, current, history
|
| 31 |
|
| 32 |
|
| 33 |
+
def myco_reply(message=None, history=None, current=None):
|
| 34 |
+
"""Generate Myco's companion reply with the Hugging Face API when available."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
current_history = _safe_history(history)
|
| 36 |
clean_message = (message or "").strip()
|
| 37 |
if not clean_message:
|
| 38 |
return "", current_history
|
| 39 |
|
| 40 |
next_history = [*current_history, {"role": "user", "content": clean_message}]
|
| 41 |
+
answer = _companion_answer(clean_message, current)
|
| 42 |
+
|
| 43 |
+
return "", [*next_history, _assistant_message(answer)]
|
| 44 |
+
|
| 45 |
|
| 46 |
+
def _companion_answer(message, current):
|
| 47 |
+
"""Return the hosted-model reply, falling back to Myco's local voice."""
|
| 48 |
+
answer = _hf_companion_answer(message, current)
|
| 49 |
+
if answer is None:
|
| 50 |
+
return _local_companion_answer(current)
|
| 51 |
+
return answer
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def _discovery_reaction(current, collection_count):
|
| 55 |
+
"""Return Myco's first reaction to a new discovery."""
|
| 56 |
+
mushroom = mushroom_from_state(current)
|
| 57 |
+
prompt = (
|
| 58 |
+
f"The player just discovered {mushroom.name}. "
|
| 59 |
+
"React as Myco and invite them to ask, study, collect, or avoid eating it."
|
| 60 |
+
)
|
| 61 |
+
answer = _hf_companion_answer(prompt, current)
|
| 62 |
+
if answer:
|
| 63 |
+
return answer
|
| 64 |
+
|
| 65 |
+
return (
|
| 66 |
+
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
|
| 67 |
+
f"eat, collect, or study it. Your MycoDex has {collection_count} entries."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _local_companion_answer(current):
|
| 72 |
+
"""Return the deterministic fallback companion answer."""
|
| 73 |
if current:
|
| 74 |
mushroom = mushroom_from_state(current)
|
| 75 |
+
return (
|
| 76 |
f"I think this is **{mushroom.name}**. {mushroom.lore} "
|
| 77 |
"The MycoDex still marks edible, magic, and danger as unknown, "
|
| 78 |
"so let's observe it gently instead of tasting anything."
|
| 79 |
)
|
| 80 |
+
return "Let's explore first. The forest usually reveals a mushroom when we move slowly and listen."
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def _hf_companion_answer(message, current):
|
| 84 |
+
"""Ask a Hugging Face-hosted model for Myco's reply when a token exists."""
|
| 85 |
+
token = _hf_token()
|
| 86 |
+
if not token:
|
| 87 |
+
return None
|
| 88 |
+
|
| 89 |
+
model = hf_model_id()
|
| 90 |
+
messages = _hf_messages(message, current)
|
| 91 |
+
try:
|
| 92 |
+
response = InferenceClient(token=token).chat_completion(
|
| 93 |
+
messages=messages,
|
| 94 |
+
model=model,
|
| 95 |
+
max_tokens=90,
|
| 96 |
+
temperature=0.8,
|
| 97 |
+
)
|
| 98 |
+
except Exception:
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
+
answer = response.choices[0].message.content.strip()
|
| 102 |
+
return answer or None
|
| 103 |
|
| 104 |
+
|
| 105 |
+
def hf_model_id():
|
| 106 |
+
"""Return the configured Hugging Face model id for Myco."""
|
| 107 |
+
return os.getenv("MYCO_HF_MODEL_ID") or DEFAULT_HF_MODEL_ID
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def hf_companion_status():
|
| 111 |
+
"""Return a player-safe status line for the companion model."""
|
| 112 |
+
model = hf_model_id()
|
| 113 |
+
if _hf_token():
|
| 114 |
+
return f"🧠 Myco AI: connected to Hugging Face model `{model}`."
|
| 115 |
+
return f"🧠 Myco AI: waiting for `HF_TOKEN`; fallback voice is active with `{model}` ready."
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def _hf_token():
|
| 119 |
+
"""Return the first configured Hugging Face token without exposing it."""
|
| 120 |
+
for env_name in HF_TOKEN_ENV_VARS:
|
| 121 |
+
token = os.getenv(env_name)
|
| 122 |
+
if token:
|
| 123 |
+
return token
|
| 124 |
+
return ""
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def _hf_messages(message, current):
|
| 128 |
+
"""Build a compact chat-completion prompt for Myco."""
|
| 129 |
+
mushroom_context = "No mushroom is currently discovered."
|
| 130 |
+
if current:
|
| 131 |
+
mushroom = mushroom_from_state(current)
|
| 132 |
+
mushroom_context = (
|
| 133 |
+
f"Current mushroom: {mushroom.name}. "
|
| 134 |
+
f"Rarity: {mushroom.rarity}. "
|
| 135 |
+
f"Edible: {mushroom.edible}. "
|
| 136 |
+
f"Magic: {mushroom.magic}. "
|
| 137 |
+
f"Danger: {mushroom.danger}. "
|
| 138 |
+
f"Lore: {mushroom.lore}"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
return (
|
| 142 |
+
{
|
| 143 |
+
"role": "system",
|
| 144 |
+
"content": (
|
| 145 |
+
"You are Myco, a cozy mushroom companion in a small forest game. "
|
| 146 |
+
"Answer in 1-3 short sentences. Stay whimsical, cautious, and helpful. "
|
| 147 |
+
"Never claim an unknown mushroom is safe to eat."
|
| 148 |
+
),
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"role": "user",
|
| 152 |
+
"content": f"{mushroom_context} Player asks: {message}",
|
| 153 |
+
},
|
| 154 |
+
)
|
| 155 |
|
| 156 |
|
| 157 |
companion_reply = myco_reply
|
| 158 |
|
| 159 |
|
| 160 |
+
def collect_current(current=None, collection=None, history=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
"""Add the current mushroom to the MycoDex if it is not already collected."""
|
| 162 |
current_collection = _safe_collection(collection)
|
| 163 |
current_history = _safe_history(history)
|
| 164 |
|
| 165 |
if current is None:
|
| 166 |
+
return current_collection, _append_assistant(
|
| 167 |
+
current_history,
|
| 168 |
+
"Let's discover a mushroom before updating the MycoDex.",
|
| 169 |
+
)
|
| 170 |
|
| 171 |
if collection_contains(current_collection, current["name"]):
|
| 172 |
+
return current_collection, _append_assistant(
|
| 173 |
+
current_history,
|
| 174 |
+
f"{current['name']} is already safe in your MycoDex.",
|
| 175 |
+
)
|
| 176 |
|
| 177 |
updated_collection = [*current_collection, current]
|
| 178 |
+
return updated_collection, _append_assistant(
|
| 179 |
+
current_history,
|
| 180 |
+
f"Added {current['name']} to the MycoDex. Nice find!",
|
| 181 |
+
)
|
| 182 |
|
| 183 |
|
| 184 |
+
def study_current(current=None, history=None):
|
|
|
|
|
|
|
|
|
|
| 185 |
"""Study the current mushroom to reveal one cautious field observation."""
|
| 186 |
current_history = _safe_history(history)
|
|
|
|
| 187 |
if current is None:
|
| 188 |
+
return None, _append_assistant(
|
| 189 |
+
current_history,
|
| 190 |
+
"Let's find a mushroom before we study anything.",
|
| 191 |
+
)
|
| 192 |
|
| 193 |
studied = {**current, "magic": _study_hint(current["rarity"]), "studied": "Yes"}
|
| 194 |
message = (
|
| 195 |
f"I studied the {current['name']} closely. New MycoDex note: "
|
| 196 |
f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
|
| 197 |
)
|
| 198 |
+
return studied, _append_assistant(current_history, message)
|
| 199 |
|
| 200 |
|
| 201 |
+
def eat_current(current=None, history=None):
|
|
|
|
|
|
|
|
|
|
| 202 |
"""Handle the Eat choice safely with Myco's companion personality."""
|
| 203 |
+
current_history = _safe_history(history)
|
|
|
|
| 204 |
if current is None:
|
| 205 |
+
return _append_assistant(
|
| 206 |
+
current_history,
|
| 207 |
+
"Let's discover a mushroom before making snack decisions.",
|
| 208 |
)
|
|
|
|
| 209 |
|
| 210 |
mushroom = mushroom_from_state(current)
|
| 211 |
message = (
|
|
|
|
| 213 |
f"Its edible field is still **{mushroom.edible}**, "
|
| 214 |
"so the winning move is to study or collect it instead of eating it."
|
| 215 |
)
|
| 216 |
+
return _append_assistant(current_history, message)
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
+
def _assistant_message(content):
|
| 220 |
"""Return one assistant chat message."""
|
| 221 |
return {"role": "assistant", "content": content}
|
| 222 |
|
| 223 |
|
| 224 |
+
def _append_assistant(history, content):
|
| 225 |
"""Return chat history with one assistant message appended."""
|
| 226 |
return [*_safe_history(history), _assistant_message(content)]
|
| 227 |
|
| 228 |
|
| 229 |
+
def _safe_collection(collection):
|
| 230 |
"""Return a copy of the collection, tolerating an uninitialized Gradio state."""
|
| 231 |
return list(collection or [])
|
| 232 |
|
| 233 |
|
| 234 |
+
def _safe_history(history):
|
| 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 |
"""Return a premium-feeling but cautious study result by rarity."""
|
| 241 |
if rarity == "Legendary":
|
| 242 |
return "Strong, mysterious aura"
|
game/progression.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
"""Progression goals for Myco's collectible forest loop."""
|
| 2 |
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
from dataclasses import dataclass
|
| 6 |
|
| 7 |
|
|
|
|
| 1 |
"""Progression goals for Myco's collectible forest loop."""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from dataclasses import dataclass
|
| 4 |
|
| 5 |
|
game/state.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
"""State helpers for Myco's first playable loop."""
|
| 2 |
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
from models.mushroom import Mushroom
|
| 6 |
|
| 7 |
WELCOME_MESSAGE = "Hello! I'm Myco. Let's explore the forest."
|
|
|
|
| 1 |
"""State helpers for Myco's first playable loop."""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from models.mushroom import Mushroom
|
| 4 |
|
| 5 |
WELCOME_MESSAGE = "Hello! I'm Myco. Let's explore the forest."
|
models/mushroom.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
"""Domain models for Myco discoveries."""
|
| 2 |
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
from dataclasses import dataclass
|
| 6 |
from typing import Any
|
| 7 |
|
|
|
|
| 1 |
"""Domain models for Myco discoveries."""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from dataclasses import dataclass
|
| 4 |
from typing import Any
|
| 5 |
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
gradio==6.16.0
|
|
|
|
|
|
| 1 |
gradio==6.16.0
|
| 2 |
+
huggingface_hub>=1.0,<2.0
|
tests/test_myco_core.py
CHANGED
|
@@ -1,16 +1,21 @@
|
|
| 1 |
"""Regression tests for Myco's core game loop and UI builders."""
|
| 2 |
|
| 3 |
import ast
|
|
|
|
| 4 |
import unittest
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
-
|
| 7 |
|
| 8 |
from game.catalog import load_mushrooms
|
| 9 |
from game.engine import (
|
|
|
|
| 10 |
collect_current,
|
| 11 |
companion_reply,
|
| 12 |
discover_mushroom,
|
| 13 |
eat_current,
|
|
|
|
|
|
|
| 14 |
study_current,
|
| 15 |
)
|
| 16 |
from game.progression import current_area, next_area
|
|
@@ -20,9 +25,17 @@ from ui.renderers import (
|
|
| 20 |
forest_scene,
|
| 21 |
game_status_markdown,
|
| 22 |
progress_markdown,
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
class MycoCoreTests(unittest.TestCase):
|
| 27 |
def test_catalog_has_expected_size_and_rarities(self):
|
| 28 |
mushrooms = load_mushrooms()
|
|
@@ -42,15 +55,112 @@ class MycoCoreTests(unittest.TestCase):
|
|
| 42 |
self.assertIn("forest-scene", scene)
|
| 43 |
self.assertIn(mushroom.name, scene)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def test_companion_reply_adds_user_and_assistant_messages(self):
|
| 46 |
_mushroom, current, history = discover_mushroom([])
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
self.assertEqual(prompt, "")
|
| 50 |
self.assertEqual(updated_history[-2]["role"], "user")
|
| 51 |
self.assertEqual(updated_history[-1]["role"], "assistant")
|
| 52 |
self.assertIn(current["name"], updated_history[-1]["content"])
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def test_collect_current_prevents_duplicates(self):
|
| 55 |
_mushroom, current, history = discover_mushroom([])
|
| 56 |
collection, history = collect_current(current, [], history)
|
|
@@ -75,6 +185,8 @@ class MycoCoreTests(unittest.TestCase):
|
|
| 75 |
self.assertIn("MycoDex is empty", dex_markdown([]))
|
| 76 |
self.assertIn(current["name"], dex_markdown([current]))
|
| 77 |
self.assertIn("Forest Progress", progress_markdown([current]))
|
|
|
|
|
|
|
| 78 |
self.assertIn("Quest Log", game_status_markdown(current, [current]))
|
| 79 |
self.assertIn(current["name"], game_status_markdown(current, [current]))
|
| 80 |
self.assertEqual(current_area(0).name, "Mossy Trail")
|
|
@@ -103,12 +215,6 @@ class MycoCoreTests(unittest.TestCase):
|
|
| 103 |
self.assertEqual(source.count("def build_app"), 1)
|
| 104 |
self.assertNotIn("outputs=[", source)
|
| 105 |
self.assertNotIn("textbox_component", source)
|
| 106 |
-
|
| 107 |
-
def test_gradio_app_avoids_fragile_delimiters(self):
|
| 108 |
-
source = Path("ui/gradio_app.py").read_text()
|
| 109 |
-
self.assertNotIn("return (", source)
|
| 110 |
-
self.assertNotIn("[", source)
|
| 111 |
-
self.assertNotIn("]", source)
|
| 112 |
|
| 113 |
def test_gradio_app_rejects_old_corrupted_callback_fragments(self):
|
| 114 |
source = Path("ui/gradio_app.py").read_text()
|
|
@@ -124,22 +230,12 @@ class MycoCoreTests(unittest.TestCase):
|
|
| 124 |
)
|
| 125 |
for fragment in forbidden_fragments:
|
| 126 |
self.assertNotIn(fragment, source)
|
| 127 |
-
|
| 128 |
-
def
|
| 129 |
-
source = Path("
|
| 130 |
-
self.
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
source = Path("ui/gradio_app.py").read_text()
|
| 134 |
-
tree = ast.parse(source)
|
| 135 |
-
function_lines = {}
|
| 136 |
-
for node in ast.walk(tree):
|
| 137 |
-
if isinstance(node, ast.FunctionDef):
|
| 138 |
-
function_lines.setdefault(node.name, []).append(node.lineno)
|
| 139 |
-
|
| 140 |
-
duplicates = {name: lines for name, lines in function_lines.items() if len(lines) > 1}
|
| 141 |
-
self.assertEqual(duplicates, {})
|
| 142 |
-
self.assertNotIn("from __future__ import annotations", source)
|
| 143 |
|
| 144 |
def test_gradio_app_builds(self):
|
| 145 |
demo = build_app()
|
|
|
|
| 1 |
"""Regression tests for Myco's core game loop and UI builders."""
|
| 2 |
|
| 3 |
import ast
|
| 4 |
+
import os
|
| 5 |
import unittest
|
| 6 |
+
from types import SimpleNamespace
|
| 7 |
from pathlib import Path
|
| 8 |
+
from unittest.mock import patch
|
| 9 |
|
| 10 |
from game.catalog import load_mushrooms
|
| 11 |
from game.engine import (
|
| 12 |
+
DEFAULT_HF_MODEL_ID,
|
| 13 |
collect_current,
|
| 14 |
companion_reply,
|
| 15 |
discover_mushroom,
|
| 16 |
eat_current,
|
| 17 |
+
hf_companion_status,
|
| 18 |
+
hf_model_id,
|
| 19 |
study_current,
|
| 20 |
)
|
| 21 |
from game.progression import current_area, next_area
|
|
|
|
| 25 |
forest_scene,
|
| 26 |
game_status_markdown,
|
| 27 |
progress_markdown,
|
| 28 |
+
world_map_markdown,
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
+
def _fake_hf_response(calls, kwargs, content):
|
| 33 |
+
calls.append(kwargs)
|
| 34 |
+
message = SimpleNamespace(content=content)
|
| 35 |
+
choice = SimpleNamespace(message=message)
|
| 36 |
+
return SimpleNamespace(choices=(choice,))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
class MycoCoreTests(unittest.TestCase):
|
| 40 |
def test_catalog_has_expected_size_and_rarities(self):
|
| 41 |
mushrooms = load_mushrooms()
|
|
|
|
| 55 |
self.assertIn("forest-scene", scene)
|
| 56 |
self.assertIn(mushroom.name, scene)
|
| 57 |
|
| 58 |
+
|
| 59 |
+
def test_hf_default_model_is_llama_8b(self):
|
| 60 |
+
self.assertEqual(DEFAULT_HF_MODEL_ID, "meta-llama/Llama-3.1-8B-Instruct")
|
| 61 |
+
with patch.dict(os.environ, {"MYCO_HF_MODEL_ID": ""}, clear=False):
|
| 62 |
+
self.assertEqual(hf_model_id(), DEFAULT_HF_MODEL_ID)
|
| 63 |
+
with patch.dict(os.environ, {"MYCO_HF_MODEL_ID": "custom/model"}, clear=False):
|
| 64 |
+
self.assertEqual(hf_model_id(), "custom/model")
|
| 65 |
+
|
| 66 |
+
def test_hf_companion_status_explains_token_state(self):
|
| 67 |
+
no_token_env = {
|
| 68 |
+
"HF_TOKEN": "",
|
| 69 |
+
"HF_API_TOKEN": "",
|
| 70 |
+
"HF_BUILD_SMALL_HACKATHON_TOKEN": "",
|
| 71 |
+
}
|
| 72 |
+
with patch.dict(os.environ, no_token_env, clear=False):
|
| 73 |
+
self.assertIn("fallback voice is active", hf_companion_status())
|
| 74 |
+
|
| 75 |
+
with patch.dict(os.environ, {"HF_TOKEN": "fake-token"}, clear=False):
|
| 76 |
+
self.assertIn("connected to Hugging Face model", hf_companion_status())
|
| 77 |
+
|
| 78 |
+
def test_discovery_uses_hf_api_when_token_is_configured(self):
|
| 79 |
+
calls = []
|
| 80 |
+
|
| 81 |
+
def fake_client(token):
|
| 82 |
+
return SimpleNamespace(
|
| 83 |
+
token=token,
|
| 84 |
+
chat_completion=lambda **kwargs: _fake_hf_response(
|
| 85 |
+
calls,
|
| 86 |
+
kwargs,
|
| 87 |
+
"Llama Myco spotted a glowing cap.",
|
| 88 |
+
),
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
api_env = {
|
| 92 |
+
"HF_TOKEN": "fake-token",
|
| 93 |
+
"HF_API_TOKEN": "",
|
| 94 |
+
"HF_BUILD_SMALL_HACKATHON_TOKEN": "",
|
| 95 |
+
}
|
| 96 |
+
with patch.dict(os.environ, api_env, clear=False):
|
| 97 |
+
with patch("game.engine.InferenceClient", fake_client):
|
| 98 |
+
_mushroom, _current, history = discover_mushroom([])
|
| 99 |
+
|
| 100 |
+
self.assertEqual(history[-1]["content"], "Llama Myco spotted a glowing cap.")
|
| 101 |
+
self.assertEqual(calls[0]["model"], DEFAULT_HF_MODEL_ID)
|
| 102 |
+
|
| 103 |
def test_companion_reply_adds_user_and_assistant_messages(self):
|
| 104 |
_mushroom, current, history = discover_mushroom([])
|
| 105 |
+
no_token_env = {
|
| 106 |
+
"HF_TOKEN": "",
|
| 107 |
+
"HF_API_TOKEN": "",
|
| 108 |
+
"HF_BUILD_SMALL_HACKATHON_TOKEN": "",
|
| 109 |
+
}
|
| 110 |
+
with patch.dict(os.environ, no_token_env, clear=False):
|
| 111 |
+
prompt, updated_history = companion_reply("What is this?", history, current)
|
| 112 |
|
| 113 |
self.assertEqual(prompt, "")
|
| 114 |
self.assertEqual(updated_history[-2]["role"], "user")
|
| 115 |
self.assertEqual(updated_history[-1]["role"], "assistant")
|
| 116 |
self.assertIn(current["name"], updated_history[-1]["content"])
|
| 117 |
|
| 118 |
+
def test_companion_api_uses_hf_token_when_configured(self):
|
| 119 |
+
_mushroom, current, history = discover_mushroom([])
|
| 120 |
+
|
| 121 |
+
class FakeMessage:
|
| 122 |
+
content = "A model-powered Myco reply."
|
| 123 |
+
|
| 124 |
+
class FakeChoice:
|
| 125 |
+
message = FakeMessage()
|
| 126 |
+
|
| 127 |
+
class FakeResponse:
|
| 128 |
+
choices = (FakeChoice(),)
|
| 129 |
+
|
| 130 |
+
class FakeClient:
|
| 131 |
+
def __init__(self, token):
|
| 132 |
+
self.token = token
|
| 133 |
+
|
| 134 |
+
def chat_completion(self, **kwargs):
|
| 135 |
+
self.kwargs = kwargs
|
| 136 |
+
return FakeResponse()
|
| 137 |
+
|
| 138 |
+
api_env = {
|
| 139 |
+
"HF_TOKEN": "fake-token",
|
| 140 |
+
"HF_API_TOKEN": "",
|
| 141 |
+
"HF_BUILD_SMALL_HACKATHON_TOKEN": "",
|
| 142 |
+
}
|
| 143 |
+
with patch.dict(os.environ, api_env, clear=False):
|
| 144 |
+
with patch("game.engine.InferenceClient", FakeClient):
|
| 145 |
+
prompt, updated_history = companion_reply("Should I eat this?", history, current)
|
| 146 |
+
|
| 147 |
+
self.assertEqual(prompt, "")
|
| 148 |
+
self.assertEqual(updated_history[-1]["content"], "A model-powered Myco reply.")
|
| 149 |
+
|
| 150 |
+
def test_companion_api_falls_back_without_token(self):
|
| 151 |
+
_mushroom, current, history = discover_mushroom([])
|
| 152 |
+
no_token_env = {
|
| 153 |
+
"HF_TOKEN": "",
|
| 154 |
+
"HF_API_TOKEN": "",
|
| 155 |
+
"HF_BUILD_SMALL_HACKATHON_TOKEN": "",
|
| 156 |
+
}
|
| 157 |
+
with patch.dict(os.environ, no_token_env, clear=False):
|
| 158 |
+
prompt, updated_history = companion_reply("Should I eat this?", history, current)
|
| 159 |
+
|
| 160 |
+
self.assertEqual(prompt, "")
|
| 161 |
+
self.assertIn(current["name"], updated_history[-1]["content"])
|
| 162 |
+
self.assertIn("unknown", updated_history[-1]["content"].lower())
|
| 163 |
+
|
| 164 |
def test_collect_current_prevents_duplicates(self):
|
| 165 |
_mushroom, current, history = discover_mushroom([])
|
| 166 |
collection, history = collect_current(current, [], history)
|
|
|
|
| 185 |
self.assertIn("MycoDex is empty", dex_markdown([]))
|
| 186 |
self.assertIn(current["name"], dex_markdown([current]))
|
| 187 |
self.assertIn("Forest Progress", progress_markdown([current]))
|
| 188 |
+
self.assertIn("Forest Atlas", world_map_markdown([current]))
|
| 189 |
+
self.assertIn("First Find", world_map_markdown([current]))
|
| 190 |
self.assertIn("Quest Log", game_status_markdown(current, [current]))
|
| 191 |
self.assertIn(current["name"], game_status_markdown(current, [current]))
|
| 192 |
self.assertEqual(current_area(0).name, "Mossy Trail")
|
|
|
|
| 215 |
self.assertEqual(source.count("def build_app"), 1)
|
| 216 |
self.assertNotIn("outputs=[", source)
|
| 217 |
self.assertNotIn("textbox_component", source)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
def test_gradio_app_rejects_old_corrupted_callback_fragments(self):
|
| 220 |
source = Path("ui/gradio_app.py").read_text()
|
|
|
|
| 230 |
)
|
| 231 |
for fragment in forbidden_fragments:
|
| 232 |
self.assertNotIn(fragment, source)
|
| 233 |
+
|
| 234 |
+
def test_engine_has_single_myco_reply_implementation(self):
|
| 235 |
+
source = Path("game/engine.py").read_text()
|
| 236 |
+
self.assertEqual(source.count("def myco_reply"), 1)
|
| 237 |
+
self.assertNotIn("def companion_reply", source)
|
| 238 |
+
self.assertIn("companion_reply = myco_reply", source)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
def test_gradio_app_builds(self):
|
| 241 |
demo = build_app()
|
ui/gradio_app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
from game.engine import (
|
| 4 |
collect_current,
|
| 5 |
discover_mushroom,
|
| 6 |
eat_current,
|
|
|
|
| 7 |
myco_reply,
|
| 8 |
study_current,
|
| 9 |
)
|
|
@@ -16,6 +19,7 @@ from ui.renderers import (
|
|
| 16 |
hero_markdown,
|
| 17 |
mushroom_card,
|
| 18 |
progress_markdown,
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
EMPTY_COLLECTION = tuple()
|
|
@@ -48,57 +52,67 @@ def mushroom_card_from_state(current):
|
|
| 48 |
return mushroom_card(Mushroom.from_dict(current))
|
| 49 |
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
def search_forest(collection):
|
| 55 |
-
mushroom, current, history = discover_mushroom(collection)
|
| 56 |
-
return (
|
| 57 |
-
forest_scene(current, collection),
|
| 58 |
-
game_status_markdown(current, collection),
|
| 59 |
-
mushroom_card(mushroom),
|
| 60 |
-
current,
|
| 61 |
-
history,
|
| 62 |
-
dex_markdown(collection),
|
| 63 |
-
progress_markdown(collection),
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
def ask_companion(message, history, current):
|
| 67 |
-
return myco_reply(message, history, current)
|
| 68 |
-
|
| 69 |
-
def collect_discovery(current, collection, history):
|
| 70 |
-
updated_collection, updated_history = collect_current(current, collection, history)
|
| 71 |
-
return (
|
| 72 |
-
updated_collection,
|
| 73 |
-
forest_scene(current, updated_collection),
|
| 74 |
-
game_status_markdown(current, updated_collection),
|
| 75 |
-
dex_markdown(updated_collection),
|
| 76 |
-
updated_history,
|
| 77 |
-
progress_markdown(updated_collection),
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
def study_discovery(current, history, collection):
|
| 81 |
-
updated_current, updated_history = study_current(current, history)
|
| 82 |
-
return (
|
| 83 |
-
updated_current,
|
| 84 |
-
forest_scene(updated_current, collection),
|
| 85 |
-
game_status_markdown(updated_current, collection),
|
| 86 |
-
mushroom_card_from_state(updated_current),
|
| 87 |
-
updated_history,
|
| 88 |
-
)
|
| 89 |
-
|
| 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 |
|
| 99 |
with gr.Tabs():
|
| 100 |
with gr.Tab("🏠 Home"):
|
| 101 |
gr.Markdown(HOME_MARKDOWN)
|
|
|
|
| 102 |
|
| 103 |
with gr.Tab("🎮 Play"):
|
| 104 |
scene = gr.HTML(forest_scene(None, EMPTY_COLLECTION))
|
|
@@ -142,38 +156,41 @@ def build_app():
|
|
| 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=
|
| 151 |
-
outputs=
|
| 152 |
)
|
| 153 |
ask_button.click(
|
| 154 |
ask_companion,
|
| 155 |
-
inputs=
|
| 156 |
-
outputs=
|
| 157 |
)
|
| 158 |
prompt.submit(
|
| 159 |
ask_companion,
|
| 160 |
-
inputs=
|
| 161 |
-
outputs=
|
| 162 |
)
|
| 163 |
collect_button.click(
|
| 164 |
collect_discovery,
|
| 165 |
-
inputs=
|
| 166 |
-
outputs=
|
| 167 |
)
|
| 168 |
study_button.click(
|
| 169 |
study_discovery,
|
| 170 |
-
inputs=
|
| 171 |
-
outputs=
|
| 172 |
)
|
| 173 |
eat_button.click(
|
| 174 |
block_unsafe_eating,
|
| 175 |
-
inputs=
|
| 176 |
-
outputs=
|
| 177 |
)
|
| 178 |
|
| 179 |
return demo
|
|
|
|
| 1 |
+
"""Gradio app composition for Myco."""
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
from game.engine import (
|
| 6 |
collect_current,
|
| 7 |
discover_mushroom,
|
| 8 |
eat_current,
|
| 9 |
+
hf_companion_status,
|
| 10 |
myco_reply,
|
| 11 |
study_current,
|
| 12 |
)
|
|
|
|
| 19 |
hero_markdown,
|
| 20 |
mushroom_card,
|
| 21 |
progress_markdown,
|
| 22 |
+
world_map_markdown,
|
| 23 |
)
|
| 24 |
|
| 25 |
EMPTY_COLLECTION = tuple()
|
|
|
|
| 52 |
return mushroom_card(Mushroom.from_dict(current))
|
| 53 |
|
| 54 |
|
| 55 |
+
def search_forest(collection):
|
| 56 |
+
"""Explore the forest and refresh every visible gameplay panel."""
|
| 57 |
+
mushroom, current, history = discover_mushroom(collection)
|
| 58 |
+
return (
|
| 59 |
+
forest_scene(current, collection),
|
| 60 |
+
game_status_markdown(current, collection),
|
| 61 |
+
mushroom_card(mushroom),
|
| 62 |
+
current,
|
| 63 |
+
history,
|
| 64 |
+
dex_markdown(collection),
|
| 65 |
+
progress_markdown(collection),
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def ask_companion(message, history, current):
|
| 70 |
+
"""Ask Myco about the active discovery."""
|
| 71 |
+
return myco_reply(message, history, current)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def collect_discovery(current, collection, history):
|
| 75 |
+
"""Collect the active mushroom and refresh collection-dependent panels."""
|
| 76 |
+
updated_collection, updated_history = collect_current(current, collection, history)
|
| 77 |
+
return (
|
| 78 |
+
updated_collection,
|
| 79 |
+
forest_scene(current, updated_collection),
|
| 80 |
+
game_status_markdown(current, updated_collection),
|
| 81 |
+
dex_markdown(updated_collection),
|
| 82 |
+
world_map_markdown(updated_collection),
|
| 83 |
+
updated_history,
|
| 84 |
+
progress_markdown(updated_collection),
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def study_discovery(current, history, collection):
|
| 89 |
+
"""Study the active mushroom and refresh the discovery card."""
|
| 90 |
+
updated_current, updated_history = study_current(current, history)
|
| 91 |
+
return (
|
| 92 |
+
updated_current,
|
| 93 |
+
forest_scene(updated_current, collection),
|
| 94 |
+
game_status_markdown(updated_current, collection),
|
| 95 |
+
mushroom_card_from_state(updated_current),
|
| 96 |
+
updated_history,
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def block_unsafe_eating(current, history):
|
| 101 |
+
"""Let Myco prevent unsafe eating while preserving chat state."""
|
| 102 |
+
return eat_current(current, history)
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
def build_app():
|
| 106 |
+
"""Build the Gradio interface for Hugging Face Spaces."""
|
| 107 |
with gr.Blocks(title="Myco") as demo:
|
| 108 |
current_mushroom = gr.State(None)
|
| 109 |
collection = gr.State(EMPTY_COLLECTION)
|
|
|
|
| 110 |
gr.HTML(hero_markdown())
|
| 111 |
|
| 112 |
with gr.Tabs():
|
| 113 |
with gr.Tab("🏠 Home"):
|
| 114 |
gr.Markdown(HOME_MARKDOWN)
|
| 115 |
+
gr.Markdown(hf_companion_status(), elem_id="ai-status")
|
| 116 |
|
| 117 |
with gr.Tab("🎮 Play"):
|
| 118 |
scene = gr.HTML(forest_scene(None, EMPTY_COLLECTION))
|
|
|
|
| 156 |
elem_id="mycodex",
|
| 157 |
)
|
| 158 |
|
| 159 |
+
with gr.Tab("🗺️ World Map"):
|
| 160 |
+
world_map = gr.HTML(world_map_markdown(EMPTY_COLLECTION))
|
| 161 |
+
|
| 162 |
with gr.Tab("🧭 Guide"):
|
| 163 |
gr.Markdown(GUIDE_MARKDOWN)
|
| 164 |
|
| 165 |
search_button.click(
|
| 166 |
search_forest,
|
| 167 |
+
inputs=(collection,),
|
| 168 |
+
outputs=(scene, status, discovery, current_mushroom, chat, dex, progress),
|
| 169 |
)
|
| 170 |
ask_button.click(
|
| 171 |
ask_companion,
|
| 172 |
+
inputs=(prompt, chat, current_mushroom),
|
| 173 |
+
outputs=(prompt, chat),
|
| 174 |
)
|
| 175 |
prompt.submit(
|
| 176 |
ask_companion,
|
| 177 |
+
inputs=(prompt, chat, current_mushroom),
|
| 178 |
+
outputs=(prompt, chat),
|
| 179 |
)
|
| 180 |
collect_button.click(
|
| 181 |
collect_discovery,
|
| 182 |
+
inputs=(current_mushroom, collection, chat),
|
| 183 |
+
outputs=(collection, scene, status, dex, world_map, chat, progress),
|
| 184 |
)
|
| 185 |
study_button.click(
|
| 186 |
study_discovery,
|
| 187 |
+
inputs=(current_mushroom, chat, collection),
|
| 188 |
+
outputs=(current_mushroom, scene, status, discovery, chat),
|
| 189 |
)
|
| 190 |
eat_button.click(
|
| 191 |
block_unsafe_eating,
|
| 192 |
+
inputs=(current_mushroom, chat),
|
| 193 |
+
outputs=(chat,),
|
| 194 |
)
|
| 195 |
|
| 196 |
return demo
|
ui/renderers.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
"""Markdown renderers for the Myco Gradio interface."""
|
| 2 |
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
from html import escape
|
| 6 |
|
| 7 |
from game.progression import AREA_UNLOCKS, current_area, next_area, unlocked_areas
|
|
@@ -12,15 +10,15 @@ from models.mushroom import Mushroom
|
|
| 12 |
def hero_markdown() -> str:
|
| 13 |
"""Render a polished first-impression hero panel."""
|
| 14 |
return """
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
|
| 26 |
def forest_scene(current: MushroomState | None, collection: CollectionState | None) -> str:
|
|
@@ -112,6 +110,57 @@ def mushroom_card(mushroom: Mushroom | None) -> str:
|
|
| 112 |
)
|
| 113 |
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def dex_markdown(collection: CollectionState | None) -> str:
|
| 116 |
"""Render collected mushrooms as a compact MycoDex."""
|
| 117 |
current_collection = _safe_collection(collection)
|
|
@@ -126,6 +175,11 @@ def dex_markdown(collection: CollectionState | None) -> str:
|
|
| 126 |
return "\n".join(rows)
|
| 127 |
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
def progress_markdown(collection: CollectionState | None) -> str:
|
| 130 |
"""Render collection goals and area unlock progress."""
|
| 131 |
count = len(_safe_collection(collection))
|
|
@@ -147,9 +201,3 @@ def progress_markdown(collection: CollectionState | None) -> str:
|
|
| 147 |
rows.append(f"**All goals visible:** You have reached **{AREA_UNLOCKS[-1].name}**.")
|
| 148 |
|
| 149 |
return "\n\n".join(rows)
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def _safe_collection(collection: CollectionState | None) -> CollectionState:
|
| 153 |
-
"""Return a list-like collection even before Gradio initializes state."""
|
| 154 |
-
return list(collection or [])
|
| 155 |
-
|
|
|
|
| 1 |
"""Markdown renderers for the Myco Gradio interface."""
|
| 2 |
|
|
|
|
|
|
|
| 3 |
from html import escape
|
| 4 |
|
| 5 |
from game.progression import AREA_UNLOCKS, current_area, next_area, unlocked_areas
|
|
|
|
| 10 |
def hero_markdown() -> str:
|
| 11 |
"""Render a polished first-impression hero panel."""
|
| 12 |
return """
|
| 13 |
+
<div class="myco-hero">
|
| 14 |
+
<div class="myco-mascot">🍄</div>
|
| 15 |
+
<div>
|
| 16 |
+
<p class="eyebrow">Cozy AI companion game</p>
|
| 17 |
+
<h1>Myco</h1>
|
| 18 |
+
<p>Explore a magical forest with Myco, discover strange fungi, and complete your MycoDex.</p>
|
| 19 |
+
</div>
|
| 20 |
+
</div>
|
| 21 |
+
"""
|
| 22 |
|
| 23 |
|
| 24 |
def forest_scene(current: MushroomState | None, collection: CollectionState | None) -> str:
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def world_map_markdown(collection: CollectionState | None) -> str:
|
| 116 |
+
"""Render a game-like world map with unlocks and achievements."""
|
| 117 |
+
count = len(_safe_collection(collection))
|
| 118 |
+
cards = []
|
| 119 |
+
for area in AREA_UNLOCKS:
|
| 120 |
+
unlocked = count >= area.required_discoveries
|
| 121 |
+
state = "unlocked" if unlocked else "locked"
|
| 122 |
+
badge = "✅ Unlocked" if unlocked else f"🔒 {area.required_discoveries - count} discoveries away"
|
| 123 |
+
cards.append(
|
| 124 |
+
"\n".join(
|
| 125 |
+
(
|
| 126 |
+
f'<div class="map-card {state}">',
|
| 127 |
+
' <div class="map-card-top">',
|
| 128 |
+
f' <strong>{escape(area.name)}</strong>',
|
| 129 |
+
f' <span>{badge}</span>',
|
| 130 |
+
' </div>',
|
| 131 |
+
f' <p>{escape(area.description)}</p>',
|
| 132 |
+
'</div>',
|
| 133 |
+
)
|
| 134 |
+
)
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
achievements = (
|
| 138 |
+
_achievement_badge("First Find", count >= 1),
|
| 139 |
+
_achievement_badge("Rare Scout", count >= 10),
|
| 140 |
+
_achievement_badge("Moonlit Keeper", count >= 25),
|
| 141 |
+
_achievement_badge("Elder Friend", count >= 50),
|
| 142 |
+
)
|
| 143 |
+
return "\n".join(
|
| 144 |
+
(
|
| 145 |
+
'<div class="world-map-panel">',
|
| 146 |
+
' <div class="world-map-header">',
|
| 147 |
+
' <p class="eyebrow">World Map</p>',
|
| 148 |
+
' <h2>Forest Atlas</h2>',
|
| 149 |
+
f' <p>{count} discoveries logged. Unlock new areas by expanding the MycoDex.</p>',
|
| 150 |
+
' </div>',
|
| 151 |
+
f' <div class="achievement-row">{"".join(achievements)}</div>',
|
| 152 |
+
f' <div class="map-grid">{"".join(cards)}</div>',
|
| 153 |
+
'</div>',
|
| 154 |
+
)
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def _achievement_badge(label: str, unlocked: bool) -> str:
|
| 159 |
+
"""Render a compact achievement badge."""
|
| 160 |
+
state = "earned" if unlocked else "pending"
|
| 161 |
+
icon = "🏅" if unlocked else "◇"
|
| 162 |
+
return f'<span class="achievement {state}">{icon} {escape(label)}</span>'
|
| 163 |
+
|
| 164 |
def dex_markdown(collection: CollectionState | None) -> str:
|
| 165 |
"""Render collected mushrooms as a compact MycoDex."""
|
| 166 |
current_collection = _safe_collection(collection)
|
|
|
|
| 175 |
return "\n".join(rows)
|
| 176 |
|
| 177 |
|
| 178 |
+
def _safe_collection(collection: CollectionState | None) -> CollectionState:
|
| 179 |
+
"""Return a list-like collection even before Gradio initializes state."""
|
| 180 |
+
return list(collection or [])
|
| 181 |
+
|
| 182 |
+
|
| 183 |
def progress_markdown(collection: CollectionState | None) -> str:
|
| 184 |
"""Render collection goals and area unlock progress."""
|
| 185 |
count = len(_safe_collection(collection))
|
|
|
|
| 201 |
rows.append(f"**All goals visible:** You have reached **{AREA_UNLOCKS[-1].name}**.")
|
| 202 |
|
| 203 |
return "\n\n".join(rows)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ui/styles.py
CHANGED
|
@@ -156,11 +156,6 @@ APP_CSS = """
|
|
| 156 |
#mycodex,
|
| 157 |
#progress-panel,
|
| 158 |
#quest-log {
|
| 159 |
-
#progress-panel {
|
| 160 |
-
#discovery-card,
|
| 161 |
-
#mycodex,
|
| 162 |
-
#progress-panel {
|
| 163 |
-
#discovery-card, #mycodex, #progress-panel {
|
| 164 |
padding: 1rem;
|
| 165 |
border-radius: 20px;
|
| 166 |
background: rgba(255, 250, 235, 0.9);
|
|
@@ -171,6 +166,82 @@ button.primary,
|
|
| 171 |
button[variant="primary"] {
|
| 172 |
font-weight: 800;
|
| 173 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
@media (max-width: 720px) {
|
| 175 |
.myco-hero {
|
| 176 |
align-items: flex-start;
|
|
|
|
| 156 |
#mycodex,
|
| 157 |
#progress-panel,
|
| 158 |
#quest-log {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
padding: 1rem;
|
| 160 |
border-radius: 20px;
|
| 161 |
background: rgba(255, 250, 235, 0.9);
|
|
|
|
| 166 |
button[variant="primary"] {
|
| 167 |
font-weight: 800;
|
| 168 |
}
|
| 169 |
+
.world-map-panel {
|
| 170 |
+
border-radius: 28px;
|
| 171 |
+
padding: 1.25rem;
|
| 172 |
+
background: linear-gradient(135deg, rgba(21, 67, 49, 0.96), rgba(44, 94, 72, 0.92));
|
| 173 |
+
color: #f7ffe8;
|
| 174 |
+
box-shadow: 0 22px 60px rgba(14, 38, 29, 0.22);
|
| 175 |
+
}
|
| 176 |
+
.world-map-header h2 {
|
| 177 |
+
margin: 0.1rem 0 0.35rem;
|
| 178 |
+
font-size: 2rem;
|
| 179 |
+
}
|
| 180 |
+
.world-map-header p {
|
| 181 |
+
margin: 0.2rem 0;
|
| 182 |
+
}
|
| 183 |
+
.achievement-row {
|
| 184 |
+
display: flex;
|
| 185 |
+
flex-wrap: wrap;
|
| 186 |
+
gap: 0.65rem;
|
| 187 |
+
margin: 1rem 0;
|
| 188 |
+
}
|
| 189 |
+
.achievement {
|
| 190 |
+
border-radius: 999px;
|
| 191 |
+
padding: 0.45rem 0.75rem;
|
| 192 |
+
font-size: 0.9rem;
|
| 193 |
+
font-weight: 800;
|
| 194 |
+
}
|
| 195 |
+
.achievement.earned {
|
| 196 |
+
background: rgba(255, 224, 138, 0.95);
|
| 197 |
+
color: #3d2c00;
|
| 198 |
+
}
|
| 199 |
+
.achievement.pending {
|
| 200 |
+
background: rgba(255, 255, 255, 0.12);
|
| 201 |
+
color: #d4efcf;
|
| 202 |
+
}
|
| 203 |
+
.map-grid {
|
| 204 |
+
display: grid;
|
| 205 |
+
gap: 0.9rem;
|
| 206 |
+
grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
|
| 207 |
+
}
|
| 208 |
+
.map-card {
|
| 209 |
+
border-radius: 22px;
|
| 210 |
+
padding: 1rem;
|
| 211 |
+
border: 1px solid rgba(255, 255, 255, 0.15);
|
| 212 |
+
background: rgba(255, 255, 255, 0.1);
|
| 213 |
+
}
|
| 214 |
+
.map-card.unlocked {
|
| 215 |
+
background: rgba(236, 255, 217, 0.18);
|
| 216 |
+
border-color: rgba(214, 255, 177, 0.4);
|
| 217 |
+
}
|
| 218 |
+
.map-card.locked {
|
| 219 |
+
filter: saturate(0.75);
|
| 220 |
+
opacity: 0.78;
|
| 221 |
+
}
|
| 222 |
+
.map-card-top {
|
| 223 |
+
display: flex;
|
| 224 |
+
justify-content: space-between;
|
| 225 |
+
gap: 0.75rem;
|
| 226 |
+
align-items: center;
|
| 227 |
+
}
|
| 228 |
+
.map-card-top span {
|
| 229 |
+
font-size: 0.82rem;
|
| 230 |
+
font-weight: 800;
|
| 231 |
+
}
|
| 232 |
+
.map-card p {
|
| 233 |
+
margin-bottom: 0;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
#ai-status {
|
| 237 |
+
border: 1px solid rgba(74, 222, 128, 0.28);
|
| 238 |
+
border-radius: 18px;
|
| 239 |
+
background: rgba(236, 253, 245, 0.72);
|
| 240 |
+
color: #17402f;
|
| 241 |
+
font-weight: 800;
|
| 242 |
+
padding: 0.65rem 0.9rem;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
@media (max-width: 720px) {
|
| 246 |
.myco-hero {
|
| 247 |
align-items: flex-start;
|