File size: 2,140 Bytes
08d2ea6
 
 
 
a6067aa
7ca09fd
08d2ea6
 
 
 
 
 
 
 
 
a6067aa
 
08d2ea6
 
a6067aa
08d2ea6
 
 
 
 
 
a6067aa
08d2ea6
 
a6067aa
 
08d2ea6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6067aa
08d2ea6
 
a6067aa
08d2ea6
a6067aa
08d2ea6
 
a6067aa
08d2ea6
 
 
7ca09fd
 
 
08d2ea6
 
 
 
 
 
 
a6067aa
08d2ea6
 
 
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
from pathlib import Path
import pickle
import torch
import torch.nn as nn
import pandas as pd
import json
from sentence_transformers import SentenceTransformer


DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

HIDDEN_DIM = 768
DROPOUT = 0.1


class FlatEmbedMLP(nn.Module):
    def __init__(self, input_dim, n_classes, hidden_dim=HIDDEN_DIM, dropout=DROPOUT):
        super().__init__()

        self.net = nn.Sequential(
            nn.Linear(input_dim, hidden_dim),
            nn.ReLU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim, n_classes),
        )

    def forward(self, x):
        return self.net(x)


_artifacts = None


def _artifacts_root():
    return Path(__file__).resolve().parents[1] / "training" / "artifacts"


def load_artifacts():
    global _artifacts

    if _artifacts is not None:
        return _artifacts

    artifacts_dir = _artifacts_root()

    with open(artifacts_dir / "label_maps" / "label_maps_embed.pkl", "rb") as f:
        label_maps = pickle.load(f)

    with open(artifacts_dir / "embedder" / "embed_metadata.pkl", "rb") as f:
        embed_metadata = pickle.load(f)

    embedder_model_name = embed_metadata["model_name"]
    embedder = SentenceTransformer(embedder_model_name, device=DEVICE)

    n_classes = len(label_maps["y6"]["classes"])
    input_dim = int(embed_metadata["embedding_dim"])

    model = FlatEmbedMLP(
        input_dim=input_dim,
        n_classes=n_classes,
    ).to(DEVICE)

    model_path = artifacts_dir / "models" / "flat_embed_best.pt"
    model.load_state_dict(torch.load(model_path, map_location=DEVICE))
    model.eval()

    title_lookup_path = artifacts_dir / "label_maps" / "y6_title_lookup.json"
    with open(title_lookup_path, "r") as f:
        y6_title_lookup = json.load(f)

    _artifacts = {
        "device": DEVICE,
        "embedder": embedder,
        "model": model,
        "label_maps": label_maps,
        "embed_metadata": embed_metadata,
        "y6_title_lookup": y6_title_lookup,
    }

    return _artifacts