Views
No views yet
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| 0 | 0.96 | 0.97 | 0.96 | 36,586 |
| 1 | 0.97 | 0.96 | 0.96 | 36,888 |
1import torch
2from transformers import GPT2Tokenizer, LlamaForSequenceClassification
3
4# Load the tokenizer and model
5model_path = "jatinmehra/smolLM-fined-tuned-for-PLAGAIRISM_Detection"
6tokenizer = GPT2Tokenizer.from_pretrained(model_path)
7model = LlamaForSequenceClassification.from_pretrained(model_path)
8model.eval()
9
10# Set device
11device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12model = model.to(device)
13
14# Function to preprocess and tokenize the input text
15def preprocess_text(text1, text2):
16 inputs = tokenizer(
17 text1, text2,
18 add_special_tokens=True,
19 max_length=128,
20 padding='max_length',
21 truncation=True,
22 return_tensors="pt"
23 )
24 return inputs
25
26# Dataset class
27class PlagiarismDataset(Dataset):
28 def __init__(self, text1, text2, tokenizer):
29 self.text1 = text1
30 self.text2 = text2
31 self.tokenizer = tokenizer
32
33 def __len__(self):
34 return len(self.text1)
35
36 def __getitem__(self, idx):
37 inputs = preprocess_text(self.text1[idx], self.text2[idx])
38 return {
39 'input_ids': inputs['input_ids'].squeeze(0),
40 'attention_mask': inputs['attention_mask'].squeeze(0)
41 }
42
43# Function to detect plagiarism using the model
44def detect_plagiarism(text1, text2):
45 dataset = PlagiarismDataset(text1, text2, tokenizer)
46 data_loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False)
47
48 predictions = []
49 with torch.no_grad():
50 for batch in data_loader:
51 input_ids = batch['input_ids'].to(device)
52 attention_mask = batch['attention_mask'].to(device)
53
54 outputs = model(input_ids=input_ids, attention_mask=attention_mask)
55 preds = torch.argmax(outputs.logits, dim=1)
56
57 predictions.append(preds.item())
58
59 return predictions[0]
60
61# Usage
62text1 = input("Text from the first document:")
63text2 = input("Text from the first document:")
64
65Result = detect_plagiarism(text1, text2)
66
67# Display the result
68if result == 1:
69 print("Plagiarism detected!")
70else:
71 print("No plagiarism detected.")
72