Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,43 +1,22 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import pickle
|
| 4 |
-
import torch
|
| 5 |
-
import torch.nn as nn
|
| 6 |
-
import torch.nn.functional as F
|
| 7 |
-
|
| 8 |
-
from itertools import permutations
|
| 9 |
-
from collections import defaultdict
|
| 10 |
-
from pydantic import BaseModel
|
| 11 |
from fastapi import FastAPI
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
from huggingface_hub import hf_hub_download, snapshot_download
|
| 16 |
-
|
| 17 |
-
from transformers import (
|
| 18 |
-
AutoTokenizer,
|
| 19 |
-
AutoModel,
|
| 20 |
-
BertModel,
|
| 21 |
-
PreTrainedTokenizerFast
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
from Nested.nn.BertSeqTagger import BertSeqTagger
|
|
|
|
|
|
|
|
|
|
| 25 |
from Nested.utils.helpers import load_checkpoint
|
| 26 |
from Nested.utils.data import get_dataloaders, text2segments
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
from IBO_to_XML import IBO_to_XML
|
| 29 |
from XML_to_HTML import NER_XML_to_HTML
|
| 30 |
from NER_Distiller import distill_entities
|
| 31 |
|
| 32 |
-
# =========================
|
| 33 |
-
# App
|
| 34 |
-
# =========================
|
| 35 |
app = FastAPI()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
# =========================
|
| 39 |
-
# NER MODEL (your working one)
|
| 40 |
-
# =========================
|
| 41 |
pretrained_path = "aubmindlab/bert-base-arabertv2" # must match training
|
| 42 |
tokenizer = AutoTokenizer.from_pretrained(pretrained_path)
|
| 43 |
encoder = AutoModel.from_pretrained(pretrained_path).eval()
|
|
@@ -228,182 +207,8 @@ def predict(request: NERRequest):
|
|
| 228 |
status_code=200,
|
| 229 |
)
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
# Relation Model
|
| 234 |
-
# =========================
|
| 235 |
-
repo_id_rel = "aaljabari/arabic-relation-extraction-v1"
|
| 236 |
-
|
| 237 |
-
relation_tokenizer = PreTrainedTokenizerFast(
|
| 238 |
-
tokenizer_file=hf_hub_download(repo_id_rel, "tokenizer.json")
|
| 239 |
-
)
|
| 240 |
-
|
| 241 |
-
weights_path = hf_hub_download(repo_id_rel, "pytorch_model.bin")
|
| 242 |
-
|
| 243 |
-
with open(hf_hub_download(repo_id_rel, "tag_vocab.pkl"), "rb") as f:
|
| 244 |
-
vocab = pickle.load(f)
|
| 245 |
-
|
| 246 |
-
rel2id = vocab["rel2id"]
|
| 247 |
-
id2rel = vocab["id2rel"]
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
class BertRE(nn.Module):
|
| 251 |
-
def __init__(self, num_labels):
|
| 252 |
-
super().__init__()
|
| 253 |
-
self.bert = BertModel.from_pretrained(repo_id_rel)
|
| 254 |
-
|
| 255 |
-
hidden = self.bert.config.hidden_size
|
| 256 |
-
self.dropout = nn.Dropout(self.bert.config.hidden_dropout_prob)
|
| 257 |
-
self.classifier = nn.Linear(hidden * 2, num_labels)
|
| 258 |
-
|
| 259 |
-
def forward(self, input_ids, attention_mask, sub_pos, obj_pos):
|
| 260 |
-
outputs = self.bert(
|
| 261 |
-
input_ids=input_ids,
|
| 262 |
-
attention_mask=attention_mask
|
| 263 |
-
)
|
| 264 |
-
|
| 265 |
-
hidden = outputs.last_hidden_state
|
| 266 |
-
batch = hidden.shape[0]
|
| 267 |
-
|
| 268 |
-
sub_vec = hidden[torch.arange(batch), sub_pos]
|
| 269 |
-
obj_vec = hidden[torch.arange(batch), obj_pos]
|
| 270 |
-
|
| 271 |
-
pair = torch.cat([sub_vec, obj_vec], dim=1)
|
| 272 |
-
pair = self.dropout(pair)
|
| 273 |
-
|
| 274 |
-
return self.classifier(pair)
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
model_re = BertRE(num_labels=len(rel2id))
|
| 278 |
-
model_re.load_state_dict(torch.load(weights_path, map_location="cpu"))
|
| 279 |
-
model_re.eval()
|
| 280 |
-
|
| 281 |
-
# =========================
|
| 282 |
-
# Relation utilities
|
| 283 |
-
# =========================
|
| 284 |
-
relation_lookup = defaultdict(lambda: defaultdict(list))
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
def insert_markers(sentence, ent1, ent2):
|
| 288 |
-
if ent1 not in sentence or ent2 not in sentence:
|
| 289 |
-
return None
|
| 290 |
-
|
| 291 |
-
s = sentence
|
| 292 |
-
s = s.replace(ent1, f"[Sub] {ent1} [/Sub]", 1)
|
| 293 |
-
s = s.replace(ent2, f"[Obj] {ent2} [/Obj]", 1)
|
| 294 |
-
|
| 295 |
-
return s
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
def encode(sentence):
|
| 299 |
-
enc = relation_tokenizer(
|
| 300 |
-
sentence,
|
| 301 |
-
max_length=128,
|
| 302 |
-
padding="max_length",
|
| 303 |
-
truncation=True,
|
| 304 |
-
return_tensors="pt"
|
| 305 |
-
)
|
| 306 |
-
|
| 307 |
-
input_ids = enc["input_ids"]
|
| 308 |
-
attention_mask = enc["attention_mask"]
|
| 309 |
-
|
| 310 |
-
sub_id = relation_tokenizer.convert_tokens_to_ids("[Sub]")
|
| 311 |
-
obj_id = relation_tokenizer.convert_tokens_to_ids("[Obj]")
|
| 312 |
-
|
| 313 |
-
sub_pos = (input_ids == sub_id).nonzero(as_tuple=True)[1]
|
| 314 |
-
obj_pos = (input_ids == obj_id).nonzero(as_tuple=True)[1]
|
| 315 |
-
|
| 316 |
-
return input_ids, attention_mask, sub_pos, obj_pos
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
def predict_relation(sentence):
|
| 320 |
-
input_ids, mask, sub_pos, obj_pos = encode(sentence)
|
| 321 |
-
|
| 322 |
-
with torch.no_grad():
|
| 323 |
-
logits = model_re(input_ids, mask, sub_pos, obj_pos)
|
| 324 |
-
|
| 325 |
-
probs = F.softmax(logits, dim=-1)
|
| 326 |
-
|
| 327 |
-
pred = torch.argmax(probs, dim=-1).item()
|
| 328 |
-
conf = probs[0, pred].item()
|
| 329 |
-
|
| 330 |
-
return id2rel[pred], conf
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
def relation_extractor(sentence):
|
| 334 |
-
entities = entities_and_types(sentence)
|
| 335 |
-
output = []
|
| 336 |
-
|
| 337 |
-
entity_items = list(entities.items())
|
| 338 |
-
pairs = [(e1, e2) for e1, e2 in permutations(entity_items, 2)]
|
| 339 |
-
|
| 340 |
-
for (ent1, type1), (ent2, type2) in pairs:
|
| 341 |
-
|
| 342 |
-
marked = insert_markers(sentence, ent1, ent2)
|
| 343 |
-
if not marked:
|
| 344 |
-
continue
|
| 345 |
-
|
| 346 |
-
rel, conf = predict_relation(marked)
|
| 347 |
-
|
| 348 |
-
if conf > 0.80 and rel != "no_relation":
|
| 349 |
-
output.append([ent1, rel, ent2, conf])
|
| 350 |
-
|
| 351 |
-
return output
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
# =========================
|
| 355 |
-
# API Models
|
| 356 |
-
# =========================
|
| 357 |
-
class NERRequest(BaseModel):
|
| 358 |
-
text: str
|
| 359 |
-
mode: str = "1"
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
class RERequest(BaseModel):
|
| 363 |
-
text: str
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
# =========================
|
| 367 |
-
# NER endpoint
|
| 368 |
-
# =========================
|
| 369 |
-
@app.post("/predict")
|
| 370 |
-
def predict(request: NERRequest):
|
| 371 |
-
|
| 372 |
-
text = request.text
|
| 373 |
-
mode = request.mode
|
| 374 |
-
|
| 375 |
-
sentences = sentence_tokenizer(text)
|
| 376 |
-
|
| 377 |
-
results = []
|
| 378 |
-
|
| 379 |
-
for sentence in sentences:
|
| 380 |
-
chunks = split_text_into_groups_of_Ns(sentence, 300)
|
| 381 |
-
|
| 382 |
-
for c in chunks:
|
| 383 |
-
output_list = jsons_to_list_of_lists(extract(c))
|
| 384 |
-
results.append(output_list)
|
| 385 |
-
|
| 386 |
-
return JSONResponse({
|
| 387 |
-
"resp": results,
|
| 388 |
-
"statusText": "OK",
|
| 389 |
-
"statusCode": 0
|
| 390 |
-
})
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
# =========================
|
| 394 |
-
# Relation endpoint
|
| 395 |
-
# =========================
|
| 396 |
-
@app.post("/predict_re")
|
| 397 |
-
def predict_re(request: RERequest):
|
| 398 |
-
|
| 399 |
-
results = relation_extractor(request.text)
|
| 400 |
-
|
| 401 |
-
return JSONResponse({
|
| 402 |
-
"resp": results,
|
| 403 |
-
"statusText": "OK",
|
| 404 |
-
"statusCode": 0
|
| 405 |
-
})
|
| 406 |
-
|
| 407 |
|
| 408 |
# mount frontend
|
| 409 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
import torch
|
| 3 |
+
import pickle
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download, snapshot_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from Nested.nn.BertSeqTagger import BertSeqTagger
|
| 6 |
+
from transformers import AutoTokenizer, AutoModel
|
| 7 |
+
import inspect
|
| 8 |
+
from collections import namedtuple
|
| 9 |
from Nested.utils.helpers import load_checkpoint
|
| 10 |
from Nested.utils.data import get_dataloaders, text2segments
|
| 11 |
+
import json
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
from fastapi.responses import JSONResponse
|
| 14 |
from IBO_to_XML import IBO_to_XML
|
| 15 |
from XML_to_HTML import NER_XML_to_HTML
|
| 16 |
from NER_Distiller import distill_entities
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
app = FastAPI()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
pretrained_path = "aubmindlab/bert-base-arabertv2" # must match training
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(pretrained_path)
|
| 22 |
encoder = AutoModel.from_pretrained(pretrained_path).eval()
|
|
|
|
| 207 |
status_code=200,
|
| 208 |
)
|
| 209 |
|
| 210 |
+
from fastapi.staticfiles import StaticFiles
|
| 211 |
+
from fastapi.responses import FileResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
# mount frontend
|
| 214 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|