Update README.md
Browse files
README.md
CHANGED
|
@@ -62,14 +62,31 @@ Using HuggingFace Transformers (Python):
|
|
| 62 |
---
|
| 63 |
<small>
|
| 64 |
|
| 65 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 66 |
import torch
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
def predict_eie(review_text: str):
|
| 75 |
inputs = tokenizer(
|
|
@@ -77,7 +94,7 @@ def predict_eie(review_text: str):
|
|
| 77 |
truncation=True,
|
| 78 |
padding="max_length",
|
| 79 |
max_length=2048,
|
| 80 |
-
return_tensors="pt"
|
| 81 |
)
|
| 82 |
with torch.no_grad():
|
| 83 |
outputs = model(**inputs)
|
|
@@ -85,13 +102,9 @@ def predict_eie(review_text: str):
|
|
| 85 |
probs = torch.softmax(logits, dim=-1)
|
| 86 |
prob_eie = probs[1].item()
|
| 87 |
label = int(prob_eie >= threshold)
|
| 88 |
-
return {
|
| 89 |
-
"prob_eie": prob_eie,
|
| 90 |
-
"label": label # 1 = EIE present, 0 = EIE absent
|
| 91 |
-
}
|
| 92 |
|
| 93 |
-
|
| 94 |
-
print(predict_eie(example))
|
| 95 |
</small>
|
| 96 |
|
| 97 |
---
|
|
|
|
| 62 |
---
|
| 63 |
<small>
|
| 64 |
|
|
|
|
| 65 |
import torch
|
| 66 |
+
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
|
| 67 |
+
from huggingface_hub import hf_hub_download
|
| 68 |
+
from safetensors.torch import load_file
|
| 69 |
|
| 70 |
+
base_model_id = "answerdotai/ModernBERT-base"
|
| 71 |
+
fine_tuned_model_id = "gameresearch/modernbert-eie-2048"
|
| 72 |
+
threshold = 0.2 # optimized decision threshold
|
| 73 |
|
| 74 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 75 |
+
fine_tuned_model_id,
|
| 76 |
+
subfolder="model",
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
config = AutoConfig.from_pretrained(base_model_id)
|
| 80 |
+
config.num_labels = 2
|
| 81 |
+
|
| 82 |
+
model = AutoModelForSequenceClassification.from_config(config)
|
| 83 |
+
|
| 84 |
+
weights_filename = "model/model.safetensors"
|
| 85 |
+
weights_path = hf_hub_download(fine_tuned_model_id, weights_filename)
|
| 86 |
+
|
| 87 |
+
state_dict = load_file(weights_path)
|
| 88 |
+
model.load_state_dict(state_dict)
|
| 89 |
+
model.eval()
|
| 90 |
|
| 91 |
def predict_eie(review_text: str):
|
| 92 |
inputs = tokenizer(
|
|
|
|
| 94 |
truncation=True,
|
| 95 |
padding="max_length",
|
| 96 |
max_length=2048,
|
| 97 |
+
return_tensors="pt",
|
| 98 |
)
|
| 99 |
with torch.no_grad():
|
| 100 |
outputs = model(**inputs)
|
|
|
|
| 102 |
probs = torch.softmax(logits, dim=-1)
|
| 103 |
prob_eie = probs[1].item()
|
| 104 |
label = int(prob_eie >= threshold)
|
| 105 |
+
return {"prob_eie": prob_eie, "label": label}
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
print(predict_eie("This game absolutely destroyed me emotionally. I still think about the ending."))
|
|
|
|
| 108 |
</small>
|
| 109 |
|
| 110 |
---
|