emanuelaboros commited on
Commit
5436b2b
·
1 Parent(s): b5d1f19

lets highlight some entiteis

Browse files
Files changed (1) hide show
  1. app.py +35 -24
app.py CHANGED
@@ -7,34 +7,40 @@ MODEL_NAME = "impresso-project/ner-stacked-bert-multilingual"
7
  # Load the tokenizer and model using the pipeline
8
  ner_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
 
10
- ner_pipeline = pipeline("generic-ner", model=MODEL_NAME,
11
- tokenizer=ner_tokenizer,
12
- trust_remote_code=True,
13
- device='cpu')
14
-
15
- # Helper function to print entities nicely
16
- def print_nicely(entities):
17
- entity_details = []
18
- for entity in entities:
19
- entity_info = f"Entity: {entity['entity']} | Confidence: {entity['score']:.2f}% | Text: {entity['word'].strip()} | Start: {entity['start']} | End: {entity['end']}"
20
- entity_details.append(entity_info)
21
- return "\n".join(entity_details)
22
 
23
  # Function to process the sentence and extract entities
24
  def extract_entities(sentence):
25
  results = ner_pipeline(sentence)
26
- entity_results = []
27
-
28
- # Extract and format the entities
29
- for key in results.keys():
30
- entity_results.append(print_nicely(results[key]))
31
-
32
- return "\n".join(entity_results)
 
 
 
 
 
 
 
 
33
 
34
  # Create Gradio interface
35
  def ner_app_interface():
36
- input_sentence = gr.Textbox(lines=5, label="Input Sentence", placeholder="Enter a sentence for NER...")
37
- output_entities = gr.Textbox(label="Extracted Entities")
 
 
38
 
39
  # Interface definition
40
  interface = gr.Interface(
@@ -42,12 +48,17 @@ def ner_app_interface():
42
  inputs=input_sentence,
43
  outputs=output_entities,
44
  title="Named Entity Recognition",
45
- description="Enter a sentence to extract named entities using the NER model from the Impresso project."
 
 
 
 
 
46
  )
47
-
48
  interface.launch()
49
 
 
50
  # Run the app
51
  if __name__ == "__main__":
52
  ner_app_interface()
53
-
 
7
  # Load the tokenizer and model using the pipeline
8
  ner_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
 
10
+ ner_pipeline = pipeline(
11
+ "generic-ner",
12
+ model=MODEL_NAME,
13
+ tokenizer=ner_tokenizer,
14
+ trust_remote_code=True,
15
+ device="cpu",
16
+ )
17
+
 
 
 
 
18
 
19
  # Function to process the sentence and extract entities
20
  def extract_entities(sentence):
21
  results = ner_pipeline(sentence)
22
+ entities_with_confidences = []
23
+
24
+ # Extract and format the entities for highlighting
25
+ for entity in results:
26
+ entities_with_confidences.append(
27
+ (
28
+ entity["word"],
29
+ entity["start"],
30
+ entity["end"],
31
+ f"{entity['entity']} ({entity['score']:.2f}%)",
32
+ )
33
+ )
34
+
35
+ return {"text": sentence, "entities": entities_with_confidences}
36
+
37
 
38
  # Create Gradio interface
39
  def ner_app_interface():
40
+ input_sentence = gr.Textbox(
41
+ lines=5, label="Input Sentence", placeholder="Enter a sentence for NER..."
42
+ )
43
+ output_entities = gr.HighlightedText(label="Extracted Entities")
44
 
45
  # Interface definition
46
  interface = gr.Interface(
 
48
  inputs=input_sentence,
49
  outputs=output_entities,
50
  title="Named Entity Recognition",
51
+ description="Enter a sentence to extract named entities using the NER model from the Impresso project.",
52
+ examples=[
53
+ [
54
+ "In the year 1789, King Louis XVI, ruler of France, convened the Estates-General at the Palace of Versailles."
55
+ ]
56
+ ],
57
  )
58
+
59
  interface.launch()
60
 
61
+
62
  # Run the app
63
  if __name__ == "__main__":
64
  ner_app_interface()