|
Download README.md from amelkhoadry/optimized-bert-model: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/amelkhoadry/optimized-bert-model/resolve/main/README.md
- Command line
-
hf download hf://amelkhoadry/optimized-bert-model/README.md
-
curl -L -o README.md https://huggingface.co/amelkhoadry/optimized-bert-model/resolve/main/README.md
3.56 kB
============================================================
SIMPLE EXAMPLE: How to Use Your Trained Model
============================================================
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Step 1: Load the model and tokenizer from the local directory
# (This assumes you ran Cell 18 earlier to save the model)
model_path = "optimized-bert-model"
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
# Step 2: Put model in evaluation mode
model.eval()
# Step 3: Test on simple examples using a helper function
def predict_paraphrase(sentence1, sentence2):
"""
Predicts whether two sentences are paraphrases and returns prediction and confidence.
"""
inputs = tokenizer(sentence1, sentence2, return_tensors="pt",
truncation=True, padding=True, max_length=128)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
prediction = torch.argmax(logits, dim=1).item()
confidence = torch.softmax(logits, dim=1)[0].max().item()
return prediction, confidence
def display_result(example_idx, sentence1, sentence2):
prediction, confidence = predict_paraphrase(sentence1, sentence2)
print("="*60)
print(f"EXAMPLE {example_idx} - Are these paraphrases?")
print("="*60)
print(f"Sentence 1: {sentence1}")
print(f"Sentence 2: {sentence2}")
print(f"Prediction: {'YES (paraphrases)' if prediction == 1 else 'NO (not paraphrases)'}")
print(f"Confidence: {confidence:.4f}")
print()
# Example 1: Two sentences that ARE paraphrases
sentence1_1 = "The cat is sleeping on the mat"
sentence2_1 = "The cat is napping on the mat"
display_result(1, sentence1_1, sentence2_1)
# Example 2: Two sentences that are NOT paraphrases
sentence1_2 = "The dog is barking loudly"
sentence2_2 = "I love eating pizza"
display_result(2, sentence1_2, sentence2_2)
print("="*60)
# -----------------------
# Try your own examples!
# -----------------------
# Uncomment and edit the sentences below to test your own custom examples:
# user_sentence1 = "Your first sentence here."
# user_sentence2 = "Your second sentence here."
# display_result("USER", user_sentence1, user_sentence2)