Spaces:
Sleeping
Sleeping
File size: 13,095 Bytes
ebae6ab 0e4f105 ebae6ab 0e4f105 ebae6ab dfa9070 09f7d63 ebae6ab dfa9070 09f7d63 ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab 98b952a ebae6ab cfe83fc | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | """
Rollout function bridging TRL's GRPOTrainer to the live ViralScriptEnv.
Each call:
1. Parses episode config from the prompt metadata header
2. Resets env with that config (live environment — not a static dataset)
3. Generates an action via the model (JSON)
4. Steps through the env for up to max_steps
5. Returns completions and final episode rewards
"""
import json
import re
import sys
from pathlib import Path
from typing import List, Tuple
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from viral_script_engine.environment.env import ViralScriptEnv
_FALLBACK_ACTION = {
"action_type": "hook_rewrite",
"target_section": "hook",
"instruction": "Rewrite the hook to open with a strong immediate claim.",
"critique_claim_id": "C1",
"reasoning": "Default fallback when model output is not valid JSON.",
}
_VALID_ACTIONS = {"hook_rewrite", "section_reorder", "cultural_ref_sub", "cta_placement"}
ARBITRATOR_SYSTEM = (
"You are an expert content strategist acting as an Arbitrator in a script improvement debate.\n"
"Before choosing your action, you must reason through the debate explicitly.\n\n"
"AVAILABLE ACTIONS: hook_rewrite | section_reorder | cultural_ref_sub | cta_placement\n\n"
"OUTPUT FORMAT (JSON only, in this exact order):\n"
"{\n"
' "priority_assessment": "which critique is most urgent and why — one sentence",\n'
' "conflict_check": "does acting on this critique risk harming any other reward signal? yes/no + reason",\n'
' "defender_consideration": "is the Defender\'s flagged concern relevant to this decision? yes/no + reason",\n'
' "action_type": "...",\n'
' "target_section": "...",\n'
' "instruction": "...",\n'
' "critique_claim_id": "...",\n'
' "reasoning": "..."\n'
"}"
)
def _format_observation_prompt(obs: dict, step_num: int, max_steps: int) -> str:
current_script = obs.get("current_script", "")
region = obs.get("region", "")
platform = obs.get("platform", "")
niche = obs.get("niche", "")
rc = obs.get("reward_components", {})
r1 = rc.get("r1_hook_strength") or 0.0
r2 = rc.get("r2_coherence") or 0.0
r3 = rc.get("r3_cultural_alignment", "N/A")
r4 = rc.get("r4_debate_resolution", "N/A")
r5 = rc.get("r5_defender_preservation", "N/A")
debate = obs.get("debate_history", [])
critic_text = "None"
defender_text = "None"
if debate:
last = debate[-1]
claims = last.get("critic_claims", [])
critic_text = "\n".join(
f"- [{c.get('claim_id','?')}] {c.get('claim_text','')} (severity: {c.get('severity','')})"
for c in claims
) or "None"
df = last.get("defender_response") or {}
if df:
defender_text = (
f"Core strength: {df.get('core_strength_quote','')}\n"
f"Defense: {df.get('defense_argument','')}\n"
f"Flagged claims: {df.get('flagged_critic_claims', [])}"
)
# Phase 8: include creator profile in prompt
profile = obs.get("creator_profile") or {}
profile_section = ""
if profile:
profile_section = (
f"\nCREATOR PROFILE:\n"
f"Tier: {profile.get('tier', 'unknown')} ({profile.get('follower_count', '?')} followers)\n"
f"Posting frequency: {profile.get('posting_frequency', 'unknown')}\n"
f"Recurring weak points: {profile.get('past_weak_points', [])}\n"
f"Voice: {profile.get('voice_descriptors', [])}\n"
f"Niche maturity: {profile.get('niche_maturity', 'unknown')}\n"
)
# Phase 11: include creator history context
history_context = obs.get("history_context") or "First session — no history available."
history_section = f"\nCREATOR HISTORY:\n{history_context}\n"
return (
f"<|system|>\n{ARBITRATOR_SYSTEM}\n<|end|>\n\n"
f"<|user|>\n"
f"CURRENT SCRIPT:\n{current_script}\n\n"
f"REGION: {region} | PLATFORM: {platform} | NICHE: {niche}\n\n"
f"{profile_section}"
f"{history_section}"
f"CRITIC CLAIMS:\n{critic_text}\n\n"
f"DEFENDER RESPONSE:\n{defender_text}\n\n"
f"CURRENT REWARDS: R1={r1:.2f} R2={r2:.2f} R3={r3} R4={r4} R5={r5}\n"
f"STEP: {step_num}/{max_steps}\n\n"
"Choose your action:\n<|end|>"
)
def _extract_json_action(text: str) -> dict:
text = text.strip()
# strip markdown fences
text = re.sub(r"^```(?:json)?", "", text).strip()
text = re.sub(r"```$", "", text).strip()
# find first {...}
match = re.search(r"\{.*?\}", text, re.DOTALL)
if match:
try:
action = json.loads(match.group())
if action.get("action_type") in _VALID_ACTIONS:
return action
except json.JSONDecodeError:
pass
return _FALLBACK_ACTION.copy()
def _model_generate(model, tokenizer, prompt: str, max_new_tokens: int = 256) -> str:
"""
Generate text from the model. Works with HuggingFace-style models.
Falls back gracefully if model has no standard generate() (e.g., mock models).
"""
if hasattr(model, "generate") and hasattr(tokenizer, "encode"):
import torch
inputs = tokenizer(prompt, return_tensors="pt")
input_ids = inputs["input_ids"]
if hasattr(model, "device"):
input_ids = input_ids.to(model.device)
with torch.no_grad():
outputs = model.generate(
input_ids,
max_new_tokens=max_new_tokens,
temperature=0.8,
top_p=0.9,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
new_tokens = outputs[0][input_ids.shape[-1]:]
return tokenizer.decode(new_tokens, skip_special_tokens=True)
elif callable(model):
return model(prompt)
else:
raise ValueError(f"Model type {type(model)} is not supported.")
def build_rollout_fn(
env: ViralScriptEnv,
max_steps: int = 5,
max_new_tokens: int = 256,
):
"""
Returns a reward function compatible with TRL 0.15+ GRPOTrainer.
TRL 0.15+ handles generation internally and calls reward functions as:
reward_fn(completions, prompts=None, **kwargs) -> List[float]
Each completion is parsed for a JSON action which is stepped through the
live ViralScriptEnv to produce a scalar reward.
"""
def rollout_fn(
completions: List[str],
prompts: List[str] = None,
**kwargs,
) -> List[float]:
rewards: List[float] = []
_prompts = prompts or [""] * len(completions)
for prompt, completion in zip(_prompts, completions):
config = _parse_episode_config(prompt)
if config:
obs, _ = env.reset_from_config(config)
else:
obs, _ = env.reset()
action = _extract_json_action(completion)
episode_reward = 0.0
terminated = False
truncated = False
# Run up to max_steps using the single generated completion as the action
for step in range(max_steps):
try:
obs, reward, terminated, truncated, info = env.step(
action, raw_output=completion
)
episode_reward = reward
except Exception:
terminated = True
if terminated or truncated:
break
rewards.append(episode_reward)
return rewards
return rollout_fn
def _parse_episode_config(prompt: str) -> dict:
"""Extract embedded episode config JSON from a prompt string."""
match = re.search(r"##EPISODE_CONFIG##\s*(\{.*?\})\s*##END_CONFIG##", prompt, re.DOTALL)
if match:
try:
return json.loads(match.group(1))
except json.JSONDecodeError:
pass
return {}
def build_training_prompts(tier: str, curriculum_dir: str = None) -> List[str]:
"""
Load a curriculum tier JSONL and convert to prompt strings with embedded episode configs.
Used by train_grpo.py to build the training dataset.
"""
if curriculum_dir is None:
curriculum_dir = Path(__file__).parent.parent / "data" / "curriculum"
else:
curriculum_dir = Path(curriculum_dir)
tier_file = curriculum_dir / f"{tier}_tier.jsonl"
if not tier_file.exists():
raise FileNotFoundError(
f"Curriculum file not found: {tier_file}\n"
"Run data/curriculum/build_curriculum.py first."
)
prompts = []
with open(tier_file, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
config = json.loads(line)
prompt = _config_to_prompt(config)
prompts.append(prompt)
return prompts
def _config_to_prompt(config: dict) -> str:
"""Convert an episode config into a training prompt with embedded config header."""
config_json = json.dumps({
"script_text": config["script_text"],
"region": config["region"],
"platform": config["platform"],
"niche": config["niche"],
"difficulty": config["difficulty"],
"script_id": config["script_id"],
})
header = f"##EPISODE_CONFIG## {config_json} ##END_CONFIG##"
return (
f"{header}\n\n"
f"<|system|>\n{ARBITRATOR_SYSTEM}\n<|end|>\n\n"
f"<|user|>\n"
f"CURRENT SCRIPT:\n{config['script_text']}\n\n"
f"REGION: {config['region']} | PLATFORM: {config['platform']} | NICHE: {config['niche']}\n\n"
f"DOMINANT FLAW: {config.get('dominant_flaw', 'unknown')}\n"
f"CURRICULUM NOTES: {config.get('curriculum_notes', '')}\n\n"
"Choose your action:\n<|end|>"
)
# ---------------------------------------------------------------------------
# Phase 10 — A/B rollout function
# ---------------------------------------------------------------------------
def _format_ab_observation_prompt(state: dict, max_steps: int) -> str:
"""Format the A/B observation for the Arbitrator prompt."""
traj_a = state.get("trajectory_a", {})
traj_b = state.get("trajectory_b", {})
delta = state.get("delta", 0.0)
step_num = state.get("step_num", 1)
def _rc_summary(rc: dict) -> str:
return (
f"R1={rc.get('r1_hook_strength') or 0.0:.2f} "
f"R2={rc.get('r2_coherence') or 0.0:.2f} "
f"R3={rc.get('r3_cultural_alignment') or 0.0:.2f} "
f"Total={rc.get('total') or 0.0:.2f}"
)
rc_a = traj_a.get("reward_components", {})
rc_b = traj_b.get("reward_components", {})
return (
f"<|system|>\n{ARBITRATOR_SYSTEM}\n<|end|>\n\n"
f"<|user|>\n"
f"TRAJECTORY A (Critic-first approach):\n"
f"Current script: {traj_a.get('current_script', '')}\n"
f"Rewards so far: {_rc_summary(rc_a)} Cumulative={traj_a.get('cumulative_reward', 0.0):.3f}\n\n"
f"TRAJECTORY B (Defender-first approach):\n"
f"Current script: {traj_b.get('current_script', '')}\n"
f"Rewards so far: {_rc_summary(rc_b)} Cumulative={traj_b.get('cumulative_reward', 0.0):.3f}\n\n"
f"Delta (A - B): {delta:.3f}\n"
f"Step: {step_num}/{max_steps}\n\n"
"Choose your next action (applied to BOTH trajectories):\n<|end|>"
)
def build_ab_rollout_fn(
ab_env,
max_steps: int = 5,
max_new_tokens: int = 256,
):
"""
Rollout function for the A/B environment.
The prompt includes both trajectory states so the Arbitrator can see
how the two paths diverge and learn which starting action leads to
better cumulative outcomes.
"""
def rollout_fn(
prompts: List[str],
model,
tokenizer,
) -> Tuple[List[str], List[float]]:
completions: List[str] = []
rewards: List[float] = []
for prompt in prompts:
state = ab_env.reset()
episode_parts: List[str] = []
episode_reward = 0.0
terminated = False
for step in range(max_steps - 1): # step 1 is forced; free steps = max_steps-1
obs_prompt = _format_ab_observation_prompt(state, max_steps)
full_prompt = prompt + "\n\n" + obs_prompt
raw_output = _model_generate(model, tokenizer, full_prompt, max_new_tokens)
action = _extract_json_action(raw_output)
episode_parts.append(raw_output)
try:
state, episode_reward, terminated, _, _ = ab_env.step(action)
except Exception:
terminated = True
if terminated:
break
if not terminated:
episode_reward = ab_env.reward()
completions.append("\n".join(episode_parts))
rewards.append(episode_reward)
return completions, rewards
return rollout_fn
|