bacngv commited on
Commit
17cd6e8
·
verified ·
1 Parent(s): f05b03b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -16,6 +16,8 @@ BACKGROUND_COLOR_MAP = {
16
  'O': 'white'
17
  }
18
 
 
 
19
  def load_model(pretrained_path, model_path, device):
20
  # Load the processor and label map
21
  processor = NerProcessor()
@@ -82,15 +84,15 @@ def predict_ner(model, tokenizer, text, label_map, device):
82
 
83
  return result
84
 
85
- def format_ner_output(text, predictions):
86
  formatted_text = ""
87
  for word, label in predictions:
88
- background_color = BACKGROUND_COLOR_MAP.get(label, 'white')
89
- if word in '.,~!@#$%^&*()':
90
- formatted_text += f"{word} "
91
  else:
92
- formatted_text += f"<span style='background-color: {background_color}; font-size: 16px;'>{word}</span> "
93
- return formatted_text
 
94
 
95
  # Load the model and tokenizer
96
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -98,7 +100,7 @@ model, tokenizer, label_map = load_model("./pretrained_models/xlmr.base", "./mod
98
 
99
  def ner(text):
100
  predictions = predict_ner(model, tokenizer, text, label_map, device)
101
- return format_ner_output(text, predictions)
102
 
103
  # Define Gradio interface
104
  interface = gr.Interface(
 
16
  'O': 'white'
17
  }
18
 
19
+ PUNCTUATION = set('. , ~!@#$%^&*()')
20
+
21
  def load_model(pretrained_path, model_path, device):
22
  # Load the processor and label map
23
  processor = NerProcessor()
 
84
 
85
  return result
86
 
87
+ def format_predictions(predictions):
88
  formatted_text = ""
89
  for word, label in predictions:
90
+ if word in PUNCTUATION:
91
+ formatted_text += f'{word} '
 
92
  else:
93
+ color = BACKGROUND_COLOR_MAP.get(label, 'white')
94
+ formatted_text += f'<span style="background-color: {color}; font-size: 1.2em;">{word}</span> <span style="background-color: {color}; font-size: 1.2em; font-weight: bold;">({label})</span> '
95
+ return formatted_text.strip()
96
 
97
  # Load the model and tokenizer
98
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
100
 
101
  def ner(text):
102
  predictions = predict_ner(model, tokenizer, text, label_map, device)
103
+ return format_predictions(predictions)
104
 
105
  # Define Gradio interface
106
  interface = gr.Interface(