File size: 8,822 Bytes
e0bb6f9
 
 
 
bb44c75
 
 
 
1e72efd
 
78e4eb9
 
 
 
 
db387fc
bb44c75
 
 
 
 
 
 
 
13067a4
1a693af
 
 
13067a4
1a693af
bb44c75
e0bb6f9
 
 
 
db387fc
 
e0bb6f9
 
a66b5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0bb6f9
 
 
 
bbf619c
e0bb6f9
 
bb44c75
e0bb6f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a43f2f
e0bb6f9
 
4a43f2f
e0bb6f9
 
 
 
 
 
 
4a43f2f
e0bb6f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb44c75
 
 
 
 
 
e0bb6f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb44c75
e0bb6f9
 
 
 
 
 
bb44c75
e0bb6f9
 
bb44c75
e0bb6f9
 
bb44c75
e0bb6f9
 
bb44c75
e0bb6f9
 
bb44c75
 
 
 
e0bb6f9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from typing import Dict, Union
from gliner import GLiNER
import gradio as gr

# Available models
AVAILABLE_MODELS = {
    "nvidia/gliner-PII": "nvidia/gliner-PII",
    "urchade/gliner_multi_pii-v1": "urchade/gliner_multi_pii-v1",
    # "BaoLocTown/gliner-vn-demo": "BaoLocTown/gliner-vn-demo",
    # "BaoLocTown/gliner-bi-large-v1.0-tuned-v3-50k": "BaoLocTown/gliner-bi-large-v1.0-tuned-v3-50k",
    "GreenNode/gliner-pii-base-v1.0-tuned": "GreenNode/gliner-pii-base-v1.0-tuned",
    # "GreenNode/gliner_multi_pii-v1-tuned": "GreenNode/gliner_multi_pii-v1-tuned",
    # "GreenNode/gliner-bi-large-v1.0-tuned": "GreenNode/gliner-bi-large-v1.0-tuned",
    "GreenNode/gliner_multi_pii-v1-tuned-2048": "GreenNode/gliner_multi_pii-v1-tuned-2048",
    "GreenNode/gliner-bi-large-v1.0-tuned-2048": "GreenNode/gliner-bi-large-v1.0-tuned-2048",
    
}

# Cache for loaded models
model_cache = {}

def get_model(model_name: str) -> GLiNER:
    """Get model from cache or load it if not cached."""
    if model_name not in model_cache:
        print(f"Loading model {model_name}")
        try:
            model_cache[model_name] = GLiNER.from_pretrained(model_name)
        except Exception as e:
            print(f"Error loading model {model_name}: {e}")
            raise e
    return model_cache[model_name]

examples = [
    [
        "Công ty TNHH XYZ tuyển dụng vị trí **Nhân viên Kinh doanh** với yêu cầu tốt nghiệp đại học, kỹ năng giao tiếp tốt và đam mê xây dựng mối quan hệ khách hàng. Quyền lợi hấp dẫn, lương cạnh tranh, thưởng theo doanh số và cơ hội thăng tiến. Liên hệ: **0903 123 456** hoặc **[tuyendung@xyz.com](mailto:tuyendung@xyz.com)**.",
        "person, company, phone, job title, email",
        0.3,
        False,
    ],
    [
        "Số hộ chiếu của ông Phạm Hùng Cường là A1234567 và ông đang cư trú tại địa chỉ 123 Đường Láng, Hà Nội. Ông cũng sở hữu số tài khoản ngân hàng 9876543210 tại Ngân hàng VietinBank.",
        "person, company, phone, job title, location, email, credit card number, date of birth, passport number, bank number",
        0.3,
        False
    ],
    [
        "Mã số bảo hiểm y tế của ông Trần Quang Minh là <b>987654321</b>, và số điện thoại của ông là 0987-654-321. Ông đang điều trị bệnh tại bệnh viện <i>Chợ Rẫy</i>, TP.HCM.",
        "person, company, phone, job title, location, email, credit card number, date of birth, passport number, bank number, student id number, organization, social security number, health insurance id number",
        0.3,
        False
    ],
    [
        "Chị **Hoàng Thị Lan**, mã số sinh viên **20237865**, đã đăng ký tham gia khóa học “Data Science” tại **Đại học FPT**, và đã cung cấp số tài khoản ngân hàng ***1234567890123456*** tại **Vietcombank**. Cô cũng đã thanh toán qua số thẻ tín dụng ***4321-8765-1234-5678*** và cung cấp số hộ chiếu <b>C1234567</b>. Chị Hoàng Thị Lan sinh ngày <i>15/08/1998</i> và có email là ***hoangthilan123@fpt.edu.vn***. Cô cũng đã cung cấp số điện thoại ***0909123456*** để nhận thông báo.",
        "person, company, phone, job title, location, email, credit card number, date of birth, passport number, bank number, student id number, organization, social security number, health insurance id number",
        0.3,
        False
    ]
]


def ner(
    text, labels: str, threshold: float, nested_ner: bool, model_name: str = "BaoLocTown/gliner-vn-demo"
) -> Dict[str, Union[str, int, float]]:
    labels = labels.split(",")
    model = get_model(model_name)
    return {
        "text": text,
        "entities": [
            {
                "entity": entity["label"],
                "word": entity["text"],
                "start": entity["start"],
                "end": entity["end"],
                "score": 0,
            }
            for entity in model.predict_entities(
                text, labels, flat_ner=not nested_ner, threshold=threshold
            )
        ],
    }


with gr.Blocks(title="VN-GLiNER") as demo:
    gr.Markdown(
        """
        # VN-GLiNER (Vietnamese Named Entity Recognition)

        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.

        The model has been trained by fine-tuning urchade/gliner_multi-v2.1 on the urchade/synthetic-pii-ner-mistral-v1 dataset.
        
        ## Links

        * All VN-GLiNER models: https://huggingface.co/collections/BaoLocTown/vn-gliner
        * Paper: https://arxiv.org/abs/2311.08526
        * Repository: https://github.com/urchade/GLiNER
        """
    )
    with gr.Accordion("How to run this model locally", open=False):
        gr.Markdown(
            """
            ## Installation
            To use this model, you must install the GLiNER Python library:
            ```
            !pip install gliner
            ```
         
            ## Usage
            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`.
            """
        )
        gr.Code(
            '''
from gliner import GLiNER

model = GLiNER.from_pretrained("urchade/gliner_multi_pii-v1")

text = """
Harilala Rasoanaivo, un homme d'affaires local d'Antananarivo, a enregistré une nouvelle société nommée "Rasoanaivo Enterprises" au Lot II M 92 Antohomadinika. Son numéro est le +261 32 22 345 67, et son adresse électronique est harilala.rasoanaivo@telma.mg. Il a fourni son numéro de sécu 501-02-1234 pour l'enregistrement.
"""

labels = ["work", "booking number", "personally identifiable information", "driver licence", "person", "book", "full address", "company", "actor", "character", "email", "passport number", "Social Security Number", "phone number"]
entities = model.predict_entities(text, labels)

for entity in entities:
    print(entity["text"], "=>", entity["label"])
            ''',
            language="python",
        )
        gr.Code(
            """
Harilala Rasoanaivo => person
Rasoanaivo Enterprises => company
Lot II M 92 Antohomadinika => full address
+261 32 22 345 67 => phone number
harilala.rasoanaivo@telma.mg => email
501-02-1234 => Social Security Number
            """
        )

    model_dropdown = gr.Dropdown(
        choices=list(AVAILABLE_MODELS.keys()),
        value="BaoLocTown/gliner-vn-demo",
        label="Model",
        info="Select the GLiNER model to use",
    )
    input_text = gr.Textbox(
        value=examples[0][0], label="Text input", placeholder="Enter your text here"
    )
    with gr.Row() as row:
        labels = gr.Textbox(
            value=examples[0][1],
            label="Labels",
            placeholder="Enter your labels here (comma separated)",
            scale=2,
        )
        threshold = gr.Slider(
            0,
            1,
            value=0.3,
            step=0.01,
            label="Threshold",
            info="Lower the threshold to increase how many entities get predicted.",
            scale=1,
        )
        nested_ner = gr.Checkbox(
            value=examples[0][2],
            label="Nested NER",
            info="Allow for nested NER?",
            scale=0,
        )
    output = gr.HighlightedText(label="Predicted Entities")
    submit_btn = gr.Button("Submit")
    examples = gr.Examples(
        examples,
        fn=ner,
        inputs=[input_text, labels, threshold, nested_ner, model_dropdown],
        outputs=output,
        cache_examples=True,
    )

    # Submitting
    input_text.submit(
        fn=ner, inputs=[input_text, labels, threshold, nested_ner, model_dropdown], outputs=output
    )
    labels.submit(
        fn=ner, inputs=[input_text, labels, threshold, nested_ner, model_dropdown], outputs=output
    )
    threshold.release(
        fn=ner, inputs=[input_text, labels, threshold, nested_ner, model_dropdown], outputs=output
    )
    submit_btn.click(
        fn=ner, inputs=[input_text, labels, threshold, nested_ner, model_dropdown], outputs=output
    )
    nested_ner.change(
        fn=ner, inputs=[input_text, labels, threshold, nested_ner, model_dropdown], outputs=output
    )
    model_dropdown.change(
        fn=ner, inputs=[input_text, labels, threshold, nested_ner, model_dropdown], outputs=output
    )

demo.queue()
demo.launch(debug=True)