| import asyncio |
| import os |
| from typing import Any, Dict |
|
|
| import requests |
| from fastapi import FastAPI, Request |
|
|
| from task2_rubric_utils import ( |
| approx_token_count, |
| coerce_extra_info, |
| compute_weighted_score, |
| extract_proposed_plan, |
| parse_rubric_items, |
| ) |
|
|
|
|
| app = FastAPI() |
|
|
|
|
| def has_required_sections(plan_text: str) -> bool: |
| lowered = (plan_text or "").lower() |
| required_sections = ( |
| "objective:", |
| "baseline setup:", |
| "variants:", |
| "fixed protocols & metrics:", |
| ) |
| return all(section in lowered for section in required_sections) |
|
|
|
|
| def compute_rule_penalty(raw_response: str, plan_text: str) -> Dict[str, float]: |
| format_penalty = 0.0 |
| if "<Proposed_Plan>" not in (raw_response or ""): |
| format_penalty += 0.25 |
| if not has_required_sections(plan_text): |
| format_penalty += 0.25 |
|
|
| length_threshold = int(os.environ.get("TASK2_LENGTH_THRESHOLD", "1024")) |
| length_penalty_rate = float(os.environ.get("TASK2_LENGTH_PENALTY_RATE", "0.05")) |
| token_count = approx_token_count(plan_text) |
| overlong_tokens = max(0, token_count - length_threshold) |
| length_penalty = (overlong_tokens / 100.0) * length_penalty_rate if overlong_tokens > 0 else 0.0 |
|
|
| return { |
| "format_penalty": float(format_penalty), |
| "length_penalty": float(length_penalty), |
| "token_count": float(token_count), |
| } |
|
|
|
|
| def call_task2_judge(payload: Dict[str, Any]) -> Dict[str, Any]: |
| judge_url = os.environ.get("TASK2_JUDGE_URL", "http://127.0.0.1:8001/evaluate_task2") |
| response = requests.post(judge_url, json=payload, timeout=int(os.environ.get("TASK2_JUDGE_TIMEOUT", "600"))) |
| response.raise_for_status() |
| return response.json() |
|
|
|
|
| def build_reward_response(raw_request: Dict[str, Any]) -> Dict[str, Any]: |
| extra_info = coerce_extra_info(raw_request.get("extra_info")) |
| raw_response = raw_request.get("response_str", "") |
| if isinstance(raw_response, list): |
| raw_response = raw_response[0] if raw_response else "" |
|
|
| plan_text = extract_proposed_plan(raw_response) |
| rubric_items = parse_rubric_items(extra_info.get("rubric", "")) |
| if not rubric_items: |
| return { |
| "score": 0.0, |
| "rubric_score": 0.0, |
| "format_penalty": 0.0, |
| "length_penalty": 0.0, |
| "token_count": 0.0, |
| "error": "no_rubric_items", |
| } |
|
|
| judge_payload = { |
| "paper_context": extra_info.get("paper_context", ""), |
| "goal": extra_info.get("goal", ""), |
| "generated_design": plan_text, |
| "rubric_items": rubric_items, |
| } |
| judge_result = call_task2_judge(judge_payload) |
| eval_items = judge_result.get("eval_items", []) |
| rubric_score = compute_weighted_score(eval_items) |
| if rubric_score is None: |
| rubric_score = 0.0 |
|
|
| penalties = compute_rule_penalty(raw_response=raw_response, plan_text=plan_text) |
| total_score = max(0.0, float(rubric_score) - penalties["format_penalty"] - penalties["length_penalty"]) |
|
|
| return { |
| "score": total_score, |
| "rubric_score": float(rubric_score), |
| "format_penalty": penalties["format_penalty"], |
| "length_penalty": penalties["length_penalty"], |
| "token_count": penalties["token_count"], |
| "eval_items": eval_items, |
| "raw_judge_output": judge_result.get("raw_judge_output", ""), |
| } |
|
|
|
|
| @app.post("/get_reward_task2") |
| async def get_reward_task2(request: Request): |
| json_data = await request.json() |
| result = await asyncio.to_thread(build_reward_response, json_data) |
| return result |
|
|
|
|
| @app.post("/get_reward2") |
| async def get_reward2(request: Request): |
| json_data = await request.json() |
| result = await asyncio.to_thread(build_reward_response, json_data) |
| return result |
|
|
|
|
| @app.get("/health") |
| async def health(): |
| return {"status": "ok"} |
|
|
|
|
| if __name__ == "__main__": |
| import uvicorn |
|
|
| port = int(os.environ.get("TASK2_REWARD_PORT", "6009")) |
| uvicorn.run(app, host="0.0.0.0", port=port) |
|
|