emanuelaboros commited on
Commit
9df3a2f
·
1 Parent(s): 2dac3ae
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -16,6 +16,29 @@ ner_pipeline = pipeline(
16
  )
17
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Helper function to align entities correctly and debug tokenization
20
  def prepare_entities_for_highlight(text, results):
21
  entities = []
@@ -45,7 +68,7 @@ def prepare_entities_for_highlight(text, results):
45
  # Sort entities by their start position
46
  entities = sorted(entities, key=lambda x: x["start"])
47
 
48
- return {"text": text, "entities": entities}
49
 
50
 
51
  # Function to process the sentence and extract entities
@@ -64,7 +87,7 @@ def ner_app_interface():
64
  input_sentence = gr.Textbox(
65
  lines=5, label="Input Sentence", placeholder="Enter a sentence for NER..."
66
  )
67
- output_entities = gr.HighlightedText(label="Extracted Entities")
68
 
69
  # Interface definition
70
  interface = gr.Interface(
 
16
  )
17
 
18
 
19
+ def format_entities_as_html(entities):
20
+ excluded_keys = {"start", "end", "index", "word"} # Keys to exclude from the output
21
+ html_output = "<ul>"
22
+
23
+ for entity in entities:
24
+ html_output += "<li>"
25
+
26
+ # Dynamically add all fields except the excluded ones
27
+ for key, value in entity.items():
28
+ if key not in excluded_keys:
29
+ if isinstance(value, float): # Format score if it's a float
30
+ html_output += (
31
+ f"<strong>{key.capitalize()}:</strong> {value:.2f}<br>"
32
+ )
33
+ else:
34
+ html_output += f"<strong>{key.capitalize()}:</strong> {value}<br>"
35
+
36
+ html_output += "</li>"
37
+
38
+ html_output += "</ul>"
39
+ return html_output
40
+
41
+
42
  # Helper function to align entities correctly and debug tokenization
43
  def prepare_entities_for_highlight(text, results):
44
  entities = []
 
68
  # Sort entities by their start position
69
  entities = sorted(entities, key=lambda x: x["start"])
70
 
71
+ return format_entities_as_html(entities)
72
 
73
 
74
  # Function to process the sentence and extract entities
 
87
  input_sentence = gr.Textbox(
88
  lines=5, label="Input Sentence", placeholder="Enter a sentence for NER..."
89
  )
90
+ output_entities = gr.HTML(label="Extracted Entities")
91
 
92
  # Interface definition
93
  interface = gr.Interface(