Download example.py from Joaoffg/SHARE-4B-reverse: direct link, hf CLI and curl.
- Browser
- Download file 890 Bytes
-
https://huggingface.co/Joaoffg/SHARE-4B-reverse/resolve/main/example.py
- Command line
-
hf download hf://Joaoffg/SHARE-4B-reverse/example.py
-
curl -L -o example.py https://huggingface.co/Joaoffg/SHARE-4B-reverse/resolve/main/example.py
890 Bytes
| 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) | |