rmi-backend/app/_archive/legacy_2026_07/moderation.py
cryptorugmunch 628c1d2a10
Some checks failed
CI / build (push) Failing after 3s
refactor(rmi-backend,audit): mount Wave 3 + archive 136 dead-code files (P2.3)
PHASE 2.3 (AUDIT-2026-Q3.md):

Task 1 — Wire-in Wave 3 (1 router mounted, 2 deferred):
  - app.routers.unified_scanner_router mounted at /api/v2/scanner/* (2 routes:
    POST /api/v2/scanner/token/scan, POST /api/v2/scanner/wallet/scan).
    Refactored prefix from /api/v2 -> /api/v2/scanner to avoid future conflicts
    with the v1 /api/v1/scanner/ stub.
  - app.routers.unified_wallet_scanner DEFERRED (no router APIRouter attribute;
    library module consumed by unified_scanner_router via get_wallet_scanner()).
  - app.routers.admin_extensions DEFERRED (DORMANT per audit; 25 routes at
    /api/v1/admin/* would shadow /api/v1/admin/alerts_webhook).

Task 2 — Archive 136 dead-code files to app/_archive/legacy_2026_07/:
  - 73 routers in app/routers/ (reach graph showed zero reach into mount.py).
  - 63 flat app/*.py (domain modules never imported by live code).
  - 1 file RESTORED post-archive: app/routers/x402_bridge_health.py (caught by
    tests/unit/test_bridge_health.py which directly imports it; reach graph
    considered tests/ only as transitive reach — to be patched in next cycle).

Forced-LIVE (NOT archived per user directive):
  - app/ai_pipeline_v3.py  (3 importers in audit window, importers themselves DEAD)
  - app/splade_bm25.py       (LIVE via app.rag_service)
  - app/wallet_manager_v2.py (LIVE via x402_enforcement, x402_tools, sweep_all, sweep_now)
  - app/crypto_embeddings.py (NOT in audit ARCHIVE list; heavy import graph)

Verification (forward-import closure from mount.py + main.py + factory.py + lifespan.py):
  - imports = 348 app.* modules
  - reached = 194 files reachable from roots
  - archive set = audit_dead (186) - reached - forced_live (4) - test_live (1) = 136
  - Net delta: 136 files moved, 44,932 LOC reduction, 293->295 active routes (+2 from Wave 3)

pyproject.toml updates:
  - setuptools.packages.find: added exclude for app._archive*
  - ruff.extend-exclude: added "app/_archive/"
  - mypy.exclude: added "app/_archive/"

Smoke test: pytest tests/ — 817 passed, 3 pre-existing failures unchanged
(0 new failures; 0 routes lost; all 4 forced-LIVE files still importable).

Restoration: git mv app/_archive/legacy_2026_07/<name>.py <original-path>
and add the import to app/mount.py ROUTER_MODULES.

Refs: AUDIT-2026-Q3.md /home/dev/pry/rmi-final-deadcode-2026-07-06.md
2026-07-06 20:52:31 +02:00

116 lines
3.4 KiB
Python

"""Content Moderation Pipeline - AI-powered spam/scam/NSFW detection for user content."""
import os
import re
import httpx
from fastapi import APIRouter
from pydantic import BaseModel
router = APIRouter(prefix="/api/v1/moderation", tags=["moderation"])
OLLAMA = os.getenv("OLLAMA_HOST", "http://localhost:11434")
BLOCKED_PATTERNS = [
(r"(?i)(buy|sell|trade).*\b(signal|call)\b", "trading_signal"),
(r"(?i)(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)", "ip_address"),
(r"(?i)\b(0x[a-fA-F0-9]{40})\b.*\b(private.?key|seed.?phrase|mnemonic|password)\b", "wallet_phishing"),
(r"(?i)(airdrop|giveaway|free.*token).*\b(claim|connect|verify)\b", "scam_airdrop"),
(r"(https?://(?!rugmunch\.io|polymarket\.com|dexscreener\.com)[^\s]+)", "external_link"),
]
class ModerationRequest(BaseModel):
text: str
user_id: str = "anonymous"
context: str = "comment" # comment, post, review, chat
class ModerationResult(BaseModel):
approved: bool
risk_score: int # 0-100
flags: list[str]
reason: str = ""
async def _ai_classify(text: str) -> dict:
"""Use Ollama to classify content."""
try:
async with httpx.AsyncClient(timeout=15) as c:
r = await c.post(
f"{OLLAMA}/api/generate",
json={
"model": "qwen2.5-coder:7b",
"prompt": f"Classify this crypto-related content as SAFE, SPAM, SCAM, or NSFW. Answer with one word only.\n\nContent: {text[:500]}\n\nClassification:",
"stream": False,
"options": {"num_predict": 5, "temperature": 0.1},
},
)
if r.status_code == 200:
response = r.json().get("response", "").strip().upper()
return {"ai_verdict": response, "ai_used": True}
except Exception:
pass
return {"ai_verdict": "UNKNOWN", "ai_used": False}
@router.post("/check")
async def moderate_content(req: ModerationRequest):
"""Check content for spam, scams, NSFW, and policy violations."""
flags = []
risk = 0
# Pattern-based detection
for pattern, flag_type in BLOCKED_PATTERNS:
if re.search(pattern, req.text):
flags.append(flag_type)
risk += 25
# Length checks
if len(req.text) < 5:
flags.append("too_short")
risk += 10
if len(req.text) > 10000:
flags.append("too_long")
risk += 5
# AI classification
ai = await _ai_classify(req.text)
ai_verdict = ai["ai_verdict"]
if "SCAM" in ai_verdict:
flags.append("ai_scam")
risk += 40
elif "SPAM" in ai_verdict:
flags.append("ai_spam")
risk += 25
elif "NSFW" in ai_verdict:
flags.append("ai_nsfw")
risk += 50
approved = risk < 40
reason = "Content approved" if approved else f"Flagged: {', '.join(flags)}"
return {
"approved": approved,
"risk_score": min(100, risk),
"flags": flags,
"reason": reason,
"ai_classification": ai_verdict,
}
@router.get("/stats")
async def moderation_stats():
return {
"patterns_checked": len(BLOCKED_PATTERNS),
"ai_model": "qwen2.5-coder:7b",
"categories": [
"trading_signal",
"wallet_phishing",
"scam_airdrop",
"external_link",
"ai_scam",
"ai_spam",
"ai_nsfw",
],
}