Views
No views yet
1from peft import AutoPeftModelForCausalLM
2from transformers import AutoTokenizer
3
4model_name = "thefrigidliquidation/lightnovel-translate-Qwen2.5-32B"
5model = AutoPeftModelForCausalLM.from_pretrained(model_name)
6tokenizer = AutoTokenizer.from_pretrained(model_name)1<|im_start|>system
2Translate this text from Japanese to English.<|im_end|>
3<|im_start|>user
4{prompt}<|im_end|>
5<|im_start|>assistant1<|im_start|>system
2Translate this text from Japanese to English.<|im_end|>
3<|im_start|>user
4<GLOSSARY>
5マイン : Myne
6</GLOSSARY>
7マイン、ルッツが迎えに来たよ<|im_end|>
8<|im_start|>assistant
9Myne, Lutz is here to take you home.clean_string function that replaces some unicode characters
with ASCII equivalents. Failure to do this may cause issues.1import ftfy
2
3FTFY_ADDITIONAL_MAP = {
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
30def clean_string(text: str, strip: bool = True) -> str:
31 config = ftfy.TextFixerConfig(normalization="NFC")
32 s = ftfy.fix_text(text, config=config)
33 s = "\n".join((x.strip() if strip else x.rstrip()) for x in s.splitlines())
34 for b, g in FTFY_ADDITIONAL_MAP.items():
35 s = s.replace(b, g)
36 return sJapanese term : English term inside <GLOSSARY></GLOSSARY> tags.マイン translated as Myne you can construct the input prompt with:1glossary = [
2 {"ja": "マイン", "en": "Myne"},
3]
4chapter_text = "マイン、ルッツが迎えに来たよ"
5
6def make_glossary_str(glossary: list[dict[str, str]]) -> str:
7 if glossart is None or len(glossary) == 0:
8 return ""
9 unique_glossary = {(term['ja'], term['en']) for term in glossary}
10 terms = "\n".join([f"{ja} : {en}" for ja, en in unique_glossary])
11 return f"<GLOSSARY>\n{terms}\n</GLOSSARY>\n"
12
13user_prompt = f"{make_glossary_str(glossary)}{clean_string(chapter_text)}"1<GLOSSARY>
2マイン : Myne
3</GLOSSARY>
4マイン、ルッツが迎えに来たよ