Spaces:
Paused
Paused
Create artifact.py
Browse files- app/artifact.py +48 -0
app/artifact.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, json, numpy as np
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
|
| 4 |
+
REPO = os.environ["ARTIFACT_REPO"]
|
| 5 |
+
REV = os.environ.get("ARTIFACT_VERSION", "v8.0.0")
|
| 6 |
+
TOK = os.environ["HF_TOKEN"]
|
| 7 |
+
|
| 8 |
+
def _get(fn):
|
| 9 |
+
return hf_hub_download(REPO, fn, repo_type="dataset",
|
| 10 |
+
revision=REV, token=TOK)
|
| 11 |
+
|
| 12 |
+
class Artifact:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.manifest = json.load(open(_get("manifest.json")))
|
| 15 |
+
self.vocab = open(_get("vocab.txt"), encoding="utf-8").read().split("\n")
|
| 16 |
+
self.w2i = {w: i for i, w in enumerate(self.vocab)}
|
| 17 |
+
|
| 18 |
+
self.alias = {}
|
| 19 |
+
for line in open(_get("aliases.tsv"), encoding="utf-8"):
|
| 20 |
+
line = line.strip()
|
| 21 |
+
if line:
|
| 22 |
+
parts = line.split("\t")
|
| 23 |
+
if len(parts) == 2:
|
| 24 |
+
k, v = parts
|
| 25 |
+
# 🛑 ข้ามบรรทัดที่เป็นหัวตาราง (Header)
|
| 26 |
+
if k == "source" or v == "target":
|
| 27 |
+
continue
|
| 28 |
+
try:
|
| 29 |
+
self.alias[k] = int(v)
|
| 30 |
+
except ValueError:
|
| 31 |
+
# เผื่อเจอเคสข้อมูลพิลึก จะได้ไม่แครชกลางอากาศ
|
| 32 |
+
continue
|
| 33 |
+
|
| 34 |
+
self.secrets = json.load(open(_get("secrets.json")))
|
| 35 |
+
|
| 36 |
+
self.order = np.load(_get("order.u16.npy"), mmap_mode="r")
|
| 37 |
+
self.rank = np.load(_get("rank.u16.npy"), mmap_mode="r")
|
| 38 |
+
self.V = self.order.shape[1]
|
| 39 |
+
self.S = self.order.shape[0]
|
| 40 |
+
|
| 41 |
+
def resolve(self, w):
|
| 42 |
+
if w in self.w2i:
|
| 43 |
+
return self.w2i[w], "ok"
|
| 44 |
+
if w in self.alias:
|
| 45 |
+
return self.alias[w], "alias_resolved"
|
| 46 |
+
return None, "not_in_dictionary"
|
| 47 |
+
|
| 48 |
+
ART = Artifact()
|