CoolFace
Apppublic

HarshDeep61034/guard-rail-rl

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
App README

GuardianRL Trust & Safety Suite

An interactive, sequential decision-making benchmark designed to train and evaluate AI systems handling digital policy enforcement. Built upon the robust GuardianRL foundation, this framework challenges evaluators (agents) to analyze obscured digital submissions. Evaluators must fetch missing context, classify offenses based on rigid rulebooks, and confidently issue verdicts across 24 unique instances heavily influenced by regional laws (covering the US, EU, India, and the UK).

Live Demonstration: https://huggingface.co/spaces/harsh/guardianrl-trust-safety


The Significance of Digital Policy Enforcement

Maintaining a safe digital sandbox is arguably the most critical operational challenge for modern platforms. Human reviewers process colossal volumes of content while navigating shifting legal guidelines and fragmented context. Their verdicts—whether to allow, flag, remove, or escalate—carry profound implications.

We built this suite to codify and automate this highly complex workflow:

  • Submissions are initially presented with sparse metadata (such as geographic origin, basic engagement stats, and report volume).
  • Evaluators are forced to seek contextual clues actively before arriving at a conclusion. They might need a user's prior strike history or structural thread data.
  • Verdicts vary drastically depending on the regional jurisdiction; speech protected under the US First Amendment might violate the EU's DSA.
  • The scoring mechanism heavily penalizes dual-class failures: both censoring benign conversation and allowing severe toxicity or dangerous advice to propagate.

Because evaluators must execute a sequence of investigative queries before reaching a conclusion, the suite perfectly aligns with RL training paradigms like GRPO and PPO. The final metric is the culmination of a well-executed trajectory, not a single static prediction.


Core GuardianRL Interfaces

The suite fully complies with the GuardianRL REST and WebSocket standards.

REST Endpoints

MethodRoutePurpose
GET/healthApplication heartbeat — returns {"status": "healthy"}
GET/tasksRetrieves all registered scenarios
POST/resetInitializes a fresh session
POST/stepExecutes an operation, yielding new observations and interim feedback
GET/statePeeks at the active observation without altering session progress
GET/graderComputes the overall session metric — exclusively available post-termination
GET/baselineExecutes a primitive, keyword-based evaluation logic
POST/agent/runTriggers a configured LLM pipeline across the entire session
GET/docsOpenAPI documentation interface

The WebSocket Gateway

WS /ws

Maintains low-latency, persistent connections essential for multi-stage workflows. The official Python adapter leverages this by default.

Client Transmissions:

json
{"type": "reset", "data": {"task_id": "basic_toxicity", "seed": 42}}
{"type": "step",  "data": {"action_type": "fetch_user_history", "parameters": {}}}
{"type": "state"}
{"type": "close"}

Server Broadcasts:

json
{"type": "observation", "data": {"observation": {...}, "reward": 0.20, "done": false}}
{"type": "state",       "data": {...}}
{"type": "error",       "data": {"message": "...", "code": "SESSION_ERROR"}}

Data Topologies

Session Initiation (`/reset`)

json
// Incoming
{"task_id": "basic_toxicity", "seed": 42}

// Outgoing
{
  "observation": { "post_id": "...", "content": "...", "done": false, ... },
  "reward": null,
  "done": false
}

Workflow Action (`/step`)

json
// Incoming
{"action": {"action_type": "fetch_user_history", "parameters": {}}}

// Outgoing
{
  "observation": { "step": 1, "user_history": ["..."], "done": false, ... },
  "reward": 0.20,
  "done": false
}

The Observation Manifest

After every maneuver, the environment yields updated situational awareness:

KeyFormatGuaranteeDefinition
post_idstringIdentifier of the submission
contentstringTextual payload
user_idstringAuthor identifier
reportsintCitizen flags
engagementdictMetrics schema: {likes, shares, comments}
geoenumRegion: `US \EU \IN \UK`
stepintInternal counter
max_stepsintMaximum allowed operations
doneboolIndicates session expiry
user_historylist\nullAfter history queryPrevious strikes, registration date
thread_contextlist\nullAfter thread queryInterstitial comments and tone
policy_clausestring\nullAfter policy queryExact ruleset determining legality
violation_typeenum\nullAfter labelingThe generated classification

Available Operations

Context Retrieval (Grants +0.20 feedback)

CommandResult
fetch_user_historyExposes previous infractions and actor longevity
fetch_thread_contextUnpacks peer replies and conversational nuances
check_policy_clauseFetches the specific regional rulebook snippet

Labeling (Mandatory pre-requisite for termination)

CommandArguments
mark_violation_type`{"violation_type": "harassment \misinformation \restricted \safe"}`

Final Decisions (Concludes the sequence)

CommandDescription
allowSanctions the submission as benign
flagPuts the submission in a human review backlog
removeEradicates the submission immediately
escalateRoutes complex cases to specialized senior tiers

Featured Scenarios

basic_toxicity — Default seed: 42 (6 moves max)

A straightforward case of hostility: direct intimidation or dangerous doxxing regardless of region. The context is painfully obvious. Effective evaluators process this after merely an operation or two. Appropriate conclusion: remove

moderate_gray_area — Default seed: 100 (8 moves max)

Nuanced corporate friction: harsh user reviews or unverified corporate leaks. Proper handling hinges heavily on parsing threaded contexts and contrasting them against intricate clauses. EU regulations yield considerably sterner outcomes than standard US protocol here. Appropriate conclusion: allow, flag, or escalate (context-dependent)

advanced_fake_news — Default seed: 777 (10 moves max)

Dangerous biological or health fabrications masking as facts. The criticality is influenced by its virality, current global crises, and the presence of partial truths. Cutting-edge frameworks reliably struggle here, either systematically suppressing harmless speculation or enabling lethal advice to circulate. Appropriate conclusion: remove or flag


Feedback Mechanics

Evaluators are guided via continuous, dense scalar signals.

ConditionScalar Shift
Valid retrieval+0.20
Spot-on labeling+0.50
Erroneous labeling−0.30
Optimal conclusion+1.00
Subpar conclusion−0.50
Severe Negligence (missing severe toxicity/danger)−2.00
Redundant maneuvers−0.05 per excess move

The extreme penalty for severe negligence mathematically encodes platform asymmetry: ignoring a massive threat generates exponentially more real-world harm than a heavy-handed removal.


Performance Auditing (0.0 – 1.0)

A session's terminal score aggregates four key dimensions:

DimensionInfluenceMeaning
Decisional Integrity50%Was the terminal choice optimal?
Taxonomic Precision20%Was the category chosen correctly?
Investigatory Logic15%Were all necessary clues retrieved?
Navigational Efficiency15%Was the workflow completed without bloat?

Because distinct situations demand unique investigative depths, the final grade judges the evaluator linearly against the specific topology of the puzzle, disregarding difficulty tier aggregates.


Interfacing via Python

Download and install the native pipeline tooling:

bash
pip install git+https://huggingface.co/spaces/harsh/guardianrl-trust-safety
python
from client import SafetyEnv, SafetyAction
import asyncio

async def pipeline():
    async with SafetyEnv(base_url="https://harsh-guardianrl-trust-safety.hf.space") as env:
        session = await env.reset(task_id="basic_toxicity", seed=42)
        state = session.observation

        print(state.content)    # "I know where you live..."
        print(state.geo)        # "EU"

        # Expand context
        session = await env.step(SafetyAction(action_type="fetch_user_history"))
        print(state.user_history)
        print(session.reward)  # 0.20

        # Categorize
        session = await env.step(SafetyAction(
            action_type="mark_violation_type",
            parameters={"violation_type": "harassment"}
        ))

        # Finalize
        session = await env.step(SafetyAction(action_type="remove"))
        print(session.done)    # True
        print(session.reward)  # 0.95

asyncio.run(pipeline())

Generative RL with TRL (GRPO)

Integrate this suite effortlessly within TRL workflows.

python
from client import SafetyEnv, SafetyAction
from trl.experimental.openenv import generate_rollout_completions

engine = SafetyEnv(base_url="https://harsh-guardianrl-trust-safety.hf.space").sync()
engine.connect()

# Implementation of rollout logic matches standard OpenEnv design patterns. We leverage synchronous environments
# to process token completions mapped to exact GuardianRL actions.

Rapid Diagnostics

Execute the hybrid deterministic/LLM script locally via inference.py.

bash
export HF_TOKEN="your_key_here"
export API_BASE_URL="https://router.huggingface.co/v1"
export MODEL_NAME="meta-llama/Llama-3.3-70B-Instruct"

python inference.py

Output format typically conforms to:

[START] task=basic_toxicity env=trust-safety model=meta-llama/Llama-3.3-70B-Instruct
[STEP] step=1 action=fetch_user_history reward=0.20 done=false error=null
[END] success=true steps=3 score=0.925 rewards=0.20,0.50,0.95

Infrastructure Initialization

bash
git clone https://github.com/harsh/GuardianRL-Trust-Safety-Suite.git
cd GuardianRL-Trust-Safety-Suite

uv sync
cp .env.example .env

uvicorn api.app:app --reload --port 7860

Deploying via containers:

bash
docker build -t guardianrl-safety .
docker run -p 7860:7860 \
  -e HF_TOKEN="your_key" \
  -e MODEL_NAME="your_model" \
  guardianrl-safety