shamik commited on
Commit
18c5816
1 Parent(s): ad57fa6

Added all the files for running the app.

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. README.md +2 -2
  3. app.py +156 -0
  4. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .ipynb_checkpoints/
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
  title: Extract Any Entity
3
- emoji: 📉
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 4.21.0
8
  app_file: app.py
9
- pinned: false
10
  license: apache-2.0
11
  ---
12
 
 
1
  ---
2
  title: Extract Any Entity
3
+ emoji: 🔥
4
  colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 4.21.0
8
  app_file: app.py
9
+ pinned: true
10
  license: apache-2.0
11
  ---
12
 
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Union
2
+ from gliner import GLiNER
3
+ import gradio as gr
4
+
5
+ model = GLiNER.from_pretrained("urchade/gliner_base")
6
+
7
+ examples = [
8
+ [
9
+ """
10
+ * Data Scientist, Data Analyst, or Data Engineer with 1+ years of experience.
11
+ * Experience with technologies such as Docker, Kubernetes, or Kubeflow
12
+ * Machine Learning experience preferred
13
+ * Experience with programming languages such as Python, C++, or SQL preferred
14
+ * Experience with technologies such as Databricks, Qlik, TensorFlow, PyTorch, Python, Dash, Pandas, or NumPy preferred
15
+ * BA or BS degree
16
+ * Active Secret OR Active Top Secret or Active TS/SCI clearance
17
+ """,
18
+ "software package, programing language, software tool, degree, job title",
19
+ 0.3,
20
+ False,
21
+ ],
22
+ [
23
+ "Synergy between signal transduction pathways is obligatory for expression of c-fos in B and T cell lines: implication for c-fos control via surface immunoglobulin and T cell antigen receptors.",
24
+ "DNA, RNA, cell line, cell type, protein",
25
+ 0.5,
26
+ False,
27
+ ],
28
+ [
29
+ "The choice of the encoder and decoder modules of dnpg can be quite flexible, for instance long short term memory networks (lstm) or convolutional neural network (cnn).",
30
+ "short acronym, long acronym",
31
+ 0.5,
32
+ False,
33
+ ],
34
+ [
35
+ "On 25 July 1948, on the 39th anniversary of Bleriot's crossing of the English Channel, the Type 618 Nene-Viking flew Heathrow to Paris (Villacoublay) in the morning carrying letters to Bleriot's widow and son (secretary of the FAI), who met it at the airport.",
36
+ "date, location, person, organization",
37
+ 0.5,
38
+ False,
39
+ ],
40
+ [
41
+ "Leo & Ian won the 1962 Bathurst Six Hour Classic at Mount Panorama driving a Daimler SP250 sports car, (that year the 500 mile race for touring cars were held at Phillip Island)",
42
+ "person, date, location, organization, competition",
43
+ 0.5,
44
+ False,
45
+ ],
46
+ [
47
+ "From November 29, 2011 to March 31, 2012, Karimloo returned to ``Les Misérables`` to play the lead role of Jean Valjean at The Queen's Theatre, London, for which he won the 2013 Theatregoers' Choice Award for Best Takeover in a Role.",
48
+ "person, actor, award, date, location",
49
+ 0.5,
50
+ False,
51
+ ],
52
+ [
53
+ "A Mexicali health clinic supported by former Baja California gubernatorial candidate Enrique Acosta Fregoso (PRI) was closed on June 15 after selling a supposed COVID-19 ``cure'' for between MXN $10,000 and $50,000.",
54
+ "location, organization, person, date, currency",
55
+ 0.5,
56
+ False,
57
+ ],
58
+ [
59
+ "Built in 1793, it was the home of Mary Young Pickersgill when she moved to Baltimore in 1806 and the location where she later sewed the ``Star Spangled Banner'', in 1813, the huge out-sized garrison flag that flew over Fort McHenry at Whetstone Point in Baltimore Harbor in the summer of 1814 during the British Royal Navy attack in the Battle of Baltimore during the War of 1812.",
60
+ "date, person, location, organization, event, flag",
61
+ 0.5,
62
+ False,
63
+ ],
64
+ ]
65
+
66
+
67
+ def ner(
68
+ text, labels: str, threshold: float, nested_ner: bool
69
+ ) -> Dict[str, Union[str, int, float]]:
70
+ labels = labels.split(",")
71
+ return {
72
+ "text": text,
73
+ "entities": [
74
+ {
75
+ "entity": entity["label"],
76
+ "word": entity["text"],
77
+ "start": entity["start"],
78
+ "end": entity["end"],
79
+ "score": 0,
80
+ }
81
+ for entity in model.predict_entities(
82
+ text, labels, flat_ner=not nested_ner, threshold=threshold
83
+ )
84
+ ],
85
+ }
86
+
87
+
88
+ with gr.Blocks(title="GLiNER") as demo:
89
+ gr.Markdown(
90
+ """
91
+ # GLiNER
92
+
93
+ [GLiNER](https://huggingface.co/urchade/gliner_base) 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.
94
+
95
+ ## Resources
96
+
97
+ * All GLiNER models: https://huggingface.co/models?library=gliner
98
+ * Repository: https://github.com/urchade/GLiNER
99
+ """
100
+ )
101
+
102
+
103
+ input_text = gr.Textbox(
104
+ value=examples[0][0], label="Text input", placeholder="Enter your text here"
105
+ )
106
+ with gr.Row() as row:
107
+ labels = gr.Textbox(
108
+ value=examples[0][1],
109
+ label="Labels",
110
+ placeholder="Enter your labels here (comma separated)",
111
+ scale=2,
112
+ )
113
+ threshold = gr.Slider(
114
+ 0,
115
+ 1,
116
+ value=0.5,
117
+ step=0.01,
118
+ label="Threshold",
119
+ info="Lower the threshold to increase how many entities get predicted.",
120
+ scale=1,
121
+ )
122
+ nested_ner = gr.Checkbox(
123
+ value=examples[0][2],
124
+ label="Nested NER",
125
+ info="Allow for nested NER.",
126
+ scale=0,
127
+ )
128
+ output = gr.HighlightedText(label="Predicted Entities")
129
+ submit_btn = gr.Button("Submit")
130
+ examples = gr.Examples(
131
+ examples,
132
+ fn=ner,
133
+ inputs=[input_text, labels, threshold, nested_ner],
134
+ outputs=output,
135
+ cache_examples=True,
136
+ )
137
+
138
+ # Submitting
139
+ input_text.submit(
140
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
141
+ )
142
+ labels.submit(
143
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
144
+ )
145
+ threshold.release(
146
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
147
+ )
148
+ submit_btn.click(
149
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
150
+ )
151
+ nested_ner.change(
152
+ fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
153
+ )
154
+
155
+ demo.queue(5)
156
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gliner