luerhard commited on
Commit
b11fc3d
·
1 Parent(s): 2354335

Update README.md

Browse files

add usage example

Files changed (1) hide show
  1. README.md +40 -0
README.md CHANGED
@@ -27,6 +27,46 @@ The table connects the position of the predicted probability to its dimension.
27
  | 2 | Left-Wing Host-Ideology |
28
  | 3 | Right-Wing Host-Ideology |
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Performance
32
 
 
27
  | 2 | Left-Wing Host-Ideology |
28
  | 3 | Right-Wing Host-Ideology |
29
 
30
+ # Usage Example
31
+
32
+ ```python
33
+ import torch
34
+ from transformers import AutoModel
35
+ from transformers import AutoTokenizer
36
+
37
+ # optional commit_hash to ensure a consistent version of the model
38
+ commit_hash = "2354335caedc36df44da926291786f0159a502f0"
39
+
40
+ # load tokenizer
41
+ tokenizer = AutoTokenizer.from_pretrained("luerhard/PopBERT", revision=commit_hash)
42
+
43
+ # load model
44
+ # trust_remote_code is necessary to use the custom architecture of this model (module.py)
45
+ model = AutoModel.from_pretrained("luerhard/PopBERT", trust_remote_code=True, revision=commit_hash)
46
+
47
+ # define text to be predicted
48
+ text = (
49
+ "Das ist Klassenkampf von oben, das ist Klassenkampf im Interesse von "
50
+ "Vermögenden und Besitzenden gegen die Mehrheit der Steuerzahlerinnen und "
51
+ "Steuerzahler auf dieser Erde."
52
+ )
53
+
54
+ # encode text with tokenizer
55
+ encodings = tokenizer(text, padding=True, return_tensors="pt")
56
+
57
+ # predict
58
+ with torch.inference_mode():
59
+ _, prediction_tensor = model(**encodings)
60
+
61
+ # convert prediction from torch tensor to numpy array
62
+ prediction = prediction_tensor.numpy()
63
+ print(prediction)
64
+ ```
65
+
66
+ ```
67
+ [[0.84803474 0.9991047 0.9919584 0.19843338]]
68
+ ```
69
+
70
 
71
  # Performance
72