"""Answer the example requests with MANTA-4B (one GPU with about 16 GB). python example.py # examples/text_request.json and image_request.json python example.py --request my_request.json Prints, per question, the option probabilities (most probable first) and the answer. """ from __future__ import annotations import argparse import json import sys from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) def main(argv=None) -> int: ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--request", nargs="*", type=Path, default=[HERE / "examples" / "text_request.json", HERE / "examples" / "image_request.json"]) ap.add_argument("--release", default=str(HERE), help="the release folder or its Hub repository id") ap.add_argument("--device", default="cuda") ap.add_argument("--option-order", default="eval", choices=("eval", "given", "sorted")) args = ap.parse_args(argv) from manta import Manta model = Manta.from_pretrained(args.release, device=args.device) for path in args.request: request = json.loads(path.read_text()) result = model.predict(request, option_order=args.option_order) print(f"\n{path.name} ({result['prompt_tokens']} prompt tokens)") for qid, answer in result["questions"].items(): ranked = sorted(answer["probabilities"].items(), key=lambda kv: -kv[1]) shown = ", ".join(f"{option} {p:.3f}" for option, p in ranked) print(f" {qid} ({answer['type']}): answer {answer['answer']} | {shown}") return 0 if __name__ == "__main__": sys.exit(main())