File size: 890 Bytes
dadf6a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import sys
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = sys.argv[1] if len(sys.argv) > 1 else "Joaoffg/SHARE-4B-Reverse"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, device_map="auto")
text = "The correlation between social media use and life satisfaction was negative."
# The model reads and writes right-to-left: reverse the characters before tokenizing...
inputs = tokenizer(text[::-1], return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=120, do_sample=True, temperature=0.8, top_p=0.95, repetition_penalty=1.2)
# ...and reverse the generated text back. It is what the model thinks came *before* `text`.
before = tokenizer.decode(output[0, inputs.input_ids.shape[1]:], skip_special_tokens=True)[::-1]
print(before + text)
|