Upload folder using huggingface_hub
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- app.py +72 -26
__pycache__/app.cpython-312.pyc
ADDED
Binary file (2.66 kB). View file
|
|
app.py
CHANGED
@@ -1,25 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
models = {
|
7 |
-
|
8 |
-
|
9 |
-
),
|
10 |
-
"dslim/bert-base-NER-uncased": pipeline(
|
11 |
-
"ner", model="dslim/bert-base-NER-uncased", grouped_entities=True
|
12 |
-
),
|
13 |
-
"dslim/bert-large-NER": pipeline(
|
14 |
-
"ner", model="dslim/bert-large-NER", grouped_entities=True
|
15 |
-
),
|
16 |
-
"dslim/distilbert-NER": pipeline(
|
17 |
-
"ner", model="dslim/distilbert-NER", grouped_entities=True
|
18 |
-
),
|
19 |
}
|
20 |
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
ner = models[model_name]
|
24 |
ner_results = ner(text)
|
25 |
highlighted_text = []
|
@@ -41,20 +63,44 @@ def process(text, model_name):
|
|
41 |
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
-
gr.Markdown("# Named Entity Recognition with BERT Models")
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
text_input = gr.Textbox(
|
52 |
label="Enter Text",
|
53 |
lines=5,
|
54 |
-
value=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
)
|
56 |
-
output = gr.HighlightedText(label="Named Entities")
|
57 |
-
analyze_button = gr.Button("Analyze")
|
58 |
-
analyze_button.click(process, inputs=[text_input, model_selector], outputs=output)
|
59 |
|
60 |
demo.launch()
|
|
|
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 = []
|
|
|
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()
|