ELHACHYMI commited on
Commit
5239852
·
verified ·
1 Parent(s): 93c9bdd

Create app file

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # -------------------------------
5
+ # MODEL CONFIGURATION
6
+ # -------------------------------
7
+
8
+ model_name = "ELHACHYMI/bert-ner"
9
+
10
+ example_sent = "Bill Gates founded Microsoft in the United States."
11
+
12
+ example_list = [
13
+ "Elon Musk is the CEO of SpaceX and Tesla.",
14
+ "Cristiano Ronaldo plays for Al Nassr in Saudi Arabia.",
15
+ "Google was founded in California by Larry Page and Sergey Brin.",
16
+ "Paris is the capital of France and home to the Eiffel Tower.",
17
+ "Apple Inc. was created by Steve Jobs in Cupertino.",
18
+ ]
19
+
20
+ # Model info for usage display
21
+ model_info = {
22
+ model_name: {
23
+ "link": f"https://huggingface.co/{model_name}",
24
+ "usage": f"""from transformers import pipeline
25
+
26
+ ner = pipeline("ner", model="{model_name}", aggregation_strategy="simple")
27
+ text = "{example_sent}"
28
+ print(ner(text))""",
29
+ }
30
+ }
31
+
32
+ # Load model
33
+ ner_pipeline = pipeline("ner", model=model_name, aggregation_strategy="simple")
34
+
35
+
36
+ # -------------------------------
37
+ # DISPLAY MODEL INFO
38
+ # -------------------------------
39
+
40
+ def display_model_info(model_name):
41
+ info = model_info[model_name]
42
+ usage_code = info["usage"]
43
+ link_button = f'[ Open model page: {model_name}]({info["link"]})'
44
+ return usage_code, link_button
45
+
46
+
47
+ # -------------------------------
48
+ # NER ANALYSIS FUNCTION
49
+ # -------------------------------
50
+
51
+ def analyze_text(text):
52
+ ner_results = ner_pipeline(text)
53
+
54
+ highlighted_text = []
55
+ last_idx = 0
56
+
57
+ for entity in ner_results:
58
+ start = entity["start"]
59
+ end = entity["end"]
60
+ label = entity["entity_group"]
61
+
62
+ # Text before entity
63
+ if start > last_idx:
64
+ highlighted_text.append((text[last_idx:start], None))
65
+
66
+ # Entity text
67
+ highlighted_text.append((text[start:end], label))
68
+ last_idx = end
69
+
70
+ # Remaining text
71
+ if last_idx < len(text):
72
+ highlighted_text.append((text[last_idx:], None))
73
+
74
+ return highlighted_text
75
+
76
+
77
+ # -------------------------------
78
+ # GRADIO INTERFACE
79
+ # -------------------------------
80
+
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown("# Named Entity Recognition Demo using BERT")
83
+ gr.Markdown("### Model: **ELHACHYMI/bert-ner**")
84
+
85
+ # Input text
86
+ text_input = gr.Textbox(
87
+ label="Enter Text",
88
+ lines=5,
89
+ value=example_sent,
90
+ placeholder="Type a sentence to analyze...",
91
+ )
92
+
93
+ # List of examples buttons
94
+ with gr.Row():
95
+ for ex in example_list:
96
+ gr.Button(ex).click(lambda x=ex: x, outputs=text_input)
97
+
98
+ analyze_button = gr.Button("Run NER Analysis")
99
+
100
+ # Highlighted output
101
+ output = gr.HighlightedText(label="NER Result", combine_adjacent=True)
102
+
103
+ # Code + link output
104
+ code_output = gr.Code(label="How to use this model in Python", visible=True)
105
+ link_output = gr.Markdown()
106
+
107
+ # Initialize code + link
108
+ demo.load(
109
+ fn=display_model_info,
110
+ inputs=[gr.Textbox(value=model_name, visible=False)],
111
+ outputs=[code_output, link_output],
112
+ )
113
+
114
+ # Run analysis
115
+ analyze_button.click(
116
+ analyze_text, inputs=[text_input], outputs=output
117
+ )
118
+
119
+ demo.launch()