# -*- coding: utf-8 -*- """Acceptance for 🔀 換數字再讀: the Kira question (typed) + 2 random dataset questions via /vary. python -B tools/accept_vary.py out.json""" import json import sys import time from gradio_client import Client from huggingface_hub import get_token c = Client("Bigbearbo/word-problem-reader", hf_token=get_token(), verbose=False) KIRA = ("Three train stations are 2 hours apart from one another. Kira travels from the first station to the third, " "taking a 30 minutes break at the second station. What's the total time, in minutes, that Kira takes to travel " "between the first and third station?") def info_lines(md): if "### 3." not in md: return [] return [x for x in md.split("### 3.")[1].split("```")[1].strip().split("\n") if x.strip()] def run(q, info): t0 = time.time() ans, t1, t2 = c.predict(q, api_name="/vary") r = {"抽題": info, "題目": q, "回答框": ans, "原題資訊句": info_lines(t1), "換數字資訊句": info_lines(t2), "耗秒_含網路": round(time.time() - t0, 1)} print(json.dumps(r, ensure_ascii=False, indent=1), flush=True) return r runs = [run(KIRA, "typed (Kira)")] got = 0 for _ in range(6): if got == 2: break q, info, _ = c.predict("隨機抽題型 Random type", api_name="/random") r = run(q, info) runs.append(r) got += not r["回答框"].startswith("換數字失敗") json.dump(runs, open(sys.argv[1], "w", encoding="utf-8"), ensure_ascii=False, indent=1)