File size: 16,555 Bytes
7cd5e9d 277e6df 7cd5e9d 277e6df 7cd5e9d 277e6df fa60426 277e6df 7cd5e9d 13bc6fd 553b155 7cd5e9d 277e6df 553b155 277e6df 2b6b8bb 277e6df 2b6b8bb 1f637de 277e6df 7cd5e9d 1eea13b 7cd5e9d 1eea13b 7cd5e9d 1eea13b 7cd5e9d 1eea13b 7cd5e9d 1eea13b 7cd5e9d 277e6df 7cd5e9d 1e98e8c 7cd5e9d 1e98e8c 7cd5e9d 1e98e8c 7cd5e9d 1e98e8c 7cd5e9d 1f637de 7cd5e9d 277e6df 1f637de 7cd5e9d 1f637de 7cd5e9d 277e6df 1f637de 7cd5e9d 1f637de 7cd5e9d 1e98e8c 7cd5e9d 1e98e8c 7cd5e9d 1e98e8c 7cd5e9d 1e98e8c 7cd5e9d 277e6df 7cd5e9d 277e6df 7cd5e9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | """AI model engine for NPCverse.
NPCverse transforms uploaded photos into living RPG characters using
MiniCPM-V 4.6 on Hugging Face ZeroGPU.
"""
from __future__ import annotations
import json
import re
from typing import Any
import spaces
import torch
from transformers import AutoModelForImageTextToText, AutoProcessor
from PIL import Image
MODEL_ID = "openbmb/MiniCPM-V-4.6"
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForImageTextToText.from_pretrained(
MODEL_ID,
trust_remote_code=True,
dtype="auto",
).eval()
FRIENDSHIP_THRESHOLDS = [10, 20, 35, 55]
SECRET_THRESHOLDS = [8, 18, 35]
DEFAULT_NPC: dict[str, Any] = {
"name": "Nyx Vale",
"title": "Wanderer of the Digital Realm",
"class": "Reality Glitch Rogue",
"level": 7,
"rarity": "Rare",
"alignment": "Chaotic Good",
"lore": (
"A strange traveler assembled from scattered memories, half rumor and "
"half starlight, who appears wherever forgotten stories need a champion."
),
"stats": {
"strength": 42,
"intelligence": 76,
"charisma": 68,
"luck": 81,
"stealth": 73,
"chaos": 64,
},
"passive_ability": {
"name": "Signal Echo",
"description": "Reads emotional static in the air to sense hidden motives.",
},
"ultimate": {
"name": "Myth Rewrite",
"description": "Briefly bends the scene into a heroic legend where one impossible action can succeed.",
},
"weakness": "Becomes uncertain when memories conflict with the present moment.",
"faction": "The Patchwork Covenant",
"world": "The Neon Wilds",
"opening_line": "You found me between one heartbeat and the next. That usually means trouble.",
"quests": [
{
"title": "Trace the Lost Signal",
"description": "Follow a broken transmission through the alleys of a city that dreams.",
"reward": "Echo Compass",
"rarity": "Uncommon",
},
{
"title": "Steal Back the Moon Key",
"description": "Recover a silver key from a guild of masked probability thieves.",
"reward": "Moonlit Lockpick",
"rarity": "Rare",
},
{
"title": "Defend the Last Save Point",
"description": "Hold the line while ancient code repairs a collapsing sanctuary.",
"reward": "Legendary Bond Fragment",
"rarity": "Epic",
},
],
"secrets": [
"Nyx remembers fragments of every player who has ever abandoned a quest.",
"Their shadow sometimes moves a few seconds before they do.",
"The Patchwork Covenant may have created Nyx as a living apology.",
],
"emoji": "✨",
}
REQUIRED_NPC_KEYS = {
"name",
"title",
"class",
"level",
"rarity",
"alignment",
"lore",
"stats",
"passive_ability",
"ultimate",
"weakness",
"faction",
"world",
"opening_line",
"quests",
"secrets",
"emoji",
}
REQUIRED_STAT_KEYS = {
"strength",
"intelligence",
"charisma",
"luck",
"stealth",
"chaos",
}
def _generate_from_messages(
messages: list[dict[str, Any]],
max_new_tokens: int = 512,
downsample_mode: str = "16x",
) -> str:
"""Generate text with MiniCPM-V-4.6 from chat-template messages."""
device = "cuda" if torch.cuda.is_available() else "cpu"
if model.device.type != device:
model.to(device)
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
processor_kwargs={
"downsample_mode": downsample_mode,
"max_slice_nums": 36,
},
).to(model.device)
generated_ids = model.generate(
**inputs,
downsample_mode=downsample_mode,
max_new_tokens=max_new_tokens,
pad_token_id=processor.tokenizer.eos_token_id,
do_sample=True,
temperature=0.8,
top_p=0.9,
)
generated_ids_trimmed = [
output_ids[len(input_ids):]
for input_ids, output_ids in zip(inputs["input_ids"], generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)
return str(output_text[0]).strip() if output_text else ""
def parse_json_safe(text: str) -> dict:
"""Parse JSON after removing common markdown fences and wrapper text."""
cleaned = re.sub(r"^\s*```(?:json)?\s*", "", text.strip(), flags=re.IGNORECASE)
cleaned = re.sub(r"\s*```\s*$", "", cleaned).strip()
try:
parsed = json.loads(cleaned)
except json.JSONDecodeError:
match = re.search(r"\{.*\}", cleaned, flags=re.DOTALL)
if match is None:
raise
parsed = json.loads(match.group(0))
if not isinstance(parsed, dict):
raise ValueError("Expected a JSON object.")
return parsed
def _validate_npc_payload(payload: dict) -> dict:
"""Validate the NPC payload shape, filling in missing keys from DEFAULT_NPC."""
# Fill any missing top-level keys with defaults so a partial response still works
for key in REQUIRED_NPC_KEYS:
if key not in payload:
print(f"[NPCverse] NPC payload missing key '{key}', using default.")
payload[key] = DEFAULT_NPC[key]
stats = payload.get("stats")
if not isinstance(stats, dict):
print("[NPCverse] NPC stats invalid, using defaults.")
payload["stats"] = dict(DEFAULT_NPC["stats"])
stats = payload["stats"]
# Fill any missing stat keys with defaults
for key in REQUIRED_STAT_KEYS:
if key not in stats:
print(f"[NPCverse] NPC stat '{key}' missing, using default.")
stats[key] = DEFAULT_NPC["stats"][key]
payload["level"] = int(payload.get("level") or DEFAULT_NPC["level"])
for key in REQUIRED_STAT_KEYS:
stats[key] = max(1, min(100, int(stats[key])))
# Ensure quests is a list of dicts
if not isinstance(payload.get("quests"), list) or len(payload["quests"]) == 0:
payload["quests"] = DEFAULT_NPC["quests"]
# Ensure secrets is a list of strings
if not isinstance(payload.get("secrets"), list) or len(payload["secrets"]) == 0:
payload["secrets"] = DEFAULT_NPC["secrets"]
return payload
def get_friendship_label(msg_count: int) -> str:
"""Return the friendship label for the current message count."""
if msg_count >= FRIENDSHIP_THRESHOLDS[3]:
return "Legendary Bond"
if msg_count >= FRIENDSHIP_THRESHOLDS[2]:
return "Trusted Ally"
if msg_count >= FRIENDSHIP_THRESHOLDS[1]:
return "Friend"
if msg_count >= FRIENDSHIP_THRESHOLDS[0]:
return "Acquaintance"
return "Stranger"
def check_new_secrets(msg_count: int, already_unlocked: list) -> list[int]:
"""Return newly unlocked secret indices for the current message count."""
unlocked = {int(index) for index in already_unlocked if str(index).isdigit()}
return [
index
for index, threshold in enumerate(SECRET_THRESHOLDS)
if msg_count >= threshold and index not in unlocked
]
@spaces.GPU
def analyze_image(image_path: str) -> str:
"""Describe an uploaded image as factual character source material."""
prompt_text = (
"Describe this person's appearance in detail. Include: approximate age and gender, "
"clothing style and colors, facial expression and mood, hair style and color, "
"accessories (glasses, jewelry, etc.), body language and pose, background environment. "
"Be specific and factual. Under 120 words."
)
try:
with Image.open(image_path) as image_obj:
image_obj = image_obj.convert("RGB")
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image_obj},
{"type": "text", "text": prompt_text},
],
}
]
return _generate_from_messages(messages, max_new_tokens=180)
except Exception:
return "A mysterious figure in the digital realm."
def _npc_generation_prompt(description: str) -> str:
"""Build the primary JSON-only NPC generation prompt."""
return f"""You are the NPCverse character engine. Transform the visual description below into a vivid RPG character.
Return ONLY a valid JSON object. No backticks, no markdown, no extra text before or after.
You MUST include every single key shown in the template below. Do not skip any key.
JSON template (replace all placeholder values):
{{
"name": "<unique fantasy name inspired by appearance>",
"title": "<short evocative title, e.g. 'Wanderer of the Neon Wilds'>",
"class": "<creative RPG class name>",
"level": <integer 1-20>,
"rarity": "<one of: Common, Uncommon, Rare, Epic, Legendary>",
"alignment": "<e.g. Chaotic Good, Lawful Neutral, True Neutral>",
"lore": "<2-3 sentence backstory inspired by their appearance>",
"stats": {{
"strength": <1-100>,
"intelligence": <1-100>,
"charisma": <1-100>,
"luck": <1-100>,
"stealth": <1-100>,
"chaos": <1-100>
}},
"passive_ability": {{
"name": "<passive skill name>",
"description": "<what it does>"
}},
"ultimate": {{
"name": "<ultimate skill name>",
"description": "<what it does>"
}},
"weakness": "<one sentence describing their weakness or flaw>",
"faction": "<name of the group or faction they belong to>",
"world": "<name of the realm or world they come from>",
"opening_line": "<first thing they say when summoned, in character, 1-2 sentences>",
"quests": [
{{"title": "<quest 1 title>", "description": "<quest 1 desc>", "reward": "<item name>", "rarity": "Common"}},
{{"title": "<quest 2 title>", "description": "<quest 2 desc>", "reward": "<item name>", "rarity": "Rare"}},
{{"title": "<quest 3 title>", "description": "<quest 3 desc>", "reward": "<item name>", "rarity": "Epic"}}
],
"secrets": [
"<secret 1 about this character>",
"<secret 2 about this character>",
"<secret 3 about this character>"
],
"emoji": "<single emoji that represents this character>"
}}
Visual description to transform:
{description}""".strip()
def _npc_retry_prompt(description: str) -> str:
"""Build a shorter strict prompt for retrying malformed JSON."""
return f"""Output ONLY a valid JSON object. No markdown. No extra text.
Create an RPG character from this description: {description}
Required JSON — include ALL these keys:
{{"name":"","title":"","class":"","level":1,"rarity":"Common","alignment":"","lore":"",
"stats":{{"strength":50,"intelligence":50,"charisma":50,"luck":50,"stealth":50,"chaos":50}},
"passive_ability":{{"name":"","description":""}},
"ultimate":{{"name":"","description":""}},
"weakness":"","faction":"","world":"","opening_line":"",
"quests":[{{"title":"","description":"","reward":"","rarity":"Common"}},{{"title":"","description":"","reward":"","rarity":"Rare"}},{{"title":"","description":"","reward":"","rarity":"Epic"}}],
"secrets":["","",""],"emoji":"⚔"}}
Fill in all empty string values with creative content based on the description.""".strip()
@spaces.GPU
def generate_npc(description: str) -> dict:
"""Generate a complete RPG NPC JSON object from a visual description."""
import traceback
try:
msgs = [{'role': 'user', 'content': [{"type": "text", "text": _npc_generation_prompt(description)}]}]
result = _generate_from_messages(msgs, max_new_tokens=1200)
return _validate_npc_payload(parse_json_safe(str(result)))
except Exception as e:
print(f"[NPCverse] generate_npc first attempt failed: {e}")
traceback.print_exc()
try:
retry_msgs = [{'role': 'user', 'content': [{"type": "text", "text": _npc_retry_prompt(description)}]}]
retry_result = _generate_from_messages(retry_msgs, max_new_tokens=1200)
return _validate_npc_payload(parse_json_safe(str(retry_result)))
except Exception as e2:
print(f"[NPCverse] generate_npc retry also failed: {e2}")
traceback.print_exc()
return DEFAULT_NPC
def _stats_summary(npc: dict) -> str:
"""Format NPC stats for the roleplay prompt."""
stats = npc.get("stats", {})
return ", ".join(
f"{key}: {stats.get(key, DEFAULT_NPC['stats'][key])}"
for key in ["strength", "intelligence", "charisma", "luck", "stealth", "chaos"]
)
def _format_unlocked_secrets(npc: dict, unlocked_secrets: list) -> str:
"""Format unlocked secret indices and text for the roleplay prompt."""
secrets = npc.get("secrets", [])
lines = []
for index in unlocked_secrets:
try:
secret_index = int(index)
secret_text = secrets[secret_index]
except (TypeError, ValueError, IndexError):
continue
lines.append(f"{secret_index}: {secret_text}")
return "\n".join(lines) if lines else "None"
def _normalize_history(history: list) -> list[dict[str, str]]:
"""Convert common Gradio chat history formats into MiniCPM messages."""
normalized: list[dict[str, str]] = []
for exchange in history[-20:]: # keep last 20 turns for richer memory
if isinstance(exchange, dict):
role = exchange.get("role")
content = exchange.get("content")
if role in {"user", "assistant"} and content:
normalized.append({"role": role, "content": str(content)})
continue
if isinstance(exchange, (list, tuple)) and len(exchange) >= 2:
user_turn, assistant_turn = exchange[0], exchange[1]
if user_turn:
normalized.append({"role": "user", "content": str(user_turn)})
if assistant_turn:
normalized.append({"role": "assistant", "content": str(assistant_turn)})
return normalized
@spaces.GPU
def chat_respond(
npc: dict,
history: list,
user_message: str,
msg_count: int,
unlocked_secrets: list,
) -> str:
"""Generate an in-character NPC chat response."""
npc_name = str(npc.get("name", DEFAULT_NPC["name"]))
try:
friendship_label = get_friendship_label(msg_count)
passive = npc.get("passive_ability", DEFAULT_NPC["passive_ability"])
system_prompt = f"""You are {npc_name}, an NPC in NPCverse. ALWAYS stay in character.
Name: {npc_name}
Class: {npc.get("class", DEFAULT_NPC["class"])}
World: {npc.get("world", DEFAULT_NPC["world"])}
Alignment: {npc.get("alignment", DEFAULT_NPC["alignment"])}
Stats summary: {_stats_summary(npc)}
Passive ability: {passive.get("name", DEFAULT_NPC["passive_ability"]["name"])} - {passive.get("description", DEFAULT_NPC["passive_ability"]["description"])}
Weakness: {npc.get("weakness", DEFAULT_NPC["weakness"])}
Current friendship level: {friendship_label}
Unlocked secrets by index:
{_format_unlocked_secrets(npc, unlocked_secrets)}
IMPORTANT memory rules:
- You have access to the full conversation history below. Read it carefully.
- Remember everything the user has told you (their name, choices, quests, preferences).
- Reference past exchanges naturally — if they told you their name, use it; if they completed a quest, acknowledge it.
- Never contradict something you said earlier in this conversation.
- Stay consistent with your established personality throughout.
Respond naturally as this character. Keep replies concise, flavorful, and interactive.
Never say you are an AI model or break character.""".strip()
msgs = [
{"role": "user", "content": [{"type": "text", "text": system_prompt}]},
{"role": "assistant", "content": [{"type": "text", "text": "Understood. I will remain fully in character."}]},
]
for turn in _normalize_history(history):
msgs.append(
{
"role": turn["role"],
"content": [{"type": "text", "text": turn["content"]}],
}
)
msgs.append({"role": "user", "content": [{"type": "text", "text": user_message}]})
result = _generate_from_messages(msgs, max_new_tokens=320)
return str(result).strip()
except Exception:
return f"*{npc_name} seems momentarily absent...*"
|