Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4device = "cuda" if torch.cuda.is_available() else "cpu"
5model_id = "gokaygokay/SmolLM2-Prompt-Enhance"
6tokenizer_id = "HuggingFaceTB/SmolLM2-135M-Instruct"
7# Load model and tokenizer
8tokenizer = AutoTokenizer.from_pretrained(tokenizer_id )
9model = AutoModelForCausalLM.from_pretrained(model_id).to(device)
10
11# Model response generation functions
12def generate_response(model, tokenizer, instruction, device="cpu"):
13 """Generate a response from the model based on an instruction."""
14 messages = [{"role": "user", "content": instruction}]
15 input_text = tokenizer.apply_chat_template(
16 messages, tokenize=False, add_generation_prompt=True
17 )
18 inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
19 outputs = model.generate(
20 inputs, max_new_tokens=256, repetition_penalty=1.2
21 )
22 response = tokenizer.decode(outputs[0], skip_special_tokens=True)
23 return response
24
25def print_response(response):
26 """Print the model's response."""
27 print(f"Model response:")
28 print(response.split("assistant\n")[-1])
29 print("-" * 100)
30
31prompt = "cat"
32
33response = generate_response(model, tokenizer, prompt, device)
34print_response(response)
35
36# a gray cat with white fur and black eyes is in the center of an open window on a concrete floor.
37# The front wall has two large windows that have light grey frames behind them.
38# here is a small wooden door to the left side of the frame at the bottom right corner.
39# A metal fence runs along both sides of the image from top down towards the middle ground.
40# Behind the cats face away toward the camera's view it appears as if there is another cat sitting next to the one
41# they're facing forward against the glass surface above their head.1@software{aydogan2024smollm2_prompt_enhance,
2 author = {Aydoğan, Gökay},
3 title = {{SmolLM2-Prompt-Enhance}},
4 year = {2024},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/gokaygokay/SmolLM2-Prompt-Enhance},
7 note = {Model repository; cite the base model and upstream datasets as required.}
8}