Download prompts/render.py from fyrisk159/RVG-Bench: direct link, hf CLI and curl.
- Browser
- Download file 4.38 kB
-
https://huggingface.co/datasets/fyrisk159/RVG-Bench/resolve/main/prompts/render.py
- Command line
-
hf download hf://datasets/fyrisk159/RVG-Bench/prompts/render.py
-
curl -L -o render.py https://huggingface.co/datasets/fyrisk159/RVG-Bench/resolve/main/prompts/render.py
4.38 kB
| """Render verified RVG-Bench task templates without running a model.""" | |
| from __future__ import annotations | |
| import argparse | |
| from pathlib import Path | |
| import re | |
| import sys | |
| TEMPLATE_ROOT = Path(__file__).resolve().parent / "templates" | |
| DEFAULT_FORMATS = {"omni": "seconds", "vl": "mmss"} | |
| MODES = ("cot", "direct", "two_step", "ground") | |
| SLOT = re.compile(r"\{(question|reference_answer|answer_desc)\}") | |
| def render_prompt( | |
| *, | |
| profile: str, | |
| mode: str, | |
| question: str, | |
| time_format: str | None = None, | |
| stage: int | None = None, | |
| reference_answer: str | None = None, | |
| answer_desc: str | None = None, | |
| ) -> str: | |
| """Return task text; the caller supplies media through its model interface. | |
| ``reference_answer`` is accepted only for oracle Ground. Two-step stage 2 | |
| takes the MODEL-GENERATED ``answer_desc``, never the reference answer. | |
| No model SDK, media decoder, network call, or chat wrapper is involved. | |
| """ | |
| if profile not in DEFAULT_FORMATS: | |
| raise ValueError("profile must be 'omni' or 'vl'") | |
| if mode not in MODES: | |
| raise ValueError(f"mode must be one of {MODES}") | |
| fmt = time_format or DEFAULT_FORMATS[profile] | |
| if fmt not in ("seconds", "mmss"): | |
| raise ValueError("time_format must be 'seconds' or 'mmss'") | |
| if not isinstance(question, str) or not question.strip(): | |
| raise ValueError("question must be a non-empty string") | |
| if mode == "two_step": | |
| if stage not in (1, 2): | |
| raise ValueError("two_step requires stage=1 or stage=2") | |
| name = f"two_step_{stage}" | |
| else: | |
| if stage is not None: | |
| raise ValueError("stage is only valid for two_step") | |
| name = mode | |
| if reference_answer is not None and mode != "ground": | |
| raise ValueError("reference_answer is only allowed for oracle Ground") | |
| if answer_desc is not None and (mode != "two_step" or stage != 2): | |
| raise ValueError("answer_desc is only allowed for two_step stage 2") | |
| values = { | |
| "question": question, | |
| "reference_answer": reference_answer, | |
| "answer_desc": answer_desc, | |
| } | |
| template = (TEMPLATE_ROOT / profile / fmt / f"{name}.txt").read_text(encoding="utf-8") | |
| for field in set(SLOT.findall(template)): | |
| value = values[field] | |
| if not isinstance(value, str) or not value.strip(): | |
| raise ValueError(f"{field} must be a non-empty string for this template") | |
| # One pass: braces inside user questions/answers remain literal text. | |
| return SLOT.sub(lambda match: values[match.group(1)], template) | |
| def main(argv: list[str] | None = None) -> int: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--profile", choices=tuple(DEFAULT_FORMATS), required=True) | |
| parser.add_argument("--mode", choices=MODES, default="cot") | |
| parser.add_argument("--time-format", choices=("seconds", "mmss"), | |
| help="Default: seconds for omni; mmss for vl.") | |
| parser.add_argument("--stage", type=int, choices=(1, 2)) | |
| question = parser.add_mutually_exclusive_group(required=True) | |
| question.add_argument("--question") | |
| question.add_argument("--question-file", type=Path, help="UTF-8 text file") | |
| parser.add_argument("--reference-answer", help="GT text answer, oracle Ground only") | |
| parser.add_argument("--answer-desc", help="Model-generated description, two_step stage 2 only") | |
| parser.add_argument("--output", type=Path, help="Save task text instead of printing it") | |
| args = parser.parse_args(argv) | |
| try: | |
| question_text = (args.question_file.read_text(encoding="utf-8") | |
| if args.question_file is not None else args.question) | |
| prompt = render_prompt( | |
| profile=args.profile, mode=args.mode, question=question_text, | |
| time_format=args.time_format, stage=args.stage, | |
| reference_answer=args.reference_answer, answer_desc=args.answer_desc, | |
| ) | |
| if args.output: | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| args.output.write_text(prompt, encoding="utf-8") | |
| else: | |
| sys.stdout.write(prompt) | |
| if not prompt.endswith("\n"): | |
| sys.stdout.write("\n") | |
| except (OSError, ValueError) as exc: | |
| parser.error(str(exc)) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |