Upload load_winner.py with huggingface_hub
Browse files- load_winner.py +45 -0
load_winner.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
|
| 5 |
+
def load_winner_model(kge_path: str = r"kge", device: str = "cpu"):
|
| 6 |
+
"""
|
| 7 |
+
Load the CoDEx-S ComplEx winner model from Hugging Face.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
kge_path : absolute path to your local codex/kge directory
|
| 11 |
+
device : "cpu" or "cuda"
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
winner_model : KgeModel ready for inference
|
| 15 |
+
"""
|
| 16 |
+
sys.path.insert(0, kge_path)
|
| 17 |
+
from kge.model import KgeModel
|
| 18 |
+
from kge.util.io import load_checkpoint
|
| 19 |
+
|
| 20 |
+
print("Downloading winner_model from Hugging Face...")
|
| 21 |
+
path = hf_hub_download(
|
| 22 |
+
repo_id="aaryaupadhya20/codex-s-complex-winner",
|
| 23 |
+
filename="winner_model.pt"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
print("Loading checkpoint...")
|
| 27 |
+
checkpoint = load_checkpoint(path, device=device)
|
| 28 |
+
winner_model = KgeModel.create_from(checkpoint)
|
| 29 |
+
winner_model.eval()
|
| 30 |
+
print("winner_model loaded and ready!")
|
| 31 |
+
return winner_model
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
import torch
|
| 36 |
+
|
| 37 |
+
model = load_winner_model()
|
| 38 |
+
|
| 39 |
+
# Score a test triple (integer indices from CoDEx-S)
|
| 40 |
+
s = torch.tensor([0])
|
| 41 |
+
p = torch.tensor([1])
|
| 42 |
+
o = torch.tensor([2])
|
| 43 |
+
|
| 44 |
+
score = model.score_spo(s, p, o, direction="o")
|
| 45 |
+
print(f"Test triple score: {score.item():.4f}")
|