Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,18 +6,47 @@ from fastapi.responses import JSONResponse
|
|
| 6 |
from transformers import AutoTokenizer, AutoModel
|
| 7 |
import json
|
| 8 |
|
| 9 |
-
print("Version ----
|
| 10 |
-
app = FastAPI()
|
| 11 |
|
| 12 |
from huggingface_hub import snapshot_download, hf_hub_download
|
| 13 |
import os
|
| 14 |
import shutil
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
BASE_DIR = os.path.expanduser("~/.sinatools")
|
| 17 |
|
| 18 |
# Paths expected by sinatools
|
| 19 |
RELATION_MODEL_DIR = os.path.join(BASE_DIR, "relation_model")
|
| 20 |
-
NER_DIR = os.path.join(BASE_DIR, "Wj27012000.tar")
|
| 21 |
|
| 22 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 23 |
|
|
@@ -34,23 +63,23 @@ if not os.path.exists(RELATION_MODEL_DIR) or not os.listdir(RELATION_MODEL_DIR):
|
|
| 34 |
# -------------------------
|
| 35 |
# 2. Download NER resources
|
| 36 |
# -------------------------
|
| 37 |
-
if not os.path.exists(NER_DIR):
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
# Optional debug
|
| 52 |
print("sinatools dir:", os.listdir(BASE_DIR))
|
| 53 |
-
print("NER dir:", os.listdir(NER_DIR))
|
| 54 |
|
| 55 |
|
| 56 |
from sinatools.relations.relation_extractor import relation_extraction
|
|
|
|
| 6 |
from transformers import AutoTokenizer, AutoModel
|
| 7 |
import json
|
| 8 |
|
| 9 |
+
print("Version ---- 2")
|
|
|
|
| 10 |
|
| 11 |
from huggingface_hub import snapshot_download, hf_hub_download
|
| 12 |
import os
|
| 13 |
import shutil
|
| 14 |
|
| 15 |
+
from transformers import AutoTokenizer, AutoModel
|
| 16 |
+
import inspect
|
| 17 |
+
from collections import namedtuple
|
| 18 |
+
import json
|
| 19 |
+
from pydantic import BaseModel
|
| 20 |
+
from fastapi.responses import JSONResponse
|
| 21 |
+
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
pretrained_path = "aubmindlab/bert-base-arabertv2" # must match training
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(pretrained_path)
|
| 26 |
+
encoder = AutoModel.from_pretrained(pretrained_path).eval()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
checkpoint_path = snapshot_download(repo_id="SinaLab/Nested", allow_patterns="checkpoints/")
|
| 30 |
+
|
| 31 |
+
args_path = hf_hub_download(
|
| 32 |
+
repo_id="SinaLab/Nested",
|
| 33 |
+
filename="args.json"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
with open(args_path, 'r') as f:
|
| 37 |
+
args_data = json.load(f)
|
| 38 |
+
|
| 39 |
+
# Load model
|
| 40 |
+
with open("Nested/utils/tag_vocab.pkl", "rb") as f:
|
| 41 |
+
label_vocab = pickle.load(f)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
BASE_DIR = os.path.expanduser("~/.sinatools")
|
| 46 |
|
| 47 |
# Paths expected by sinatools
|
| 48 |
RELATION_MODEL_DIR = os.path.join(BASE_DIR, "relation_model")
|
| 49 |
+
# NER_DIR = os.path.join(BASE_DIR, "Wj27012000.tar")
|
| 50 |
|
| 51 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 52 |
|
|
|
|
| 63 |
# -------------------------
|
| 64 |
# 2. Download NER resources
|
| 65 |
# -------------------------
|
| 66 |
+
# if not os.path.exists(NER_DIR):
|
| 67 |
+
# os.makedirs(NER_DIR, exist_ok=True)
|
| 68 |
|
| 69 |
+
# nested_repo_path = snapshot_download(
|
| 70 |
+
# repo_id="SinaLab/Nested"
|
| 71 |
+
# )
|
| 72 |
|
| 73 |
+
# # Copy tag_vocab.pkl to expected location
|
| 74 |
+
# src_vocab = os.path.join(nested_repo_path, "Nested", "utils", "tag_vocab.pkl")
|
| 75 |
+
# dst_vocab = os.path.join(NER_DIR, "tag_vocab.pkl")
|
| 76 |
|
| 77 |
+
# if os.path.exists(src_vocab):
|
| 78 |
+
# shutil.copy(src_vocab, dst_vocab)
|
| 79 |
|
| 80 |
# Optional debug
|
| 81 |
print("sinatools dir:", os.listdir(BASE_DIR))
|
| 82 |
+
# print("NER dir:", os.listdir(NER_DIR))
|
| 83 |
|
| 84 |
|
| 85 |
from sinatools.relations.relation_extractor import relation_extraction
|