lingo-bridge / build_examples_cache.py
Iona401's picture
Publish Lingo Bridge
7135dbd verified
Raw
History Blame Contribute Delete
1.4 kB
"""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()