meera21-sy commited on
Commit
f225cb5
·
verified ·
1 Parent(s): 38eb777

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Model names (keeping it programmatic)
5
+ model_names = [
6
+ "dslim/bert-base-NER",
7
+ "dslim/bert-base-NER-uncased",
8
+ "dslim/bert-large-NER",
9
+ "dslim/distilbert-NER",
10
+ ]
11
+
12
+ example_sent = (
13
+ "Nim Chimpsky was a chimpanzee at Columbia University named after Noam Chomsky."
14
+ )
15
+
16
+ # Programmatically build the model info dict
17
+ model_info = {
18
+ model_name: {
19
+ "link": f"https://huggingface.co/{model_name}",
20
+ "usage": f"""from transformers import pipeline
21
+ ner = pipeline("ner", model="{model_name}", grouped_entities=True)
22
+ result = ner("{example_sent}")
23
+ print(result)""",
24
+ }
25
+ for model_name in model_names
26
+ }
27
+
28
+ # Load models into a dictionary programmatically for the analyze function
29
+ models = {
30
+ model_name: pipeline("ner", model=model_name, grouped_entities=True)
31
+ for model_name in model_names
32
+ }
33
+
34
+
35
+ # Function to display model info (link and usage code)
36
+ def display_model_info(model_name):
37
+ info = model_info[model_name]
38
+ usage_code = info["usage"]
39
+ link_button = f'[Open model page for {model_name} ]({info["link"]})'
40
+ return usage_code, link_button
41
+
42
+
43
+ # Function to run NER on input text
44
+ def analyze_text(text, model_name):
45
+ ner = models[model_name]
46
+ ner_results = ner(text)
47
+ highlighted_text = []
48
+ last_idx = 0
49
+ for entity in ner_results:
50
+ start = entity["start"]
51
+ end = entity["end"]
52
+ label = entity["entity_group"]
53
+ # Add non-entity text
54
+ if start > last_idx:
55
+ highlighted_text.append((text[last_idx:start], None))
56
+ # Add entity text
57
+ highlighted_text.append((text[start:end], label))
58
+ last_idx = end
59
+ # Add any remaining text after the last entity
60
+ if last_idx < len(text):
61
+ highlighted_text.append((text[last_idx:], None))
62
+ return highlighted_text
63
+
64
+
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("# Named Entity Recognition (NER) with BERT Models")
67
+
68
+ # Dropdown for model selection
69
+ model_selector = gr.Dropdown(
70
+ choices=list(model_info.keys()),
71
+ value=list(model_info.keys())[0],
72
+ label="Select Model",
73
+ )
74
+
75
+ # Textbox for input text
76
+ text_input = gr.Textbox(
77
+ label="Enter Text",
78
+ lines=5,
79
+ value=example_sent,
80
+ )
81
+ analyze_button = gr.Button("Run NER Model")
82
+ output = gr.HighlightedText(label="NER Result", combine_adjacent=True)
83
+
84
+ # Outputs: usage code, model page link, and analyze button
85
+ code_output = gr.Code(label="Use this model", visible=True)
86
+ link_output = gr.Markdown(
87
+ f"[Open model page for {model_selector} ]({model_selector})"
88
+ )
89
+ # Button for analyzing the input text
90
+ analyze_button.click(
91
+ analyze_text, inputs=[text_input, model_selector], outputs=output
92
+ )
93
+
94
+ # Trigger the code output and model link when model is changed
95
+ model_selector.change(
96
+ display_model_info, inputs=[model_selector], outputs=[code_output, link_output]
97
+ )
98
+
99
+ # Call the display_model_info function on load to set initial values
100
+ demo.load(
101
+ fn=display_model_info,
102
+ inputs=[model_selector],
103
+ outputs=[code_output, link_output],
104
+ )
105
+
106
+ demo.launch()
107
+
108
+
109
+