wjbmattingly commited on
Commit
750cb42
·
verified ·
1 Parent(s): 6a4bf95

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +6 -6
  2. app.py +158 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Gliner Small-v2.1-holocaust
3
- emoji: 📉
4
- colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.31.5
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: GLiNER-Multiv2.1
3
+ emoji: 💻
4
+ colorFrom: pink
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 4.20.1
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Union
2
+ from gliner import GLiNER
3
+ import gradio as gr
4
+
5
+ model = GLiNER.from_pretrained("placingholocaust/gliner_small-v2.1-holocaust")
6
+
7
+ examples = [
8
+ [
9
+ "Okay. So now it's spring of '44? A: ‘4, And she says, You're going to go to Brzezinka . I said, What is Brzezinka ? She said, It's a crematorium and the gas chamber . They have a half a million Hungarian Jews are coming in. That's when the time they -- and they need people to select. We do not select the people to -- who die or not. The women fold the clothes and look for jewelry and make packages to send it to Germany.",
10
+ "dlf, populated place, country, region, interior space, env feature, building, spatial object.",
11
+ 0.3,
12
+ True,
13
+ ]
14
+ ]
15
+
16
+
17
+ def ner(
18
+ text, labels: str, threshold: float, nested_ner: bool
19
+ ) -> Dict[str, Union[str, int, float]]:
20
+ labels = labels.split(",")
21
+ return {
22
+ "text": text,
23
+ "entities": [
24
+ {
25
+ "entity": entity["label"],
26
+ "word": entity["text"],
27
+ "start": entity["start"],
28
+ "end": entity["end"],
29
+ "score": 0,
30
+ }
31
+ for entity in model.predict_entities(
32
+ text, labels, flat_ner=not nested_ner, threshold=threshold
33
+ )
34
+ ],
35
+ }
36
+
37
+
38
+ with gr.Blocks(title="GLiNER-M-v2.1") as demo:
39
+ gr.Markdown("# GliNER model for Holocaust NER.")
40
+ with gr.Accordion("About this model.", open=False):
41
+ gr.Markdown(
42
+ """
43
+ # GLiNER-base
44
+
45
+ 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.
46
+
47
+ ## Links
48
+
49
+ * Original GliNER model: https://huggingface.co/urchade/gliner_small-v2.1
50
+ * Finetuned GliNER model: https://huggingface.co/placingholocaust/gliner_small-v2.1-holocaust
51
+ * Finetuned with this data: https://huggingface.co/datasets/placingholocaust/spacy-project
52
+ * Paper: https://arxiv.org/abs/2311.08526
53
+ * Repository: https://github.com/urchade/GLiNER
54
+ """
55
+ )
56
+ # with gr.Accordion("How to run this model locally", open=False):
57
+ # gr.Markdown(
58
+ # """
59
+ # ## Installation
60
+ # To use this model, you must install the GLiNER Python library:
61
+ # ```
62
+ # !pip install gliner
63
+ # ```
64
+
65
+ # ## Usage
66
+ # 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`.
67
+ # """
68
+ # )
69
+ # gr.Code(
70
+ # '''
71
+ # from gliner import GLiNER
72
+
73
+ # model = GLiNER.from_pretrained("urchade/gliner_mediumv2.1")
74
+
75
+ # text = """
76
+ # 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.
77
+ # """
78
+
79
+ # labels = ["person", "award", "date", "competitions", "teams"]
80
+
81
+ # entities = model.predict_entities(text, labels)
82
+
83
+ # for entity in entities:
84
+ # print(entity["text"], "=>", entity["label"])
85
+ # ''',
86
+ # language="python",
87
+ # )
88
+ # gr.Code(
89
+ # """
90
+ # Cristiano Ronaldo dos Santos Aveiro => person
91
+ # 5 February 1985 => date
92
+ # Al Nassr => teams
93
+ # Portugal national team => teams
94
+ # Ballon d'Or => award
95
+ # UEFA Men's Player of the Year Awards => award
96
+ # European Golden Shoes => award
97
+ # UEFA Champions Leagues => competitions
98
+ # UEFA European Championship => competitions
99
+ # UEFA Nations League => competitions
100
+ # Champions League => competitions
101
+ # European Championship => competitions
102
+ # """
103
+ # )
104
+
105
+ input_text = gr.Textbox(
106
+ value=examples[0][0], label="Text input", placeholder="Enter your text here"
107
+ )
108
+ with gr.Row() as row:
109
+ labels = gr.Textbox(
110
+ value=examples[0][1],
111
+ label="Labels",
112
+ placeholder="Enter your labels here (comma separated)",
113
+ scale=2,
114
+ )
115
+ threshold = gr.Slider(
116
+ 0,
117
+ 1,
118
+ value=0.3,
119
+ step=0.01,
120
+ label="Threshold",
121
+ info="Lower the threshold to increase how many entities get predicted.",
122
+ scale=1,
123
+ )
124
+ nested_ner = gr.Checkbox(
125
+ value=examples[0][2],
126
+ label="Nested NER",
127
+ info="Allow for nested NER?",
128
+ scale=0,
129
+ )
130
+ output = gr.HighlightedText(label="Predicted Entities")
131
+ submit_btn = gr.Button("Submit")
132
+ examples = gr.Examples(
133
+ examples,
134
+ fn=ner,
135
+ inputs=[input_text, labels, threshold, nested_ner],
136
+ outputs=output,
137
+ cache_examples=True,
138
+ )
139
+
140
+ # Submitting
141
+ input_text.submit(
142
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
143
+ )
144
+ labels.submit(
145
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
146
+ )
147
+ threshold.release(
148
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
149
+ )
150
+ submit_btn.click(
151
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
152
+ )
153
+ nested_ner.change(
154
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
155
+ )
156
+
157
+ demo.queue()
158
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gliner