File size: 1,401 Bytes
7135dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pre-compute the curated example translations once, so the "Surprise me"
button can load them instantly with NO LLM call at request time.

Run locally (needs the local LLM):  python build_examples_cache.py
Writes examples_cache.py (a module embedding the precomputed results as JSON).
"""
import json
import config
import examples
import llm
import translate


def key(text, src, tgt):
    return f"{src}|{tgt}|{text}"


def main():
    langs = set(config.LANGUAGES)
    print("LLM backend:", llm.backend())
    cache = {}
    for text, src, tgt in examples.EXAMPLES:
        if src not in langs or tgt not in langs:
            print("skip (unsupported lang):", src, "->", tgt)
            continue
        k = key(text, src, tgt)
        if k in cache:
            continue
        print(f"translating {src}->{tgt}: {text[:45]}")
        cache[k] = translate.progressive_translate(text, src, tgt)

    payload = json.dumps(cache, ensure_ascii=False)
    with open("examples_cache.py", "w", encoding="utf-8") as f:
        f.write('"""Auto-generated by build_examples_cache.py — do not edit by hand.\n')
        f.write('Precomputed example translations so the Surprise-me button needs no LLM.\n"""\n')
        f.write("import json\n\n")
        f.write("CACHE = json.loads(%r)\n" % payload)
    print(f"wrote examples_cache.py with {len(cache)} entries")


if __name__ == "__main__":
    main()