IsmaelMousa commited on
Commit
bae4cd2
·
verified ·
1 Parent(s): 3a5d5bf

setup the program

Browse files
Files changed (2) hide show
  1. app.py +72 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ ner = pipeline(task="token-classification", model="IsmaelMousa/modernbert-ner-conll2003", aggregation_strategy="max")
5
+
6
+ def extract(text):
7
+ """
8
+ Extract named entities from text
9
+
10
+ :param text: input text
11
+ :return: formatted output and highlighted text
12
+ """
13
+ if not text.strip(): return "Please enter some text to analyze.", []
14
+
15
+ results = ner(text)
16
+ output = ""
17
+
18
+ for entity in results:
19
+ word = entity["word"]
20
+ label = entity["entity_group"]
21
+ score = entity["score"]
22
+ output += f"**{word}** → {label} (confidence: {score:.2%})\n"
23
+
24
+ if not output: output = "No named entities found in the text."
25
+
26
+ highlighted = []
27
+ last = 0
28
+
29
+ for entity in results:
30
+ start = entity["start"]
31
+ end = entity["end"]
32
+
33
+ if start > last: highlighted.append((text[last:start], None))
34
+
35
+ highlighted.append((text[start:end], entity["entity_group"]))
36
+ last = end
37
+
38
+ if last < len(text): highlighted.append((text[last:], None))
39
+
40
+ return output, highlighted if highlighted else [(text, None)]
41
+
42
+ examples = [["Hi, I'm Ismael Mousa from Palestine working for NVIDIA inc."] ,
43
+ ["Apple Inc. was founded by Steve Jobs in Cupertino, California."] ,
44
+ ["The Eiffel Tower in Paris, France attracts millions of visitors every year."],
45
+ ["Barack Obama was the 44th President of the United States."] ,]
46
+
47
+ with gr.Blocks(title="Named Entity Recognition") as demo:
48
+ gr.Markdown(
49
+ """
50
+ # 🏷️ Named Entity Recognition
51
+ Extract named entities (persons, organizations, locations) from text using ModernBERT.
52
+
53
+ **Model:** [IsmaelMousa/modernbert-ner-conll2003](https://huggingface.co/IsmaelMousa/modernbert-ner-conll2003)
54
+ """
55
+ )
56
+
57
+ with gr.Row():
58
+ with gr.Column():
59
+ input_text = gr.Textbox(label="Input Text", placeholder="Enter text to analyze...", lines=5)
60
+ submit_btn = gr.Button("Extract Entities", variant="primary")
61
+
62
+ with gr.Column():
63
+ output_text = gr.Markdown(label="Detected Entities")
64
+ highlighted_text = gr.HighlightedText(label="Highlighted Text", combine_adjacent=True, show_legend=True)
65
+
66
+ gr.Examples(examples=examples, inputs=input_text, outputs=[output_text, highlighted_text], fn=extract, cache_examples=False)
67
+
68
+ submit_btn.click(fn=extract, inputs=input_text, outputs=[output_text, highlighted_text])
69
+ input_text.submit(fn=extract, inputs=input_text, outputs=[output_text, highlighted_text])
70
+
71
+
72
+ if __name__ == "__main__": demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio