forked from VivatImperial/SlovarikDB_Hallucination_Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvsevolo_de_bert.py
More file actions
59 lines (49 loc) · 1.61 KB
/
Copy pathvsevolo_de_bert.py
File metadata and controls
59 lines (49 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from sentence_transformers.cross_encoder import CrossEncoder
class VsevoloDeBERT:
def __init__(self, model_path: str) -> None:
"""
model_path: path to dir with weights in transformers format
"""
self.model_path = model_path
self.model = self.load_model()
def load_model(self) -> None:
"""
Load model weights
"""
try:
model = CrossEncoder(
self.model_path,
num_labels=1,
automodel_args={
"ignore_mismatched_sizes": True
}
)
print("Model loaded successfully")
return model
except Exception as e:
print(f"Error loading model: {e}")
return None
def predict(self, data: list[str]) -> int:
"""
Model prediction on one sample
"""
if not self.model:
print("Model is not loaded")
return None
try:
predictions = int(self.model.predict(data) > 0.2)
return predictions
except Exception as e:
print(f"Error during prediction: {e}")
return None
# Usage example:
if __name__ == "__main__":
import polars as pl
raw = pl.read_csv("../../../Data/Raw Data/train.csv").to_dicts()
test_data = raw[0]["summary"] + " Ответ: " + raw[0]["answer"]
model = VsevoloDeBERT(
"vsevolo_de_bert"
)
predictions = model.predict([test_data])
if predictions is not None:
print(predictions)