wjbmattingly commited on
Commit
fd16ff4
·
verified ·
1 Parent(s): 65b868d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Union
2
+ from gliner import GLiNER
3
+ import gradio as gr
4
+
5
+ model = GLiNER.from_pretrained("wjbmattingly/gliner-hrd")
6
+
7
+ examples = [
8
+ [
9
+ "We discovered a collection of deer bones next to a burial mound. Inside the mound we found the finger of a human and twelve ribs from a separate person. We also found a bat wing and a cow skull. There was even a mummified human head.",
10
+ "human remains, animal remains, general remains",
11
+ 0.3,
12
+ True,
13
+ ]
14
+ ]
15
+
16
+ def ner(
17
+ text, labels: str, threshold: float, nested_ner: bool
18
+ ) -> Dict[str, Union[str, int, float]]:
19
+ labels = labels.split(",")
20
+ return {
21
+ "text": text,
22
+ "entities": [
23
+ {
24
+ "entity": entity["label"],
25
+ "word": entity["text"],
26
+ "start": entity["start"],
27
+ "end": entity["end"],
28
+ "score": 0,
29
+ }
30
+ for entity in model.predict_entities(
31
+ text, labels, flat_ner=not nested_ner, threshold=threshold
32
+ )
33
+ ],
34
+ }
35
+
36
+
37
+ with gr.Blocks(title="GLiNER-M-v2.1") as demo:
38
+ gr.Markdown(
39
+ """
40
+ # GLiNER-base
41
+ GLiNER is a Named Entity Recognition (NER) model capable of identifying any entity type using a bidirectional transformer encoder (BERT-like). It provides a practical alternative to traditional NER models, which are limited to predefined entities, and Large Language Models (LLMs) that, despite their flexibility, are costly and large for resource-constrained scenarios.
42
+ ## Links
43
+ * Model: https://huggingface.co/urchade/gliner_multi-v2.1
44
+ * All GLiNER models: https://huggingface.co/models?library=gliner
45
+ * Paper: https://arxiv.org/abs/2311.08526
46
+ * Repository: https://github.com/urchade/GLiNER
47
+ """
48
+ )
49
+ input_text = gr.Textbox(
50
+ value=examples[0][0], label="Text input", placeholder="Enter your text here"
51
+ )
52
+ with gr.Row() as row:
53
+ labels = gr.Textbox(
54
+ value=examples[0][1],
55
+ label="Labels",
56
+ placeholder="Enter your labels here (comma separated)",
57
+ scale=2,
58
+ )
59
+ threshold = gr.Slider(
60
+ 0,
61
+ 1,
62
+ value=0.3,
63
+ step=0.01,
64
+ label="Threshold",
65
+ info="Lower the threshold to increase how many entities get predicted.",
66
+ scale=1,
67
+ )
68
+ nested_ner = gr.Checkbox(
69
+ value=examples[0][2],
70
+ label="Nested NER",
71
+ info="Allow for nested NER?",
72
+ scale=0,
73
+ )
74
+ output = gr.HighlightedText(label="Predicted Entities")
75
+ submit_btn = gr.Button("Submit")
76
+ examples = gr.Examples(
77
+ examples,
78
+ fn=ner,
79
+ inputs=[input_text, labels, threshold, nested_ner],
80
+ outputs=output,
81
+ cache_examples=True,
82
+ )
83
+
84
+ # Submitting
85
+ input_text.submit(
86
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
87
+ )
88
+ labels.submit(
89
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
90
+ )
91
+ threshold.release(
92
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
93
+ )
94
+ submit_btn.click(
95
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
96
+ )
97
+ nested_ner.change(
98
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
99
+ )
100
+
101
+ demo.queue()
102
+ demo.launch(debug=True)