"""FastAPI + Gradio server for the GraphStrike OpenEnv environment."""
from __future__ import annotations
import json
import os
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
sys.path.insert(0, str(Path(__file__).parent.parent))
from fastapi import Body, FastAPI, HTTPException
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import Any, Dict, Optional
from models import FakeGangAction, FakeGangObservation, FakeGangState, ActionType
from environment import FakeGangEnvironment
# ---------------------------------------------------------------------------
# App + environment
# ---------------------------------------------------------------------------
app = FastAPI(
title="GraphStrike — OpenEnv",
description="RL environment for detecting coordinated fake account rings in social networks.",
version="1.0.0",
)
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
# Serve project images at /static/* and /img/* (NOT /assets/ — Gradio uses that path for its own JS/CSS)
_PROJECT_ROOT = Path(__file__).parent.parent
_ASSETS_DIR = _PROJECT_ROOT / "assets"
_IMAGES_DIR = _PROJECT_ROOT / "images"
if _ASSETS_DIR.exists():
app.mount("/static", StaticFiles(directory=str(_ASSETS_DIR)), name="static")
if _IMAGES_DIR.exists():
app.mount("/img", StaticFiles(directory=str(_IMAGES_DIR)), name="img")
_env = FakeGangEnvironment()
class ResetRequest(BaseModel):
task: str = "easy"
seed: Optional[int] = None
episode_id: Optional[str] = None
class StepResponse(BaseModel):
observation: Dict[str, Any]
done: bool
reward: Optional[float]
message: str
# ---------------------------------------------------------------------------
# OpenEnv API endpoints
# ---------------------------------------------------------------------------
@app.get("/health")
def health():
return {"status": "healthy"}
@app.post("/reset", response_model=StepResponse)
def reset(req: Optional[ResetRequest] = Body(default=None)):
if req is None:
req = ResetRequest()
obs = _env.reset(task=req.task, seed=req.seed, episode_id=req.episode_id)
return StepResponse(observation=obs.model_dump(), done=obs.done, reward=obs.reward, message=obs.message)
@app.post("/step", response_model=StepResponse)
def step(action: FakeGangAction):
obs = _env.step(action)
return StepResponse(observation=obs.model_dump(), done=obs.done, reward=obs.reward, message=obs.message)
@app.get("/state")
def state():
return _env.state.model_dump()
@app.get("/tasks")
def list_tasks():
_formula = (
"if recall >= win_recall and precision >= win_precision: "
"score = 0.55 + 0.20*recall + 0.15*precision + 0.10*efficiency "
"else: score = 0.30*recall + 0.10*precision"
)
return {
"tasks": [
{
"name": "easy",
"description": "50 accounts, 10 fakes, no evasion, 30 steps",
"max_steps": 30,
"grader": {
"endpoint": "/grader",
"score_range": [0.0, 1.0],
"win_threshold": 0.815,
"win_conditions": {"recall": 0.8, "precision": 0.7},
"formula": _formula,
},
},
{
"name": "medium",
"description": "200 accounts, 10 fakes + 20 decoys, evasion at step 20, 50 steps",
"max_steps": 50,
"grader": {
"endpoint": "/grader",
"score_range": [0.0, 1.0],
"win_threshold": 0.815,
"win_conditions": {"recall": 0.8, "precision": 0.7},
"formula": _formula,
},
},
{
"name": "hard",
"description": "1000 accounts, 10 fakes + 50 decoys, recurring evasion, 80 steps",
"max_steps": 80,
"grader": {
"endpoint": "/grader",
"score_range": [0.0, 1.0],
"win_threshold": 0.868,
"win_conditions": {"recall": 0.9, "precision": 0.8},
"formula": _formula,
},
},
],
"action_schema": {
"action_type": ["inspect", "investigate_network", "flag", "unflag", "submit"],
"account_id": "string (required for all actions except submit)",
},
"score_range": [0.0, 1.0],
}
@app.get("/grader")
def grader():
if not _env._done:
raise HTTPException(status_code=400, detail="Episode not complete. Call SUBMIT first.")
return {"score": _env._last_grader_score, "task": _env._task, "episode_id": _env._episode_id}
@app.get("/metadata")
def metadata():
return {
"name": "graphstrike", "version": "1.0.0", "author": "Pandago",
"description": "RL environment for detecting coordinated fake account rings in social networks.",
"tags": ["social-network", "fraud-detection", "graph", "rl"],
}
@app.get("/schema")
def schema():
return {
"action": FakeGangAction.model_json_schema(),
"observation": FakeGangObservation.model_json_schema(),
"state": FakeGangState.model_json_schema(),
}
@app.post("/mcp")
def mcp(body: Dict[str, Any] = {}):
method = body.get("method", "")
req_id = body.get("id", 1)
if method == "tools/list":
return {"jsonrpc": "2.0", "id": req_id, "result": {"tools": [
{"name": "reset", "description": "Reset the environment",
"inputSchema": {"type": "object", "properties": {"task": {"type": "string"}, "seed": {"type": "integer"}}}},
{"name": "step", "description": "Take an action", "inputSchema": FakeGangAction.model_json_schema()},
{"name": "state", "description": "Get episode state", "inputSchema": {"type": "object", "properties": {}}},
]}}
return {"jsonrpc": "2.0", "id": req_id, "result": {"name": "graphstrike", "version": "1.0.0", "protocolVersion": "2024-11-05"}}
@app.api_route("/baseline", methods=["GET", "POST"])
def baseline():
sys.path.insert(0, str(Path(__file__).parent.parent))
from inference import run_rule_based_episode
scores = {}
for task in ["easy", "medium", "hard"]:
scores[task] = run_rule_based_episode(_env, task=task, seed=0)
return {"scores": scores, "agent": "rule_based"}
# HF Spaces probes /web — redirect to root (must be on FastAPI before Gradio mount)
@app.get("/web", response_class=RedirectResponse)
def web_redirect():
return RedirectResponse(url="/")
# ---------------------------------------------------------------------------
# Gradio UI
# ---------------------------------------------------------------------------
import pandas as pd
# ── Benchmark data ───────────────────────────────────────────────────────────
BENCH_SEED0 = [
# [Model, Params, Easy, Medium, Hard, Mean] — sorted by Mean desc
["Llama 4 Scout 17B", "17B", 0.960, 0.979, 0.976, 0.972],
["Ministral 3 8B", "8B", 0.967, 0.964, 0.964, 0.965],
["DeepSeek V3.2", "685B", 0.967, 0.960, 0.933, 0.953],
["Nemotron Super 3", "49B", 0.930, 0.941, 0.964, 0.945],
["Rule-Based Baseline","—", 0.910, 0.906, 0.904, 0.907],
["Gemma 3 12B", "12B", 0.900, 0.908, 0.908, 0.905],
]
BENCH_VARIANCE = [
# [Model, Easy mean, Easy var, Med mean, Med var, Hard mean, Hard var]
["Llama 4 Scout 17B", 0.960, 0.000007, 0.979, 0.000001, 0.976, 0.000063],
["Nemotron Super 3", 0.957, 0.000, 0.957, 0.000, 0.645, 0.208],
["Ministral 3 8B", 0.958, 0.000, 0.645, 0.208, 0.623, 0.195],
["DeepSeek V3.2", 0.640, 0.205, 0.957, 0.000, 0.645, 0.208],
["Gemma 3 12B", 0.912, 0.000, 0.917, 0.000, 0.603, 0.182],
]
PROFILE_HEADERS = ["Account", "Status", "Risk", "Node", "Beh", "Graph", "Hub", "Photo", "Bio", "IP", "F.Nbrs"]
# Long-format DataFrame for BarPlot
_bench_long_rows = []
for _r in BENCH_SEED0:
_bench_long_rows += [
{"Model": _r[0], "Task": "Easy", "Score": _r[2]},
{"Model": _r[0], "Task": "Medium", "Score": _r[3]},
{"Model": _r[0], "Task": "Hard", "Score": _r[4]},
]
BENCH_LONG_DF = pd.DataFrame(_bench_long_rows)
# ── HTML table builders ──────────────────────────────────────────────────────
def _score_color(s: float) -> str:
if s >= 0.960: return "#22c55e"
if s >= 0.930: return "#86efac"
if s >= 0.910: return "#facc15"
return "#f97316"
def _var_color(v: float) -> str:
if v < 0.001: return "#22c55e"
if v < 0.05: return "#facc15"
return "#f87171"
_TH = "padding:11px 16px;font-weight:600;white-space:nowrap;"
_TD = "padding:10px 16px;white-space:nowrap;"
_TABLE_WRAP = (
"overflow-x:auto;border-radius:10px;border:1px solid #1e3a5f;"
"font-family:'IBM Plex Mono',monospace;font-size:13.5px;"
)
_THEAD_BG = "background:#0c2340;"
def _leaderboard_html() -> str:
header = (
f""
f" "
)
rows = ""
for i, r in enumerate(BENCH_SEED0):
bg = "#162032" if i % 2 == 0 else "#0f172a"
is_base = r[0] == "Rule-Based Baseline"
name_cell = (
f"{r[0]} (baseline)"
if is_base else r[0]
)
name_color = "#94a3b8" if is_base else "#e2e8f0"
rows += (
f"# "
f"Model "
f"Params "
f"Easy "
f"Medium "
f"Hard "
f"Mean "
f"
An OpenEnv-compatible reinforcement learning environment where an LLM agent must identify all 10 members of a coordinated fake account ring hidden inside a synthetic social network. The agent learns via Reflexion and a dynamic hybrid rule/LLM policy — no gradient updates, no fine-tuning required.
Submitted to the OpenEnv Hackathon × SCALER School of Technology. Judges deploy this container, run their own LLM agent against it, and score on task quality, environment design, code quality, creativity, and domain quality.
End-to-end pipeline: episode generation → environment server → hybrid agent → reflexion memory
| Task | Network Size | Gang | Decoys | Max Steps | Win Condition | Baseline Score |
|---|---|---|---|---|---|---|
| Easy | 50 accounts | 10 | 0 | 30 | Recall ≥ 0.8, Precision ≥ 0.7 | 0.910 |
| Medium | 200 accounts | 10 | 20 | 50 | Recall ≥ 0.8, Precision ≥ 0.7 | 0.906 |
| Hard | 1000 accounts | 10 | 50 | 80 | Recall ≥ 0.9, Precision ≥ 0.8 | 0.904 |
Hard mode fires 4 evasion events (steps 15, 30, 45, 60) that drop intra-gang follow edges mid-investigation, destroying graph signals.
Node signals (offline) → Behavioral signals (temporal/device) → Graph signals (live at INSPECT) → False-positive control via hub legitimacy
| Feature | Fake Range | Real Range | What it measures |
|---|---|---|---|
photo_reuse_score | 0.30 – 0.95 | 0.00 – 0.15 | Stolen celebrity photos via pHash fingerprint matching |
bio_template_score | 0.20 – 0.90 | 0.00 – 0.12 | Cosine similarity to known fake bio templates |
comment_repeat_score | 0.60 – 0.90 | 0.00 – 0.08 | Fraction of copy-pasted spam comments across accounts |
| Feature | Fake Pattern |
|---|---|
avg_post_hour | All 10 gang members post within ±0.5h of each other (coordinated scheduling) |
account_age_days | Created same week — base_age ± 7 days |
shared_ip_count | = 9 for all gang members (one IP subnet per episode, unique seed) |
| Feature | Fake Pattern |
|---|---|
mutual_follow_rate | 0.6 – 0.9 (dense intra-gang mutual follows) |
flagged_neighbor_count | Grows as investigation proceeds — strongest late-game signal |
avg_neighbor_photo_reuse | High when cluster shares stolen content |
Episode flow: reset → inspect/flag/investigate loop → dual SUSPECT cascade → submit → grader score
| Action | Step Cost | Effect |
|---|---|---|
INSPECT acc_XXXX | 1 step | Reveals full AccountProfile + follow list; adds 1-hop neighbors to visible set |
INVESTIGATE_NETWORK acc_XXXX | 2 steps | Bidirectional 2-hop expansion (outgoing + incoming edges); re-cascades SUSPECT |
FLAG acc_XXXX | FREE | Marks as fake; triggers dual SUSPECT cascade (follow-graph + IP cluster) |
UNFLAG acc_XXXX | FREE | Removes flag; clears CONFIRMED_FAKE status |
SUBMIT | FREE | Ends episode; triggers grader scoring |
Every account the flagged member follows (_live_edges) becomes SUSPECT if visible and NORMAL. Gang follow density is 0.70+ so this is high-precision.
Every visible account sharing the same ip_cluster_id becomes SUSPECT. Gang shares ip_gang_<seed>; real accounts have unique IPs. Zero false positives.
All scoring functions are stateless and deterministic — called inside _build_profile() at every INSPECT
Node risk, Behavior risk, Graph risk components
Hub legitimacy, Composite fake_risk_score formula
Post-episode lessons injected into every future prompt — learning without weight updates
The LLM (Qwen3-80B via AWS Bedrock) cannot be fine-tuned — it is a black-box API. Instead, a separate Qwen3 call generates a 2–3 sentence lesson after each episode. The best winning trajectory is stored as a few-shot example injected into all future prompts.
Episode N:
LLM acts using: system_prompt + reflections[last 4] + best_trajectory
Episode ends → WIN or LOSS
LOSS → generate_reflection(action_log, outcome) → lesson stored
WIN → save trajectory if better reward + generate_success_reflection
Episode N+1:
last 4 reflections + best win trajectory injected into prompt
→ LLM has learned from its past without any weight updates
Dynamic alpha-weighted blend: rules dominate early, LLM earns trust through wins and reflections
A dynamic α-weighted blend of a deterministic rule engine and the LLM. α represents trust in the LLM — starts at 0.20 (rules dominate), climbs as the LLM wins consistently and accumulates reflections, capped per task to prevent the LLM from overriding correct high-confidence rule decisions.
Alpha progression: rule-dominated early training → LLM earns authority through wins
| Situation | Rule Action | Confidence |
|---|---|---|
| Steps remaining = 0 | SUBMIT | 1.00 |
| Uninspected SUSPECT accounts exist | INSPECT suspects[0] | 0.95 |
fake_risk ≥ 0.85 | FLAG that account | 0.95 |
fake_risk in [threshold, 0.85) | FLAG that account | 0.70 – 0.94 |
| 10 flags placed | SUBMIT | 0.85 |
| Steps remaining ≤ 3 | SUBMIT | 0.90 |
| Uninspected accounts available | INSPECT top candidate | 0.30 |
When rule_confidence ≥ alpha the rule engine overrides. At easy cap (0.50), the LLM controls only exploratory INSPECT decisions. At hard cap (0.85), the LLM controls most decisions except forced submits and suspect cascade.
COORDINATED FAKE ACCOUNT RING DETECTION — OPENENV RL ENVIRONMENT
API mode. Swagger
" # --------------------------------------------------------------------------- # Entry point # --------------------------------------------------------------------------- def main(): import uvicorn port = int(os.environ.get("PORT", 7860)) print(f"[GraphStrike] Starting on port {port}", flush=True) uvicorn.run("server.app:app", host="0.0.0.0", port=port, log_level="info", workers=1) if __name__ == "__main__": main()