vumichien commited on
Commit
3d13ba6
1 Parent(s): c742233

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -0
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Union
2
+ from gliner import GLiNER
3
+ import gradio as gr
4
+
5
+ jp_model = GLiNER.from_pretrained("vumichien/ner-jp-gliner")
6
+ meal_model = GLiNER.from_pretrained("urchade/gliner_mediumv2.1")
7
+
8
+ examples = [
9
+ [
10
+ "ner_jp",
11
+ "SPRiNGSと最も仲の良いライバルグループ。",
12
+ "その他の組織名, 法人名, 地名, 人名",
13
+ 0.3,
14
+ True,
15
+ ],
16
+ [
17
+ "ner_jp",
18
+ "レッドフォックス株式会社は、東京都千代田区に本社を置くITサービス企業である",
19
+ "その他の組織名, 法人名, 地名, 人名",
20
+ 0.3,
21
+ False,
22
+ ],
23
+ ]
24
+
25
+
26
+ def ner(
27
+ text, models:str, labels: str, threshold: float, nested_ner: bool
28
+ ) -> Dict[str, Union[str, int, float]]:
29
+ labels = labels.split(",")
30
+ if models = "ner_jp":
31
+ model = jp_model
32
+ else:
33
+ model = meal_model
34
+ return {
35
+ "text": text,
36
+ "entities": [
37
+ {
38
+ "entity": entity["label"],
39
+ "word": entity["text"],
40
+ "start": entity["start"],
41
+ "end": entity["end"],
42
+ "score": 0,
43
+ }
44
+ for entity in model.predict_entities(
45
+ text, labels, flat_ner=not nested_ner, threshold=threshold
46
+ )
47
+ ],
48
+ }
49
+
50
+
51
+ with gr.Blocks(title="GLiNER-M-v2.1") as demo:
52
+ gr.Markdown(
53
+ """
54
+ # GLiNER-base
55
+ 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.
56
+ ## Links
57
+ * Model: https://huggingface.co/vumichien/ner-jp-gliner
58
+ * All GLiNER models: https://huggingface.co/models?library=gliner
59
+ * Paper: https://arxiv.org/abs/2311.08526
60
+ * Repository for finetune: https://github.com/vumichien/gliner-medium
61
+ """
62
+ )
63
+ with gr.Accordion("How to run this model locally", open=False):
64
+ gr.Markdown(
65
+ """
66
+ ## Installation
67
+ To use this model, you must install the GLiNER Python library:
68
+ ```
69
+ !pip install gliner
70
+ ```
71
+
72
+ ## Usage
73
+ Once you've downloaded the GLiNER library, you can import the GLiNER class. You can then load this model using `GLiNER.from_pretrained` and predict entities with `predict_entities`.
74
+ """
75
+ )
76
+ gr.Code(
77
+ '''
78
+ from gliner import GLiNER
79
+ model = GLiNER.from_pretrained("urchade/gliner_mediumv2.1")
80
+ text = """
81
+ Cristiano Ronaldo dos Santos Aveiro (Portuguese pronunciation: [kɾiʃˈtjɐnu ʁɔˈnaldu]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for and captains both Saudi Pro League club Al Nassr and the Portugal national team. Widely regarded as one of the greatest players of all time, Ronaldo has won five Ballon d'Or awards,[note 3] a record three UEFA Men's Player of the Year Awards, and four European Golden Shoes, the most by a European player. He has won 33 trophies in his career, including seven league titles, five UEFA Champions Leagues, the UEFA European Championship and the UEFA Nations League. Ronaldo holds the records for most appearances (183), goals (140) and assists (42) in the Champions League, goals in the European Championship (14), international goals (128) and international appearances (205). He is one of the few players to have made over 1,200 professional career appearances, the most by an outfield player, and has scored over 850 official senior career goals for club and country, making him the top goalscorer of all time.
82
+ """
83
+ labels = ["person", "award", "date", "competitions", "teams"]
84
+ entities = model.predict_entities(text, labels)
85
+ for entity in entities:
86
+ print(entity["text"], "=>", entity["label"])
87
+ ''',
88
+ language="python",
89
+ )
90
+ gr.Code(
91
+ """
92
+ Cristiano Ronaldo dos Santos Aveiro => person
93
+ 5 February 1985 => date
94
+ Al Nassr => teams
95
+ Portugal national team => teams
96
+ Ballon d'Or => award
97
+ UEFA Men's Player of the Year Awards => award
98
+ European Golden Shoes => award
99
+ UEFA Champions Leagues => competitions
100
+ UEFA European Championship => competitions
101
+ UEFA Nations League => competitions
102
+ Champions League => competitions
103
+ European Championship => competitions
104
+ """
105
+ )
106
+
107
+ input_text = gr.Textbox(
108
+ value=examples[0][0], label="Text input", placeholder="Enter your text here"
109
+ )
110
+ with gr.Row() as row:
111
+ models = gr.Dropdown((
112
+ choices=["ner_meals", "ner_jp"]
113
+ value="ner_jp",
114
+ label="Models",
115
+ placeholder="Enter your test model",
116
+ scale=2,
117
+ )
118
+ labels = gr.Textbox(
119
+ value=examples[0][2],
120
+ label="Labels",
121
+ placeholder="Enter your labels here (comma separated)",
122
+ scale=2,
123
+ )
124
+ threshold = gr.Slider(
125
+ 0,
126
+ 1,
127
+ value=0.3,
128
+ step=0.01,
129
+ label="Threshold",
130
+ info="Lower the threshold to increase how many entities get predicted.",
131
+ scale=1,
132
+ )
133
+ nested_ner = gr.Checkbox(
134
+ value=examples[0][2],
135
+ label="Nested NER",
136
+ info="Allow for nested NER?",
137
+ scale=0,
138
+ )
139
+ output = gr.HighlightedText(label="Predicted Entities")
140
+ submit_btn = gr.Button("Submit")
141
+ examples = gr.Examples(
142
+ examples,
143
+ fn=ner,
144
+ inputs=[input_text, models, labels, threshold, nested_ner],
145
+ outputs=output,
146
+ cache_examples=True,
147
+ )
148
+
149
+ # Submitting
150
+ input_text.submit(
151
+ fn=ner, inputs=[input_text, models, labels, threshold, nested_ner], outputs=output
152
+ )
153
+ models.submit(
154
+ fn=ner, inputs=[input_text, models, labels, threshold, nested_ner], outputs=output
155
+ )
156
+ labels.submit(
157
+ fn=ner, inputs=[input_text, models, labels, threshold, nested_ner], outputs=output
158
+ )
159
+ threshold.release(
160
+ fn=ner, inputs=[input_text, models, labels, threshold, nested_ner], outputs=output
161
+ )
162
+ submit_btn.click(
163
+ fn=ner, inputs=[input_text, models, labels, threshold, nested_ner], outputs=output
164
+ )
165
+ nested_ner.change(
166
+ fn=ner, inputs=[input_text, models, labels, threshold, nested_ner], outputs=output
167
+ )
168
+
169
+ demo.queue()
170
+ demo.launch()