Spaces:
Running
Running
Update app/services/classifier.py
Browse files- app/services/classifier.py +61 -23
app/services/classifier.py
CHANGED
|
@@ -16,7 +16,14 @@ NSFW_WEIGHTS_PATH = MODELS_DIR / "model.safetensors"
|
|
| 16 |
NSFW_THRESHOLD = 0.75
|
| 17 |
|
| 18 |
LLM_MODELS = [
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
]
|
| 21 |
LLM_MAX_CHARS = 3000
|
| 22 |
|
|
@@ -297,33 +304,64 @@ def _classify_game_with_llm(ocr_text: str, urls: list[str], suspected_game: bool
|
|
| 297 |
{"role": "user", "content": json.dumps(user_payload, ensure_ascii=True)},
|
| 298 |
]
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
continue
|
| 317 |
-
parsed["source"] = f"llm:{model_name}"
|
| 318 |
-
return parsed
|
| 319 |
-
except Exception as exc:
|
| 320 |
-
logger.warning("llm error model=%s err=%s", model_name, repr(exc))
|
| 321 |
-
continue
|
| 322 |
|
| 323 |
-
logger.warning("llm all models failed; fallback to heuristic")
|
| 324 |
return None
|
| 325 |
|
| 326 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
def _parse_llm_json(content: str) -> dict[str, Any] | None:
|
| 328 |
if not content:
|
| 329 |
return None
|
|
|
|
| 16 |
NSFW_THRESHOLD = 0.75
|
| 17 |
|
| 18 |
LLM_MODELS = [
|
| 19 |
+
x.strip()
|
| 20 |
+
for x in os.getenv("GAME_LLM_MODELS", os.getenv("GAME_LLM_MODEL", "Qwen/Qwen2.5-1.5B-Instruct")).split(",")
|
| 21 |
+
if x.strip()
|
| 22 |
+
]
|
| 23 |
+
LLM_PROVIDERS = [
|
| 24 |
+
x.strip()
|
| 25 |
+
for x in os.getenv("GAME_LLM_PROVIDERS", "auto,hf-inference").split(",")
|
| 26 |
+
if x.strip()
|
| 27 |
]
|
| 28 |
LLM_MAX_CHARS = 3000
|
| 29 |
|
|
|
|
| 304 |
{"role": "user", "content": json.dumps(user_payload, ensure_ascii=True)},
|
| 305 |
]
|
| 306 |
|
| 307 |
+
# Last candidate is empty => let HF pick a recommended chat model.
|
| 308 |
+
model_candidates = list(LLM_MODELS) + [""]
|
| 309 |
+
|
| 310 |
+
for provider_name in LLM_PROVIDERS:
|
| 311 |
+
for model_name in model_candidates:
|
| 312 |
+
model_label = model_name or "<recommended>"
|
| 313 |
+
try:
|
| 314 |
+
logger.info("llm try provider=%s model=%s", provider_name, model_label)
|
| 315 |
+
client = InferenceClient(
|
| 316 |
+
model=model_name or None,
|
| 317 |
+
provider=provider_name,
|
| 318 |
+
token=token or None,
|
| 319 |
+
)
|
| 320 |
+
response = client.chat_completion(
|
| 321 |
+
messages=prompt_messages,
|
| 322 |
+
max_tokens=160,
|
| 323 |
+
temperature=0.1,
|
| 324 |
+
top_p=0.9,
|
| 325 |
+
)
|
| 326 |
+
content = ""
|
| 327 |
+
if response.choices:
|
| 328 |
+
content = response.choices[0].message.content or ""
|
| 329 |
+
parsed = _parse_llm_json(content)
|
| 330 |
+
if parsed is None:
|
| 331 |
+
logger.warning(
|
| 332 |
+
"llm invalid json provider=%s model=%s content=%s",
|
| 333 |
+
provider_name,
|
| 334 |
+
model_label,
|
| 335 |
+
content[:300],
|
| 336 |
+
)
|
| 337 |
+
continue
|
| 338 |
+
parsed["source"] = f"llm:{provider_name}:{model_label}"
|
| 339 |
+
return parsed
|
| 340 |
+
except Exception as exc:
|
| 341 |
+
if _is_model_not_supported_error(exc):
|
| 342 |
+
logger.warning(
|
| 343 |
+
"llm model/provider unsupported provider=%s model=%s",
|
| 344 |
+
provider_name,
|
| 345 |
+
model_label,
|
| 346 |
+
)
|
| 347 |
+
continue
|
| 348 |
+
logger.warning(
|
| 349 |
+
"llm error provider=%s model=%s err=%s",
|
| 350 |
+
provider_name,
|
| 351 |
+
model_label,
|
| 352 |
+
repr(exc),
|
| 353 |
+
)
|
| 354 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
+
logger.warning("llm all providers/models failed; fallback to heuristic")
|
| 357 |
return None
|
| 358 |
|
| 359 |
|
| 360 |
+
def _is_model_not_supported_error(exc: Exception) -> bool:
|
| 361 |
+
text = repr(exc).lower()
|
| 362 |
+
return "model_not_supported" in text or "not supported by any provider" in text
|
| 363 |
+
|
| 364 |
+
|
| 365 |
def _parse_llm_json(content: str) -> dict[str, Any] | None:
|
| 366 |
if not content:
|
| 367 |
return None
|