File size: 2,119 Bytes
8387fae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent.parent))
import numpy as np
from collections import defaultdict
from env.discord_env import DiscordEnv
from env.wrappers import LagrangianRewardWrapper
from utils.toxicity_judge import ToxicityJudge
from utils.language_utils import detect_language
def run_loop_test():
print("Initializing environment...")
base_env = DiscordEnv()
env = LagrangianRewardWrapper(base_env)
obs, info = env.reset()
action_counts_per_lang = defaultdict(lambda: {0: 0, 1: 0, 2: 0, 3: 0})
total_steps = 1000
print(f"Running {total_steps} random steps...")
for step in range(total_steps):
# Use our custom action masks to only take valid random actions
valid_actions = np.where(env.action_masks())[0]
action = np.random.choice(valid_actions)
# We access the private language variable just for logging/testing, NOT for the agent
lang = env.env._current_language
action_counts_per_lang[lang][action] += 1
obs, reward, term, trunc, info = env.step(action)
if term or trunc:
obs, info = env.reset()
print("\n=== Action Distribution by Language (1000 steps) ===")
print(f"{'Lang':<6} | {'ALLOW':<6} | {'WARN':<6} | {'DELETE':<6} | {'BAN':<6}")
print("-" * 40)
for lang, counts in sorted(action_counts_per_lang.items()):
print(f"{lang:<6} | {counts[0]:<6} | {counts[1]:<6} | {counts[2]:<6} | {counts[3]:<6}")
def run_edge_cases():
print("\n=== Testing Edge Cases ===")
judge = ToxicityJudge()
edge_cases = [
"", # Empty string
"😊😂🔥", # Emoji only
"Hello amigo, como estas?", # Mixed language (Code-switching)
]
for text in edge_cases:
lang_res = detect_language(text)
score = judge.score_text(text)
print(f"Text: {repr(text):<30} | Lang: {lang_res.lang:<8} | Tox Score: {score:.3f}")
if __name__ == "__main__":
run_loop_test()
run_edge_cases() |