Spaces:
Running on Zero

File size: 2,196 Bytes
09f097e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""Acceptance for the two-layer Space: Kira 1/2/3 hours (/solve, expect program layer 150/270/390), Kira 2 hours with
/vary (program layer on both), 2 random dataset questions (/vary) and 1 typed question (/vary).
    python -B tools/accept_layers.py out.json"""
import json
import re
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 {h} 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?")
TYPED = ("Maria has 3 boxes of pencils with 12 pencils in each box. She gives 9 pencils to her friend. "
         "How many pencils does Maria have left?")


def info_lines(md):
    if not md or "### 3." not in md:
        return []
    return [x for x in md.split("### 3.")[1].split("```")[1].strip().split("\n") if x.strip()]


def layer(md):
    m = re.search(r"\*\*走哪一層:(.*?)\*\*(.*)", md or "")
    return (m.group(1) + m.group(2)).strip() if m else ""


def run(q, info, api):
    t0 = time.time()
    ans, t1, t2 = c.predict(q, api_name=api)
    r = {"抽題": info, "api": api, "題目": q, "回答框": ans, "層": layer(t1), "資訊句": info_lines(t1),
         "換數字層": layer(t2), "換數字資訊句": info_lines(t2), "耗秒_含網路": round(time.time() - t0, 1)}
    print(json.dumps(r, ensure_ascii=False, indent=1), flush=True)
    return r


runs = [run(KIRA.format(h=h), f"Kira {h}", "/solve") for h in ("1 hour", "2 hours", "3 hours")]
runs.append(run(KIRA.format(h="2 hours"), "Kira 2 hours 換數字", "/vary"))
got = 0
for _ in range(6):
    if got == 2:
        break
    q, info, _ = c.predict("隨機抽題型 Random type", api_name="/random")
    r = run(q, info, "/vary")
    runs.append(r)
    got += not r["回答框"].startswith("換數字失敗")
runs.append(run(TYPED, "typed", "/vary"))
json.dump(runs, open(sys.argv[1], "w", encoding="utf-8"), ensure_ascii=False, indent=1)