Views
No views yet
pip install datasets transformers seqeval torch --quiet1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3
4# Load tokenizer and model
5model = "roberta-base"
6tokenizer = AutoTokenizer.from_pretrained(model)
7model = AutoModelForSequenceClassification.from_pretrained(model)
8# Define test sentences
9sentences = [
10 "Barack Obama was born in Hawaii.",
11 "Elon Musk founded SpaceX and Tesla.",
12 "Apple is headquartered in Cupertino, California."
13]
14
15for sentence in sentences:
16 tokens = tokenizer(sentence, return_tensors="pt", truncation=True, is_split_into_words=False).to(device)
17 with torch.no_grad():
18 outputs = model(**tokens)
19 logits = outputs.logits
20 predictions = torch.argmax(logits, dim=2)
21 predicted_labels = predictions[0].cpu().numpy()
22 tokens_decoded = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0])
23 print(f"Sentence: {sentence}")
24 for token, label_id in zip(tokens_decoded, predicted_labels):
25 label = label_list[label_id]
26 if token.startswith("Ġ") or not token.startswith("▁"):
27 token = token.replace("Ġ", "")
28 if label != "O":
29 print(f"{token}: {label}")
30 print("\n" + "-"*50 + "\n")epochTrainer APImodel.to(dtype=torch.float16) to reduce model size and speed up inference.1.
2├── quantized-model/ # Directory containing trained model artifacts
3│ ├── config.json
4│ ├── merges.txt
5│ ├── model.safetensors # (May appear as 'model' in UI)
6│ ├── special_tokens_map.json
7│ ├── tokenizer.json
8│ ├── tokenizer_config.json
9│ └── vocab.json
10├── README.md