Spaces:
Paused
Paused
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -474,9 +474,16 @@ if ON_HF:
|
|
| 474 |
@spaces.GPU(duration=30)
|
| 475 |
def _hf_gpu_generate(messages, max_new_tokens=150):
|
| 476 |
model = _hf_model.to("cuda")
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
inputs = _hf_tok(text, return_tensors="pt").to("cuda")
|
| 481 |
with torch.no_grad():
|
| 482 |
out = model.generate(
|
|
@@ -1200,31 +1207,41 @@ How do I feel right now?"""
|
|
| 1200 |
response = self.call_llm(prompt, system_prompt=system)
|
| 1201 |
|
| 1202 |
if response:
|
|
|
|
|
|
|
|
|
|
| 1203 |
response = response.encode('ascii', errors='ignore').decode('ascii').strip()
|
| 1204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1205 |
try:
|
| 1206 |
-
parsed = json.loads(
|
| 1207 |
-
|
| 1208 |
-
|
| 1209 |
-
|
| 1210 |
if not lines:
|
| 1211 |
-
# Handle t1, t2, t3... format
|
| 1212 |
lines = [parsed.get(f"t{i}", "") for i in range(1, 7) if parsed.get(f"t{i}")]
|
| 1213 |
if not lines:
|
| 1214 |
lines = [parsed.get(f"l{i+1}", "") for i in range(4) if parsed.get(f"l{i+1}")]
|
| 1215 |
-
|
| 1216 |
-
# Get art ID from LLM response
|
| 1217 |
art_id = parsed.get("art", None)
|
| 1218 |
if art_id is not None:
|
| 1219 |
try:
|
| 1220 |
art_id = int(art_id)
|
| 1221 |
except (ValueError, TypeError):
|
| 1222 |
art_id = None
|
| 1223 |
-
except (json.JSONDecodeError, KeyError, TypeError):
|
| 1224 |
-
|
| 1225 |
-
|
| 1226 |
-
|
| 1227 |
-
|
|
|
|
|
|
|
|
|
|
| 1228 |
|
| 1229 |
if lines:
|
| 1230 |
# Deduplicate and clean
|
|
@@ -1258,8 +1275,8 @@ How do I feel right now?"""
|
|
| 1258 |
if rnd.random() < 0.3: # 30% chance to move
|
| 1259 |
dirs = ["up", "down", "left", "right"]
|
| 1260 |
self.grid.move(rnd.choice(dirs))
|
| 1261 |
-
|
| 1262 |
-
return tuple(lines)
|
| 1263 |
|
| 1264 |
# Fallback with context awareness
|
| 1265 |
self.agent_thinking = False
|
|
|
|
| 474 |
@spaces.GPU(duration=30)
|
| 475 |
def _hf_gpu_generate(messages, max_new_tokens=150):
|
| 476 |
model = _hf_model.to("cuda")
|
| 477 |
+
try:
|
| 478 |
+
# Ask the model not to emit chain-of-thought, if it supports it
|
| 479 |
+
text = _hf_tok.apply_chat_template(
|
| 480 |
+
messages, tokenize=False, add_generation_prompt=True,
|
| 481 |
+
enable_thinking=False,
|
| 482 |
+
)
|
| 483 |
+
except TypeError:
|
| 484 |
+
text = _hf_tok.apply_chat_template(
|
| 485 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 486 |
+
)
|
| 487 |
inputs = _hf_tok(text, return_tensors="pt").to("cuda")
|
| 488 |
with torch.no_grad():
|
| 489 |
out = model.generate(
|
|
|
|
| 1207 |
response = self.call_llm(prompt, system_prompt=system)
|
| 1208 |
|
| 1209 |
if response:
|
| 1210 |
+
# Strip any chain-of-thought blocks the model emits, then ASCII-clean
|
| 1211 |
+
response = re.sub(r"<think>.*?</think>", " ", response, flags=re.DOTALL | re.IGNORECASE)
|
| 1212 |
+
response = re.sub(r"</?think>", " ", response, flags=re.IGNORECASE)
|
| 1213 |
response = response.encode('ascii', errors='ignore').decode('ascii').strip()
|
| 1214 |
+
|
| 1215 |
+
parsed = {}
|
| 1216 |
+
art_id = None
|
| 1217 |
+
lines = []
|
| 1218 |
+
|
| 1219 |
+
# Find a JSON object even if the model wrapped it in prose
|
| 1220 |
+
m = re.search(r"\{.*\}", response, flags=re.DOTALL)
|
| 1221 |
+
candidate = m.group(0) if m else response
|
| 1222 |
try:
|
| 1223 |
+
parsed = json.loads(candidate)
|
| 1224 |
+
if not isinstance(parsed, dict):
|
| 1225 |
+
parsed = {}
|
| 1226 |
+
lines = parsed.get("s") or parsed.get("message") or []
|
| 1227 |
if not lines:
|
|
|
|
| 1228 |
lines = [parsed.get(f"t{i}", "") for i in range(1, 7) if parsed.get(f"t{i}")]
|
| 1229 |
if not lines:
|
| 1230 |
lines = [parsed.get(f"l{i+1}", "") for i in range(4) if parsed.get(f"l{i+1}")]
|
|
|
|
|
|
|
| 1231 |
art_id = parsed.get("art", None)
|
| 1232 |
if art_id is not None:
|
| 1233 |
try:
|
| 1234 |
art_id = int(art_id)
|
| 1235 |
except (ValueError, TypeError):
|
| 1236 |
art_id = None
|
| 1237 |
+
except (json.JSONDecodeError, KeyError, TypeError, AttributeError):
|
| 1238 |
+
parsed = {}
|
| 1239 |
+
|
| 1240 |
+
# No usable JSON lines? Derive short lines from the prose itself.
|
| 1241 |
+
if not lines:
|
| 1242 |
+
text = re.sub(r"\s+", " ", response).strip()
|
| 1243 |
+
frags = re.split(r"[.!?\n|]+", text)
|
| 1244 |
+
lines = [f.strip() for f in frags if len(f.strip()) > 2][:6]
|
| 1245 |
|
| 1246 |
if lines:
|
| 1247 |
# Deduplicate and clean
|
|
|
|
| 1275 |
if rnd.random() < 0.3: # 30% chance to move
|
| 1276 |
dirs = ["up", "down", "left", "right"]
|
| 1277 |
self.grid.move(rnd.choice(dirs))
|
| 1278 |
+
|
| 1279 |
+
return tuple(lines[:4])
|
| 1280 |
|
| 1281 |
# Fallback with context awareness
|
| 1282 |
self.agent_thinking = False
|