Views
No views yet
pip install transformers torch1from transformers import DistilBertForSequenceClassification, DistilBertTokenizerFast
2import torch
3
4# Load fine-tuned model
5model_path = "fine-tuned-model"
6model = DistilBertForSequenceClassification.from_pretrained(model_path)
7tokenizer = DistilBertTokenizerFast.from_pretrained(model_path)
8
9# Set model to evaluation and convert to half precision
10device = "cuda" if torch.cuda.is_available() else "cpu"
11model.to(device)
12model.eval()
13model.half()
14
15# Example input
16blog_post = "Today I went to the beach and had an amazing time with friends. The sunset was breathtaking!"
17
18# Tokenize input
19inputs = tokenizer(blog_post, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
20inputs = {k: v.half() if v.dtype == torch.float else v for k, v in inputs.items()}
21
22# Make prediction
23with torch.no_grad():
24 outputs = model(**inputs)
25
26predicted_class = torch.argmax(outputs.logits, dim=1).item()
27
28# Label mapping (example)
29label_mapping = {
30 0: "Author_A",
31 1: "Author_B",
32 2: "Author_C",
33 3: "Author_D",
34 4: "Author_E",
35 5: "Author_F",
36 6: "Author_G",
37 7: "Author_H",
38 8: "Author_I",
39 9: "Author_J"
40}
41
42predicted_author = label_mapping[predicted_class]
43print(f"Predicted Author: {predicted_author}")1quantized_model = torch.quantization.quantize_dynamic(
2 model, {torch.nn.Linear}, dtype=torch.qint8
3).
├── model/ # Contains the fine-tuned and quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary
├── model.safensors/ # Safetensors version of model weights
├── README.md # Documentation