TymaaHammouda commited on
Commit
f7d31bb
·
verified ·
1 Parent(s): a271e4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -9,42 +9,48 @@ import json
9
  print("Version ---- 1")
10
  app = FastAPI()
11
 
12
- from huggingface_hub import snapshot_download
 
 
13
 
14
- # Path expected by sinatools
15
  BASE_DIR = os.path.expanduser("~/.sinatools")
16
- MODEL_DIR = os.path.join(BASE_DIR, "relation_model")
17
 
18
- # Create directory
 
 
 
19
  os.makedirs(BASE_DIR, exist_ok=True)
20
 
21
- # Download model ONLY if not exists
22
- if not os.path.exists(MODEL_DIR) or not os.listdir(MODEL_DIR):
 
 
23
  snapshot_download(
24
  repo_id="aaljabari/arabic-relation-extraction-model",
25
- local_dir=MODEL_DIR,
26
  local_dir_use_symlinks=False
27
  )
28
 
 
 
 
 
 
29
 
30
- pretrained_path = "aubmindlab/bert-base-arabertv2" # must match training
31
- tokenizer = AutoTokenizer.from_pretrained(pretrained_path)
32
- encoder = AutoModel.from_pretrained(pretrained_path).eval()
33
-
34
 
35
- checkpoint_path = snapshot_download(repo_id="SinaLab/Nested", allow_patterns="checkpoints/")
 
 
36
 
37
- args_path = hf_hub_download(
38
- repo_id="SinaLab/Nested",
39
- filename="args.json"
40
- )
41
 
42
- with open(args_path, 'r') as f:
43
- args_data = json.load(f)
44
-
45
- # Load model
46
- with open("Nested/utils/tag_vocab.pkl", "rb") as f:
47
- label_vocab = pickle.load(f)
48
 
49
 
50
  from sinatools.relations.relation_extractor import relation_extraction
 
9
  print("Version ---- 1")
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
 
24
+ # -------------------------
25
+ # 1. Download relation model
26
+ # -------------------------
27
+ if not os.path.exists(RELATION_MODEL_DIR) or not os.listdir(RELATION_MODEL_DIR):
28
  snapshot_download(
29
  repo_id="aaljabari/arabic-relation-extraction-model",
30
+ local_dir=RELATION_MODEL_DIR,
31
  local_dir_use_symlinks=False
32
  )
33
 
34
+ # -------------------------
35
+ # 2. Download NER resources
36
+ # -------------------------
37
+ if not os.path.exists(NER_DIR):
38
+ os.makedirs(NER_DIR, exist_ok=True)
39
 
40
+ nested_repo_path = snapshot_download(
41
+ repo_id="SinaLab/Nested"
42
+ )
 
43
 
44
+ # Copy tag_vocab.pkl to expected location
45
+ src_vocab = os.path.join(nested_repo_path, "Nested", "utils", "tag_vocab.pkl")
46
+ dst_vocab = os.path.join(NER_DIR, "tag_vocab.pkl")
47
 
48
+ if os.path.exists(src_vocab):
49
+ shutil.copy(src_vocab, dst_vocab)
 
 
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