Keyurjotaniya007 commited on
Commit
0866bae
·
verified ·
1 Parent(s): 1f1a5f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -10
app.py CHANGED
@@ -2,29 +2,25 @@ import gradio as gr
2
  from transformers import pipeline
3
 
4
  instructions = "Enter a sentence in any language and click **Extract Entities**."
 
5
  ner = pipeline(
6
  "ner",
7
  model="Keyurjotaniya007/xlm-roberta-base-xtreme-multilingual-ner-2.0",
8
- aggregation_strategy="simple"
9
  )
10
 
11
  def extract_entities(text):
12
  results = ner(text)
13
- entities = [{"word": r["word"], "label": r.get("entity_group"), "score": float(r.get("score", 0))} for r in results]
14
-
15
- annotated = " | ".join([f"{e['word']} ({e['label']})" for e in entities]) if entities else "No entities found."
16
-
17
- return entities, annotated
18
 
19
  with gr.Blocks() as demo:
20
  gr.Markdown(instructions)
21
  with gr.Row():
22
- inp = gr.Textbox(label="Enter Text", placeholder="Type a sentence...", lines=3)
23
- out_json = gr.JSON(label="Entities (JSON)")
24
- out_text = gr.Textbox(label="Annotated Text")
25
  btn = gr.Button("Extract Entities")
26
 
27
- btn.click(fn=extract_entities, inputs=inp, outputs=[out_json, out_text])
28
 
29
  if __name__ == "__main__":
30
  demo.launch()
 
2
  from transformers import pipeline
3
 
4
  instructions = "Enter a sentence in any language and click **Extract Entities**."
5
+
6
  ner = pipeline(
7
  "ner",
8
  model="Keyurjotaniya007/xlm-roberta-base-xtreme-multilingual-ner-2.0",
9
+ grouped_entities=True
10
  )
11
 
12
  def extract_entities(text):
13
  results = ner(text)
14
+ return [(ent['word'], ent['entity_group']) for ent in results]
 
 
 
 
15
 
16
  with gr.Blocks() as demo:
17
  gr.Markdown(instructions)
18
  with gr.Row():
19
+ inp = gr.Textbox(label="Enter Text", placeholder="Type a sentence in any language...", lines=3)
20
+ out = gr.HighlightedText(label="Named Entities")
 
21
  btn = gr.Button("Extract Entities")
22
 
23
+ btn.click(fn=extract_entities, inputs=inp, outputs=out)
24
 
25
  if __name__ == "__main__":
26
  demo.launch()