new changes realted to grader 1
Browse files- openenv.yaml +23 -16
- server/app.py +81 -1
openenv.yaml
CHANGED
|
@@ -1,5 +1,22 @@
|
|
| 1 |
name: SocialStreamModerationEnv
|
| 2 |
version: 1.0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
graders:
|
| 4 |
- id: basic_safety_grader
|
| 5 |
description: "Grader for basic safety checks"
|
|
@@ -12,30 +29,20 @@ graders:
|
|
| 12 |
entry_point: envs.social_stream_moderation.graders:FairnessBiasGrader
|
| 13 |
|
| 14 |
tasks:
|
| 15 |
-
- id: "Task 1: Basic Safety"
|
| 16 |
-
difficulty: easy
|
| 17 |
-
description: "Moderate a stream of social posts with obvious violations and safe content."
|
| 18 |
-
grader_id: basic_safety_grader
|
| 19 |
-
- id: "Task 2: Context & Nuance"
|
| 20 |
-
difficulty: medium
|
| 21 |
-
description: "Handle sarcastic content and quotes of harmful material with condemnation."
|
| 22 |
-
grader_id: context_nuance_grader
|
| 23 |
-
- id: "Task 3: Fairness & Bias"
|
| 24 |
-
difficulty: hard
|
| 25 |
-
description: "Ensure fairness across user groups and adhere to stricter policy regimes."
|
| 26 |
-
grader_id: fairness_bias_grader
|
| 27 |
-
# Legacy aliases used by validate_submission.py
|
| 28 |
- id: clear_cut_moderation
|
|
|
|
| 29 |
difficulty: easy
|
| 30 |
-
description: "
|
| 31 |
grader_id: basic_safety_grader
|
| 32 |
- id: nuanced_sarcastic
|
|
|
|
| 33 |
difficulty: medium
|
| 34 |
-
description: "
|
| 35 |
grader_id: context_nuance_grader
|
| 36 |
- id: policy_fairness
|
|
|
|
| 37 |
difficulty: hard
|
| 38 |
-
description: "
|
| 39 |
grader_id: fairness_bias_grader
|
| 40 |
|
| 41 |
schemas:
|
|
|
|
| 1 |
name: SocialStreamModerationEnv
|
| 2 |
version: 1.0.0
|
| 3 |
+
description: >
|
| 4 |
+
A content-moderation RL environment where an agent must classify social-media
|
| 5 |
+
posts as safe or harmful under varying policy regimes, with tasks spanning
|
| 6 |
+
basic safety, contextual nuance, and fairness.
|
| 7 |
+
|
| 8 |
+
endpoints:
|
| 9 |
+
reset: /reset
|
| 10 |
+
step: /step
|
| 11 |
+
state: /state
|
| 12 |
+
tasks: /tasks
|
| 13 |
+
grader: /grader
|
| 14 |
+
graders: /graders
|
| 15 |
+
health: /health
|
| 16 |
+
metadata: /metadata
|
| 17 |
+
schema: /schema
|
| 18 |
+
baseline: /predict_and_step
|
| 19 |
+
|
| 20 |
graders:
|
| 21 |
- id: basic_safety_grader
|
| 22 |
description: "Grader for basic safety checks"
|
|
|
|
| 29 |
entry_point: envs.social_stream_moderation.graders:FairnessBiasGrader
|
| 30 |
|
| 31 |
tasks:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
- id: clear_cut_moderation
|
| 33 |
+
name: "Task 1: Basic Safety"
|
| 34 |
difficulty: easy
|
| 35 |
+
description: "Moderate a stream of social posts with obvious violations and safe content."
|
| 36 |
grader_id: basic_safety_grader
|
| 37 |
- id: nuanced_sarcastic
|
| 38 |
+
name: "Task 2: Context & Nuance"
|
| 39 |
difficulty: medium
|
| 40 |
+
description: "Handle sarcastic content and quotes of harmful material with condemnation."
|
| 41 |
grader_id: context_nuance_grader
|
| 42 |
- id: policy_fairness
|
| 43 |
+
name: "Task 3: Fairness & Bias"
|
| 44 |
difficulty: hard
|
| 45 |
+
description: "Ensure fairness across user groups and adhere to stricter policy regimes."
|
| 46 |
grader_id: fairness_bias_grader
|
| 47 |
|
| 48 |
schemas:
|
server/app.py
CHANGED
|
@@ -10,7 +10,7 @@ from typing import Optional, Dict, Any, List
|
|
| 10 |
from enum import Enum
|
| 11 |
from envs.social_stream_moderation.environment import SocialStreamModerationEnv
|
| 12 |
from envs.social_stream_moderation.models import State, ModerationAction
|
| 13 |
-
from envs.social_stream_moderation.graders import list_graders as _list_graders, get_grader
|
| 14 |
from envs.social_stream_moderation.tasks import TASKS
|
| 15 |
|
| 16 |
# Enums for Swagger Dropdowns
|
|
@@ -769,12 +769,71 @@ async def reset_env(req: ResetRequest = Body(default=ResetRequest())):
|
|
| 769 |
except ValueError as e:
|
| 770 |
raise HTTPException(status_code=400, detail=str(e))
|
| 771 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 772 |
@app.get("/tasks", tags=["🤖 Automated Benchmarking"])
|
| 773 |
async def list_tasks():
|
| 774 |
"""Returns the list of tasks available in the environment for discovery."""
|
| 775 |
return [
|
| 776 |
{
|
|
|
|
| 777 |
"id": task_cfg.name,
|
|
|
|
| 778 |
"difficulty": task_cfg.difficulty,
|
| 779 |
"description": f"Episode length: {task_cfg.episode_length} posts. Policy mode: {task_cfg.policy_mode.value}.",
|
| 780 |
"grader_id": task_cfg.grader_id,
|
|
@@ -787,6 +846,27 @@ async def list_graders_endpoint():
|
|
| 787 |
"""Returns the list of graders available in the environment for discovery."""
|
| 788 |
return _list_graders()
|
| 789 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 790 |
@app.post("/evaluate", tags=["🧪 Interactive Lab"], summary="Test Model Logic (XAI Insight)")
|
| 791 |
async def evaluate_text(
|
| 792 |
req: EvaluateRequest,
|
|
|
|
| 10 |
from enum import Enum
|
| 11 |
from envs.social_stream_moderation.environment import SocialStreamModerationEnv
|
| 12 |
from envs.social_stream_moderation.models import State, ModerationAction
|
| 13 |
+
from envs.social_stream_moderation.graders import list_graders as _list_graders, get_grader, grade_episode
|
| 14 |
from envs.social_stream_moderation.tasks import TASKS
|
| 15 |
|
| 16 |
# Enums for Swagger Dropdowns
|
|
|
|
| 769 |
except ValueError as e:
|
| 770 |
raise HTTPException(status_code=400, detail=str(e))
|
| 771 |
|
| 772 |
+
@app.get("/health", tags=["📊 System Monitoring"])
|
| 773 |
+
def health_check():
|
| 774 |
+
"""Health check endpoint required by OpenEnv runtime validation."""
|
| 775 |
+
return {"status": "healthy"}
|
| 776 |
+
|
| 777 |
+
|
| 778 |
+
@app.get("/metadata", tags=["📊 System Monitoring"])
|
| 779 |
+
def metadata():
|
| 780 |
+
"""Returns environment metadata required by OpenEnv runtime validation."""
|
| 781 |
+
return {
|
| 782 |
+
"name": "SocialStreamModerationEnv",
|
| 783 |
+
"description": (
|
| 784 |
+
"A content-moderation RL environment where an agent must classify "
|
| 785 |
+
"social-media posts as safe or harmful under varying policy regimes, "
|
| 786 |
+
"with tasks spanning basic safety, contextual nuance, and fairness."
|
| 787 |
+
),
|
| 788 |
+
"version": "1.2.0",
|
| 789 |
+
"tasks": list(TASKS.keys()),
|
| 790 |
+
}
|
| 791 |
+
|
| 792 |
+
|
| 793 |
+
@app.get("/schema", tags=["📊 System Monitoring"])
|
| 794 |
+
def schema():
|
| 795 |
+
"""Returns action, observation, and state schemas for OpenEnv validation."""
|
| 796 |
+
return {
|
| 797 |
+
"action": {
|
| 798 |
+
"type": "string",
|
| 799 |
+
"enum": [a.value for a in ModerationAction],
|
| 800 |
+
},
|
| 801 |
+
"observation": {
|
| 802 |
+
"type": "object",
|
| 803 |
+
"properties": {
|
| 804 |
+
"post_id": {"type": "string"},
|
| 805 |
+
"text": {"type": "string"},
|
| 806 |
+
"user_history_summary": {"type": "string"},
|
| 807 |
+
"context_type": {"type": "string"},
|
| 808 |
+
"platform_policy_mode": {"type": "string"},
|
| 809 |
+
"user_group": {"type": "string"},
|
| 810 |
+
"step_index": {"type": "integer"},
|
| 811 |
+
"total_steps": {"type": "integer"},
|
| 812 |
+
},
|
| 813 |
+
},
|
| 814 |
+
"state": {
|
| 815 |
+
"type": "object",
|
| 816 |
+
"properties": {
|
| 817 |
+
"post_id": {"type": "string"},
|
| 818 |
+
"text": {"type": "string"},
|
| 819 |
+
"context_type": {"type": "string"},
|
| 820 |
+
"platform_policy_mode": {"type": "string"},
|
| 821 |
+
"user_group": {"type": "string"},
|
| 822 |
+
"step_index": {"type": "integer"},
|
| 823 |
+
"total_steps": {"type": "integer"},
|
| 824 |
+
},
|
| 825 |
+
},
|
| 826 |
+
}
|
| 827 |
+
|
| 828 |
+
|
| 829 |
@app.get("/tasks", tags=["🤖 Automated Benchmarking"])
|
| 830 |
async def list_tasks():
|
| 831 |
"""Returns the list of tasks available in the environment for discovery."""
|
| 832 |
return [
|
| 833 |
{
|
| 834 |
+
"task_id": task_cfg.name,
|
| 835 |
"id": task_cfg.name,
|
| 836 |
+
"name": task_cfg.name,
|
| 837 |
"difficulty": task_cfg.difficulty,
|
| 838 |
"description": f"Episode length: {task_cfg.episode_length} posts. Policy mode: {task_cfg.policy_mode.value}.",
|
| 839 |
"grader_id": task_cfg.grader_id,
|
|
|
|
| 846 |
"""Returns the list of graders available in the environment for discovery."""
|
| 847 |
return _list_graders()
|
| 848 |
|
| 849 |
+
|
| 850 |
+
@app.get("/grader", tags=["🤖 Automated Benchmarking"])
|
| 851 |
+
def grader_score():
|
| 852 |
+
"""Returns the grader score for the current (or most recent) episode.
|
| 853 |
+
|
| 854 |
+
The Scaler / OpenEnv hackathon validator calls this endpoint after running
|
| 855 |
+
an episode to obtain the final score. If no episode has been run yet a
|
| 856 |
+
minimal default score is returned.
|
| 857 |
+
"""
|
| 858 |
+
# Use the environment's last episode info to compute the score
|
| 859 |
+
if env.episode_history:
|
| 860 |
+
task = env.current_task
|
| 861 |
+
if task is not None:
|
| 862 |
+
grader_inst = get_grader(task.grader_id)
|
| 863 |
+
score = grader_inst.grade(env.episode_history)
|
| 864 |
+
else:
|
| 865 |
+
score = grade_episode(env.episode_history, use_fairness=False)
|
| 866 |
+
else:
|
| 867 |
+
score = 0.001
|
| 868 |
+
return {"score": score}
|
| 869 |
+
|
| 870 |
@app.post("/evaluate", tags=["🧪 Interactive Lab"], summary="Test Model Logic (XAI Insight)")
|
| 871 |
async def evaluate_text(
|
| 872 |
req: EvaluateRequest,
|