bacngv commited on
Commit
52438bc
·
verified ·
1 Parent(s): 0c38940

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -43,7 +43,7 @@ def predict_ner(model, tokenizer, text, label_map, device):
43
  attention_mask = inputs["attention_mask"].to(device)
44
 
45
  with torch.no_grad():
46
- logits = model(input_ids=input_ids, attention_mask=attention_mask)[0]
47
 
48
  label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
49
 
@@ -73,7 +73,7 @@ def predict_ner(model, tokenizer, text, label_map, device):
73
  return result
74
 
75
  def format_output(text, predictions):
76
- formatted_text = "<div style='border: 1px solid #ccc; padding: 10px; border-radius: 5px;'>"
77
  i = 0
78
  for word, label in predictions:
79
  while i < len(text) and text[i].isspace():
@@ -86,11 +86,14 @@ def format_output(text, predictions):
86
  formatted_text += f"{word} "
87
  else:
88
  color = BACKGROUND_COLOR_MAP[label]
89
- formatted_text += f"<span style='background-color:{color}; padding: 2px 5px; border: 1px solid {color}; font-weight: bold;'>{word} <span style='background-color:{color}; border: 1px solid {color};'>{label}</span></span> "
 
 
 
 
90
  i += len(word)
91
 
92
- formatted_text += "</div>"
93
- return formatted_text
94
 
95
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
96
  model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./model.pt", device)
 
43
  attention_mask = inputs["attention_mask"].to(device)
44
 
45
  with torch.no_grad():
46
+ logits = model(inputs_ids=input_ids, labels=None, labels_mask=None, valid_mask=None)
47
 
48
  label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
49
 
 
73
  return result
74
 
75
  def format_output(text, predictions):
76
+ formatted_text = ""
77
  i = 0
78
  for word, label in predictions:
79
  while i < len(text) and text[i].isspace():
 
86
  formatted_text += f"{word} "
87
  else:
88
  color = BACKGROUND_COLOR_MAP[label]
89
+ formatted_text += (
90
+ f"<span style='background-color:{color}; padding: 2px 5px; border: 1px solid {color}; font-weight: bold;'>"
91
+ f"{word} <span style='background-color:{color}; border: 1px solid {color}; padding: 2px; border-radius: 3px; font-weight: bold;'>"
92
+ f"{label}</span></span> "
93
+ )
94
  i += len(word)
95
 
96
+ return f"<div style='border: 2px solid #cccccc; padding: 10px; border-radius: 5px;'>{formatted_text}</div>"
 
97
 
98
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
99
  model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./model.pt", device)