-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy patholala.py
More file actions
85 lines (71 loc) · 2.42 KB
/
Copy patholala.py
File metadata and controls
85 lines (71 loc) · 2.42 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Run OLaLa in OntoAligner
import json
from ontoaligner.ontology import OLaLaOMDataset
from ontoaligner.encoder import OLaLaEncoder
from ontoaligner.aligner.olala import (OLaLaSBERTRetrieval,
OLaLaLLMAligner,
OLaLaHighPrecisionMatcher,
OLaLaAligner)
from ontoaligner.aligner.olala.postprocessor import olala_postprocessor
from ontoaligner.utils import metrics, xmlify
# 1. Load task and ontologies
task = OLaLaOMDataset()
print("Test Task:", task)
dataset = task.collect(
source_ontology_path="../assets/source.owl",
target_ontology_path="../assets/target.owl",
reference_matching_path="../assets/reference.xml",
)
# 2. Encode ontologies
encoder_model = OLaLaEncoder()
encoded_ontology = encoder_model(
source=dataset["source"],
target=dataset["target"],
)
# 3. SBERT candidate generation
retriever = OLaLaSBERTRetrieval(
device="cuda",
top_k=5,
both_directions=True,
topk_per_resource=True,
)
# 4. LLM binary verification
llm_aligner = OLaLaLLMAligner(
device="cuda",
max_new_tokens=10,
temperature=0.0,
truncation=True,
max_length=2048,
padding=True,
loading_arguments={
"device_map": "auto",
"torch_dtype": "torch.float16",
},
)
# 5. High-precision matcher
hp_aligner = OLaLaHighPrecisionMatcher(confidence=1.0)
# 6. OLaLa alignments
olala = OLaLaAligner(retriever=retriever,
llm_aligner=llm_aligner,
hp_aligner=hp_aligner)
olala.load(llm_path="upstage/Llama-2-70b-instruct-v2",
retriever_path="multi-qa-mpnet-base-dot-v1")
alignments = olala.generate(input_data=encoded_ontology)
# 6. OLaLa postprocessing
final_matchings = olala_postprocessor(alignments,
encoded_ontology,
confidence_threshold=0.5,
strict_bad_hosts=False)
# 7. Evaluation
evaluation = metrics.evaluation_report(
predicts=final_matchings,
references=dataset["reference"],
)
print("OLaLa Evaluation Report:")
print(json.dumps(evaluation, indent=4))
# 8. XML export
xml_str = xmlify.xml_alignment_generator(matchings=final_matchings)
output_file_path = "olala_matchings.xml"
with open(output_file_path, "w", encoding="utf-8") as xml_file:
xml_file.write(xml_str)
print(f"Saved OLaLa matchings to {output_file_path}")