bacngv commited on
Commit
7c15ae0
·
verified ·
1 Parent(s): d232529

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -8
app.py CHANGED
@@ -49,7 +49,7 @@ def predict_ner(model, tokenizer, text, label_map, device):
49
 
50
  # Predict NER labels
51
  with torch.no_grad():
52
- logits = model(input_ids=input_ids, attention_mask=attention_mask)
53
 
54
  # Convert logits to label IDs
55
  label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
@@ -72,27 +72,24 @@ def predict_ner(model, tokenizer, text, label_map, device):
72
  current_label = label
73
  else:
74
  current_word += token
 
75
  if label != 'O':
76
  current_label = label
77
  if current_word:
78
  result.append((current_word, current_label))
79
 
80
- # Remove punctuation from results
81
- result = [(word, label) for word, label in result if word]
82
-
83
  return result
84
 
85
  def format_predictions(predictions):
86
  formatted_text = ""
87
  for word, label in predictions:
88
- color = BACKGROUND_COLOR_MAP.get(label, 'white')
89
- word_style = f'background-color: {color}; font-size: 18px; padding: 2px;'
90
  if label == 'O':
91
- formatted_text += f'<span style="{word_style}">{word}</span> '
92
  else:
 
93
  label_style = f'background-color: {color}; font-weight: bold;'
 
94
  formatted_text += f'<span style="{word_style}">{word} <span style="{label_style}">[{label}]</span></span> '
95
-
96
  return formatted_text.strip()
97
 
98
  # Load the model and tokenizer
 
49
 
50
  # Predict NER labels
51
  with torch.no_grad():
52
+ logits = model(inputs_ids=input_ids, labels=None, labels_mask=None, valid_mask=None)
53
 
54
  # Convert logits to label IDs
55
  label_ids = torch.argmax(logits, dim=2).detach().cpu().numpy()
 
72
  current_label = label
73
  else:
74
  current_word += token
75
+ # If the label is not 'O', update the current label to the new label
76
  if label != 'O':
77
  current_label = label
78
  if current_word:
79
  result.append((current_word, current_label))
80
 
 
 
 
81
  return result
82
 
83
  def format_predictions(predictions):
84
  formatted_text = ""
85
  for word, label in predictions:
 
 
86
  if label == 'O':
87
+ formatted_text += f'<span>{word}</span> '
88
  else:
89
+ color = BACKGROUND_COLOR_MAP.get(label, 'white')
90
  label_style = f'background-color: {color}; font-weight: bold;'
91
+ word_style = f'background-color: {color}; font-size: 18px; padding: 2px;'
92
  formatted_text += f'<span style="{word_style}">{word} <span style="{label_style}">[{label}]</span></span> '
 
93
  return formatted_text.strip()
94
 
95
  # Load the model and tokenizer