Views
No views yet

This model is a fine-tuned version of SmolLM2-360M for generating concise, one-sentence summaries of model and dataset cards from the Hugging Face Hub.
<MODEL_CARD> or <DATASET_CARD> to the start of the card you want to summarize. The training data used the body of the model or dataset card (i.e., the part after the YAML, so you will likely get better results only by passing this part of the card.0.4 generates better results.1from transformers import AutoModelForCausalLM, AutoTokenizer
2from huggingface_hub import ModelCard
3
4card = ModelCard.load("davanstrien/Smol-Hub-tldr")
5
6# Load tokenizer and model
7tokenizer = AutoTokenizer.from_pretrained("davanstrien/Smol-Hub-tldr")
8model = AutoModelForCausalLM.from_pretrained("davanstrien/Smol-Hub-tldr")
9
10# Format input according to the chat template
11messages = [{"role": "user", "content": f"<MODEL_CARD>{card.text}"}]
12# Encode with the chat template
13inputs = tokenizer.apply_chat_template(
14 messages, add_generation_prompt=True, return_tensors="pt"
15)
16
17# Generate with stop tokens
18outputs = model.generate(
19 inputs,
20 max_new_tokens=60,
21 pad_token_id=tokenizer.pad_token_id,
22 eos_token_id=tokenizer.eos_token_id,
23 temperature=0.4,
24 do_sample=True,
25)
26
27input_length = inputs.shape[1]
28response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=False)
29
30# Extract just the summary part
31summary = response.split("<CARD_SUMMARY>")[-1].split("</CARD_SUMMARY>")[0]
32print(summary)
33>>> "The Smol-Hub-tldr model is a fine-tuned version of SmolLM2-360M designed to generate concise, one-sentence summaries of model and dataset cards from the Hugging Face Hub."</CARD_SUMMARY> (cooking some more with this...), so you can also use this as a stopping criterion when using pipeline inference.1from transformers import pipeline, StoppingCriteria, StoppingCriteriaList
2import torch
3
4
5class StopOnTokens(StoppingCriteria):
6 def __init__(self, tokenizer, stop_token_ids):
7 self.stop_token_ids = stop_token_ids
8 self.tokenizer = tokenizer
9
10 def __call__(
11 self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs
12 ) -> bool:
13 for stop_id in self.stop_token_ids:
14 if input_ids[0][-1] == stop_id:
15 return True
16 return False
17
18
19# Initialize pipeline
20pipe = pipeline("text-generation", "davanstrien/Smol-Hub-tldr")
21tokenizer = pipe.tokenizer
22
23# Get the token IDs for stopping
24stop_token_ids = [
25 tokenizer.encode("</CARD_SUMMARY>", add_special_tokens=True)[-1],
26 tokenizer.eos_token_id,
27]
28
29# Create stopping criteria
30stopping_criteria = StoppingCriteriaList([StopOnTokens(tokenizer, stop_token_ids)])
31
32# Generate with stopping criteria
33response = pipe(
34 messages,
35 max_new_tokens=50,
36 do_sample=True,
37 temperature=0.7,
38 stopping_criteria=stopping_criteria,
39 return_full_text=False,
40)
41
42# Clean up the response
43summary = response[0]["generated_text"]
44print(summary)
45>>> "This model is a fine-tuned version of SmolLM2-360M for generating concise, one-sentence summaries of model and dataset cards from the Hugging Face Hub."