"""Minimal client for vinylo-e2b-lyrics GGUF via llama-server (same prompt and chunking as the Vinylo app). llama-server -m vinylo-e2b-lyrics-Q2_K-qat.gguf -c 4096 -ngl 99 --port 8080 python example.py --target ko lyrics.txt # one lyric line per text line echo "時計の針が 胸を突き" | python example.py --target en - Prints one JSON object per line: {"i": index, "src": original, "t": translation, "r": reading} ("copy": true when the line is already in the target language). Standard library only.""" import argparse, json, sys, urllib.request LANGS = {"ko": "Korean", "en": "English", "zh": "Simplified Chinese", "ja": "Japanese"} READING = {"ko": "Hangul (Korean 독음, e.g. 君の名は → 키미노 나와)", "ja": "Katakana", "zh": "Pinyin with tone marks for Chinese lines, otherwise Latin letters"} def system_prompt(code): name = LANGS[code] return f'''You translate song lyrics for language learners. Translate each numbered line into natural, idiomatic {name} ({code}). Rules: - Keep the meaning, tone and register of the song; prefer natural phrasing over literal word order. - Use the surrounding lines as context for pronouns, tense and slang. - Return exactly one entry per input index. Never merge or split lines. - If a line is already in {name}, return it with "copy": true and no translation. - For every translated line also give "r": how the ORIGINAL line sounds when sung, written in {READING.get(code, "Latin letters (romanization)")}. Transcribe the sound only; do not translate it. Leave "r" out when the original is already written in that script. - Do not censor the original meaning. Respond only with JSON: {{"lines":[{{"i":0,"t":"...","r":"..."}},{{"i":1,"copy":true}}]}}''' def translate_chunk(url, target, lines, start): user = "\n".join(f"{start + k}\t{t}" for k, t in enumerate(lines)) prompt = f"<|turn>system\n{system_prompt(target)}\n<|turn>user\n{user}\n<|turn>model\n" # server adds body = {"prompt": prompt, "temperature": 0, "top_k": 1, "n_predict": 1024, "stop": [""]} req = urllib.request.Request(url + "/completion", json.dumps(body).encode(), {"content-type": "application/json"}) text = json.load(urllib.request.urlopen(req, timeout=600))["content"] return {e["i"]: e for e in json.loads(text[text.find("{"):text.rfind("}") + 1])["lines"]} def main(): ap = argparse.ArgumentParser() ap.add_argument("file", help="lyrics file, one line per lyric line ('-' = stdin)") ap.add_argument("--target", required=True, choices=LANGS) ap.add_argument("--url", default="http://127.0.0.1:8080") ap.add_argument("--chunk", type=int, default=8, help="lines per request (the model was trained on 8)") a = ap.parse_args() src = [l.rstrip("\n") for l in (sys.stdin if a.file == "-" else open(a.file, encoding="utf-8")) if l.strip()] for s in range(0, len(src), a.chunk): got = translate_chunk(a.url, a.target, src[s:s + a.chunk], s) for i in range(s, min(s + a.chunk, len(src))): e = got.get(i, {}) print(json.dumps({"i": i, "src": src[i], **{k: v for k, v in e.items() if k != "i"}}, ensure_ascii=False)) if __name__ == "__main__": main()