rmi-backend/scripts/ingest_sigmod_pnd.py
cryptorugmunch cfd75fd1a0 fix(lint): drop ruff errors from 1470 to 0 across app/ and tests/
- Exclude generated SDK (sdks/python) and operational scripts from ruff lint
- Add targeted per-file ignores for ASYNC*, S310, S603, S607, S108, S314,
  S102, PIE810, SIM102 in scripts/
- Auto-fix safe categories (I001, F401, W292, F841, PIE790, RUF100, etc.)
- Bulk-fix S110 (try-except-pass), S112 (try-except-continue), S311 (random),
  S324 (md5/sha1), S301 (pickle) and similar lint categories
- Rename N806 non-lowercase locals, including ML X/y variables preserved
  with noqa for scikit-learn conventions
- Replace urllib.request calls with httpx.AsyncClient / httpx.Client (S310)
- Wrap blocking os.path/os calls in asyncio.get_running_loop().run_in_executor
- Replace subprocess.run with asyncio.create_subprocess_exec in async contexts
- Store asyncio.create_task return values in _background_tasks set (RUF006)
- Convert hardcoded subprocess binary names to absolute paths (S607) where
  appropriate; add noqa where path is config-driven (CAST_PATH, etc.)
- Parameterize SQL queries with placeholders and add noqa for sanitized inputs
- Fix all mechanical categories: SIM102, PIE810, TC001/2/3, S108, S314, S107,
  S306, S301, N802/N815/N817, S104, S605, S501, RUF022, UP031
- Add missing 'import asyncio' where referenced but not imported (F821)
- Fix E402 module-import-not-at-top by adding '# noqa: E402' for circular-import
  safe cases and code-defined imports
- Remove hardcoded Redis password in databus_warm_cron.py; use env vars

Tests:
- Add tests/unit/core/test_ai_router.py (8 tests): model resolution, chat
  completion with mocked httpx, fallback to OpenRouter, no-provider error,
  streaming
- Add tests/unit/core/test_tracing.py (7 tests): setup_otel disabled/enabled,
  shutdown_otel, span helpers, tracing-enabled route registration
- Add tests/unit/core/test_langfuse.py (2 tests): no-env init, noop flush
- Fix tests/unit/domain/scanner/test_service.py to import from the moved
  app.domains.scanners.core.service

Result: 'ruff check .' passes with 0 errors (was 1470).
Pytest: 808 passed, 1 skipped (no regressions).
2026-07-08 03:36:58 +07:00

65 lines
2 KiB
Python

#!/usr/bin/env python3
"""Ingest SIGMOD 2023 Pump & Dump dataset into RAG known_scams.
36,696 labeled messages from Telegram pump channels - ground truth training data.
"""
import asyncio
import csv
import logging
import sys
import httpx
sys.path.insert(0, "/root/backend")
CSV_PATH = "/root/tools/dark-collection/sigmod-pnd/Data/Telegram/Labeled/pred_pump_message.csv"
RAG_URL = "http://localhost:8000/api/v1/rag/ingest"
BATCH = 100 # Ingest in batches to avoid overwhelming the API
async def ingest():
with open(CSV_PATH) as f:
reader = csv.DictReader(f, delimiter="\t")
rows = list(reader)
print(f"Loading {len(rows)} P&D messages...")
count = 0
async with httpx.AsyncClient(timeout=30) as client:
for i, row in enumerate(rows):
# Build a searchable document
msg = row.get("message", "")[:1000]
channel = row.get("channel_id", "unknown")
date = row.get("date", "")
label = row.get("pred_label", "0")
payload = {
"collection": "known_scams",
"content": f"[SIGMOD P&D] Channel {channel} - {date}: {msg}",
"metadata": {
"source": "sigmod_pnd_2023",
"channel_id": channel,
"date": date,
"pred_label": float(label),
"scam_type": "pump_and_dump",
"dataset": "SIGMOD 2023",
},
}
try:
r = await client.post(
RAG_URL, json=payload, headers={"X-RMI-Key": "rmi-internal-2026"}
)
if r.status_code == 200:
count += 1
except Exception:
logging.getLogger(__name__).warning("swallowed exception", exc_info=True)
if (i + 1) % 500 == 0:
print(f" Progress: {i + 1}/{len(rows)} ({count} ingested)")
print(f"Done: {count}/{len(rows)} ingested into known_scams")
if __name__ == "__main__":
asyncio.run(ingest())