dslim commited on
Commit
47ae213
·
verified ·
1 Parent(s): 0a4290b

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +56 -25
app.py CHANGED
@@ -1,29 +1,60 @@
1
- from transformers import pipeline
2
  import gradio as gr
 
3
 
4
-
5
- # List of NER models
6
- models = ["dslim/bert-base-NER", "dslim/bert-base-NER-uncased", "dslim/bert-large-NER"]
7
-
8
-
9
- def ner(text, model_choice):
10
- ner_pipeline = pipeline("ner", model=model_choice)
11
- output = ner_pipeline(text)
12
- return {"text": text, "entities": output}
13
-
14
-
15
- examples = [
16
- "Does Chicago have any stores and does Joe live here?",
17
- ]
18
-
19
- demo = gr.Interface(
20
- fn=ner,
21
- inputs=[
22
- gr.Textbox(placeholder="Enter sentence here..."),
23
- gr.Dropdown(choices=models, label="Choose NER Model"),
24
- ],
25
- outputs=gr.HighlightedText(),
26
- examples=examples,
27
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # Load the NER models
5
+ # Load the NER models
6
+ models = {
7
+ "dslim/bert-base-NER": pipeline(
8
+ "ner", model="dslim/bert-base-NER", grouped_entities=True
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
+ def process(text, model_name):
23
+ ner = models[model_name]
24
+ ner_results = ner(text)
25
+ highlighted_text = []
26
+ last_idx = 0
27
+ for entity in ner_results:
28
+ start = entity["start"]
29
+ end = entity["end"]
30
+ label = entity["entity_group"]
31
+ # Add non-entity text
32
+ if start > last_idx:
33
+ highlighted_text.append((text[last_idx:start], None))
34
+ # Add entity text
35
+ highlighted_text.append((text[start:end], label))
36
+ last_idx = end
37
+ # Add any remaining text after the last entity
38
+ if last_idx < len(text):
39
+ highlighted_text.append((text[last_idx:], None))
40
+ return highlighted_text
41
+
42
+
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("# Named Entity Recognition with BERT Models")
45
+ with gr.Row():
46
+ model_selector = gr.Dropdown(
47
+ choices=list(models.keys()),
48
+ value=list(models.keys())[0],
49
+ label="Select Model",
50
+ )
51
+ text_input = gr.Textbox(
52
+ label="Enter Text",
53
+ lines=5,
54
+ value="Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, therefore very close to the Manhattan Bridge.",
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()