1import torch
2from transformers import AutoModelForPreTraining, AutoProcessor
3
4REPO = "dartags/DanbotNL-2408-260m"
5
6processor = AutoProcessor.from_pretrained(
7 REPO,
8 trust_remote_code=True,
9 revision="827103c", # optional
10)
11model = AutoModelForPreTraining.from_pretrained(
12 REPO,
13 trust_remote_code=True,
14 revision="827103c", # optional
15 torch_dtype=torch.bfloat16
16)
17
18# Translate
19inputs = processor(
20 encoder_text="一人の猫耳の少女が座ってこっちを見ている。",
21 decoder_text=processor.decoder_tokenizer.apply_chat_template(
22 {
23 "aspect_ratio": "tall",
24 "rating": "general",
25 "length": "very_short",
26 "translate_mode": "exact",
27 },
28 tokenize=False,
29 ),
30 return_tensors="pt",
31)
32
33with torch.inference_mode():
34 outputs = model.generate(
35 **inputs.to(model.device),
36 do_sample=False,
37 eos_token_id=processor.decoder_tokenizer.convert_tokens_to_ids(
38 "</translation>"
39 ),
40 )
41translation = ", ".join(
42 tag
43 for tag in processor.batch_decode(
44 outputs[0, len(inputs.input_ids[0]) :],
45 skip_special_tokens=True,
46 )
47 if tag.strip() != ""
48)
49print("translation:", translation)
50# translation: 1girl, solo, looking at viewer, sitting, cat girl
51
52# Extend
53inputs = processor(
54 encoder_text="一人の猫耳の少女が座ってこっちを見ている。",
55 decoder_text=processor.decoder_tokenizer.apply_chat_template(
56 {
57 "aspect_ratio": "tall",
58 "rating": "general",
59 "length": "long",
60 "translate_mode": "approx",
61 "copyright": "",
62 "character": "",
63 "translation": translation,
64 },
65 tokenize=False,
66 ),
67 return_tensors="pt",
68)
69with torch.inference_mode():
70 outputs = model.generate(
71 **inputs.to(model.device),
72 do_sample=False,
73 eos_token_id=processor.decoder_tokenizer.convert_tokens_to_ids("</extension>"),
74 )
75extension = ", ".join(
76 tag
77 for tag in processor.batch_decode(
78 outputs[0, len(inputs.input_ids[0]) :],
79 skip_special_tokens=True,
80 )
81 if tag.strip() != ""
82)
83print("extension:", extension)
84# extension: simple background, white background, shirt, skirt, long sleeves, animal ears, closed mouth, ribbon, jacket, pantyhose, open clothes, blue eyes, brown hair, long hair, shoes, white shirt, full body, cat ears, black skirt, loafers