LazyHuman10 commited on
Commit ·
1f637de
1
Parent(s): 73c0075
fix: add sampling to model generation + surface errors in logs
Browse files- app.py +7 -1
- model_engine.py +12 -4
app.py
CHANGED
|
@@ -47,12 +47,18 @@ async def health() -> JSONResponse:
|
|
| 47 |
@app.api(name="summon_npc")
|
| 48 |
def summon_npc(image_path: Any) -> dict:
|
| 49 |
"""Analyze an uploaded image and summon a complete NPC profile."""
|
|
|
|
| 50 |
try:
|
| 51 |
path = _extract_file_path(image_path)
|
|
|
|
| 52 |
description = analyze_image(path)
|
|
|
|
| 53 |
npc = generate_npc(description)
|
|
|
|
| 54 |
return _json_safe_dict(npc)
|
| 55 |
-
except Exception:
|
|
|
|
|
|
|
| 56 |
return _json_safe_dict(DEFAULT_NPC)
|
| 57 |
|
| 58 |
|
|
|
|
| 47 |
@app.api(name="summon_npc")
|
| 48 |
def summon_npc(image_path: Any) -> dict:
|
| 49 |
"""Analyze an uploaded image and summon a complete NPC profile."""
|
| 50 |
+
import traceback
|
| 51 |
try:
|
| 52 |
path = _extract_file_path(image_path)
|
| 53 |
+
print(f"[NPCverse] summon_npc: extracted path = {path!r}")
|
| 54 |
description = analyze_image(path)
|
| 55 |
+
print(f"[NPCverse] summon_npc: image description = {description!r}")
|
| 56 |
npc = generate_npc(description)
|
| 57 |
+
print(f"[NPCverse] summon_npc: generated NPC name = {npc.get('name')!r}")
|
| 58 |
return _json_safe_dict(npc)
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"[NPCverse] summon_npc FAILED: {e}")
|
| 61 |
+
traceback.print_exc()
|
| 62 |
return _json_safe_dict(DEFAULT_NPC)
|
| 63 |
|
| 64 |
|
model_engine.py
CHANGED
|
@@ -142,6 +142,9 @@ def _generate_from_messages(
|
|
| 142 |
downsample_mode=downsample_mode,
|
| 143 |
max_new_tokens=max_new_tokens,
|
| 144 |
pad_token_id=processor.tokenizer.eos_token_id,
|
|
|
|
|
|
|
|
|
|
| 145 |
)
|
| 146 |
generated_ids_trimmed = [
|
| 147 |
output_ids[len(input_ids):]
|
|
@@ -294,16 +297,21 @@ No markdown. No extra text.
|
|
| 294 |
@spaces.GPU
|
| 295 |
def generate_npc(description: str) -> dict:
|
| 296 |
"""Generate a complete RPG NPC JSON object from a visual description."""
|
|
|
|
| 297 |
try:
|
| 298 |
msgs = [{'role': 'user', 'content': [{"type": "text", "text": _npc_generation_prompt(description)}]}]
|
| 299 |
-
result = _generate_from_messages(msgs, max_new_tokens=
|
| 300 |
return _validate_npc_payload(parse_json_safe(str(result)))
|
| 301 |
-
except Exception:
|
|
|
|
|
|
|
| 302 |
try:
|
| 303 |
retry_msgs = [{'role': 'user', 'content': [{"type": "text", "text": _npc_retry_prompt(description)}]}]
|
| 304 |
-
retry_result = _generate_from_messages(retry_msgs, max_new_tokens=
|
| 305 |
return _validate_npc_payload(parse_json_safe(str(retry_result)))
|
| 306 |
-
except Exception:
|
|
|
|
|
|
|
| 307 |
return DEFAULT_NPC
|
| 308 |
|
| 309 |
|
|
|
|
| 142 |
downsample_mode=downsample_mode,
|
| 143 |
max_new_tokens=max_new_tokens,
|
| 144 |
pad_token_id=processor.tokenizer.eos_token_id,
|
| 145 |
+
do_sample=True,
|
| 146 |
+
temperature=0.8,
|
| 147 |
+
top_p=0.9,
|
| 148 |
)
|
| 149 |
generated_ids_trimmed = [
|
| 150 |
output_ids[len(input_ids):]
|
|
|
|
| 297 |
@spaces.GPU
|
| 298 |
def generate_npc(description: str) -> dict:
|
| 299 |
"""Generate a complete RPG NPC JSON object from a visual description."""
|
| 300 |
+
import traceback
|
| 301 |
try:
|
| 302 |
msgs = [{'role': 'user', 'content': [{"type": "text", "text": _npc_generation_prompt(description)}]}]
|
| 303 |
+
result = _generate_from_messages(msgs, max_new_tokens=1200)
|
| 304 |
return _validate_npc_payload(parse_json_safe(str(result)))
|
| 305 |
+
except Exception as e:
|
| 306 |
+
print(f"[NPCverse] generate_npc first attempt failed: {e}")
|
| 307 |
+
traceback.print_exc()
|
| 308 |
try:
|
| 309 |
retry_msgs = [{'role': 'user', 'content': [{"type": "text", "text": _npc_retry_prompt(description)}]}]
|
| 310 |
+
retry_result = _generate_from_messages(retry_msgs, max_new_tokens=1200)
|
| 311 |
return _validate_npc_payload(parse_json_safe(str(retry_result)))
|
| 312 |
+
except Exception as e2:
|
| 313 |
+
print(f"[NPCverse] generate_npc retry also failed: {e2}")
|
| 314 |
+
traceback.print_exc()
|
| 315 |
return DEFAULT_NPC
|
| 316 |
|
| 317 |
|