danagyl commited on
Commit
dcabb04
·
verified ·
1 Parent(s): cdb656c

Update Negativ/PythonCode/positive_reframe.py

Browse files
Negativ/PythonCode/positive_reframe.py CHANGED
@@ -1,65 +1,65 @@
1
- import torch
2
- from transformers import AutoTokenizer, DebertaForSequenceClassification
3
-
4
- sentence = "I really love that you suck so bad at this game, you are the worst teammate and i hope you die in a fire"
5
-
6
- # Define the model architecture
7
- num_labels = 2 # Assuming a three-class classification task
8
-
9
- # Load the tokenizer
10
- tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-base")
11
-
12
- # Initialize the model architecture
13
- model = DebertaForSequenceClassification.from_pretrained("microsoft/deberta-base", num_labels=num_labels)
14
-
15
- # Load the saved state dictionary
16
- state_dict = torch.load('C:/Users/Michael/Documents/GitHub/Negativ/PythonCode/toxic_deberta_tuned.pth')
17
-
18
- # Load the state dictionary into the model
19
- model.load_state_dict(state_dict)
20
-
21
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
-
23
- # Ensure the model is set up with the correct number of labels
24
- assert model.config.num_labels == num_labels, "Model labels mismatch!"
25
-
26
- def predict(sentence):
27
- # Tokenize the input sentence
28
- inputs = tokenizer(sentence, return_tensors="pt", padding='max_length', truncation=True)
29
-
30
- print(inputs)
31
-
32
- inputs = {k: v.to(device) for k, v in inputs.items()}
33
-
34
- # Move the model to evaluation mode
35
- model.eval()
36
-
37
- # Perform prediction
38
- with torch.no_grad():
39
- outputs = model(**inputs)
40
-
41
- print(outputs)
42
-
43
- logits = outputs.logits
44
- probabilities = torch.softmax(logits, dim=1)
45
-
46
- print(f"Logits: {logits}")
47
- print(f"Probabilities: {probabilities}")
48
-
49
- highest_logit, prediction = torch.max(logits, dim=-1)
50
-
51
- print(f"Highest Logit: {highest_logit.item()}, Predicted Class: {prediction.item()}")
52
-
53
- # Interpret the result
54
- return "Toxic" if prediction == 1 else "Non-toxic"
55
-
56
- def is_toxic(sentence):
57
-
58
- result = predict(sentence)
59
-
60
- print(f"Given Sentence: {sentence}", f"\tPrediction: {result}")
61
-
62
- if result == "Toxic":
63
- return True
64
- else:
65
  return False
 
1
+ import torch
2
+ from transformers import AutoTokenizer, DebertaForSequenceClassification
3
+
4
+ sentence = "I really love that you suck so bad at this game, you are the worst teammate and i hope you die in a fire"
5
+
6
+ # Define the model architecture
7
+ num_labels = 2 # Assuming a three-class classification task
8
+
9
+ # Load the tokenizer
10
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-base")
11
+
12
+ # Initialize the model architecture
13
+ model = DebertaForSequenceClassification.from_pretrained("microsoft/deberta-base", num_labels=num_labels)
14
+
15
+ # Load the saved state dictionary
16
+ state_dict = torch.load('./toxic_deberta_tuned.pth')
17
+
18
+ # Load the state dictionary into the model
19
+ model.load_state_dict(state_dict)
20
+
21
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
+
23
+ # Ensure the model is set up with the correct number of labels
24
+ assert model.config.num_labels == num_labels, "Model labels mismatch!"
25
+
26
+ def predict(sentence):
27
+ # Tokenize the input sentence
28
+ inputs = tokenizer(sentence, return_tensors="pt", padding='max_length', truncation=True)
29
+
30
+ print(inputs)
31
+
32
+ inputs = {k: v.to(device) for k, v in inputs.items()}
33
+
34
+ # Move the model to evaluation mode
35
+ model.eval()
36
+
37
+ # Perform prediction
38
+ with torch.no_grad():
39
+ outputs = model(**inputs)
40
+
41
+ print(outputs)
42
+
43
+ logits = outputs.logits
44
+ probabilities = torch.softmax(logits, dim=1)
45
+
46
+ print(f"Logits: {logits}")
47
+ print(f"Probabilities: {probabilities}")
48
+
49
+ highest_logit, prediction = torch.max(logits, dim=-1)
50
+
51
+ print(f"Highest Logit: {highest_logit.item()}, Predicted Class: {prediction.item()}")
52
+
53
+ # Interpret the result
54
+ return "Toxic" if prediction == 1 else "Non-toxic"
55
+
56
+ def is_toxic(sentence):
57
+
58
+ result = predict(sentence)
59
+
60
+ print(f"Given Sentence: {sentence}", f"\tPrediction: {result}")
61
+
62
+ if result == "Toxic":
63
+ return True
64
+ else:
65
  return False