Views
No views yet
quora-question-pairspip install transformers torch1from transformers import RobertaTokenizer, RobertaForSequenceClassification
2import torch
3
4device = "cuda" if torch.cuda.is_available() else "cpu"
5
6model_name = "AventIQ-AI/roberta-paraphrase-detection"
7tokenizer = RobertaTokenizer.from_pretrained(model_name)
8model = RobertaForSequenceClassification.from_pretrained(model_name).to(device)1def predict_paraphrase(sentence1, sentence2, threshold=0.96):
2 inputs = tokenizer(sentence1, sentence2, return_tensors="pt", padding=True, truncation=True).to(device)
3
4 with torch.no_grad():
5 outputs = model(**inputs)
6
7 logits = outputs.logits
8 probabilities = torch.nn.functional.softmax(logits, dim=-1)
9 predicted_class = torch.argmax(probabilities, dim=-1).item()
10 confidence = probabilities[0][predicted_class].item()
11
12 label_map = {0: "Not Duplicate", 1: "Duplicate"}
13
14 # Apply a slightly less strict threshold
15 if predicted_class == 1 and confidence < threshold:
16 return {"sentence1": sentence1, "sentence2": sentence2, "predicted_label": "Not Duplicate", "confidence": confidence}
17 else:
18 return {"sentence1": sentence1, "sentence2": sentence2, "predicted_label": label_map[predicted_class], "confidence": confidence}
19
20# 🔍 Test Example
21test_cases = [
22 ("The sun rises in the east.", "The east is where the sun rises."), # Duplicate
23 ("She enjoys playing the piano.", "She loves playing musical instruments."), # Duplicate
24 ("I had a great time at the party.", "The event was really fun."), # Duplicate
25
26 ("The sky is blue.", "Bananas are yellow."), # Not Duplicate
27 ("The capital of France is Paris.", "Berlin is the capital of Germany."), # Not Duplicate
28 ("I like reading books.", "She is going for a run."), # Not Duplicate
29]
30for sent1, sent2 in test_cases:
31 result = predict_paraphrase(sent1, sent2)
32 print(result).
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── model.safetensors/ # Quantized Model
├── README.md # Model documentation