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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from typing import Dict, Union
|
| 5 |
+
from gliner import GLiNER
|
| 6 |
+
|
| 7 |
+
model = GLiNER.from_pretrained("numind/NuNER_Zero")
|
| 8 |
+
|
| 9 |
+
classifier = pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-base-zeroshot-v1")
|
| 10 |
+
|
| 11 |
+
css = """
|
| 12 |
+
h1 {
|
| 13 |
+
text-align: center;
|
| 14 |
+
display:block;
|
| 15 |
+
}
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
#define a function to process your input and output
|
| 19 |
+
def zero_shot(doc, candidates):
|
| 20 |
+
given_labels = candidates.split(", ")
|
| 21 |
+
dictionary = classifier(doc, given_labels)
|
| 22 |
+
labels = dictionary['labels']
|
| 23 |
+
scores = dictionary['scores']
|
| 24 |
+
return dict(zip(labels, scores))
|
| 25 |
+
|
| 26 |
+
examples_text = [
|
| 27 |
+
[
|
| 28 |
+
"Pasar saham ngalaman panurunan nu signifikan akibat kateupastian global.",
|
| 29 |
+
"ékonomi, pulitik, bisnis, kauangan, téknologi"
|
| 30 |
+
],
|
| 31 |
+
[
|
| 32 |
+
"I am very happy today but suddenly sad because of the recent news.",
|
| 33 |
+
"positive, negative, neutral"
|
| 34 |
+
],
|
| 35 |
+
[
|
| 36 |
+
"I just received the best news ever! I got the job I always wanted!",
|
| 37 |
+
"joy, sadness, anger, surprise, fear, disgust"
|
| 38 |
+
],
|
| 39 |
+
]
|
| 40 |
+
examples_ner = [
|
| 41 |
+
[
|
| 42 |
+
"Pada tahun 1945, Indonesia memproklamasikan kemerdekaannya dari penjajahan Belanda. Proklamasi tersebut dibacakan oleh Soekarno dan Mohammad Hatta di Jakarta.",
|
| 43 |
+
"tahun, negara, tokoh, lokasi",
|
| 44 |
+
0.3
|
| 45 |
+
],
|
| 46 |
+
[
|
| 47 |
+
"Mount Everest is the highest mountain above sea level, located in the Himalayas. It stands at 8,848 meters (29,029 ft) and attracts many climbers.",
|
| 48 |
+
"location, measurement, person",
|
| 49 |
+
0.3
|
| 50 |
+
],
|
| 51 |
+
[
|
| 52 |
+
"Perusahaan teknologi raksasa, Google, mbukak kantor cabang anyar ing Jakarta ing wulan Januari 2020 kanggo nggedhekake operasine ing Asia Tenggara",
|
| 53 |
+
"perusahaan, lokasi, wulan, taun",
|
| 54 |
+
0.3
|
| 55 |
+
],
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
def merge_entities(entities):
|
| 59 |
+
if not entities:
|
| 60 |
+
return []
|
| 61 |
+
merged = []
|
| 62 |
+
current = entities[0]
|
| 63 |
+
for next_entity in entities[1:]:
|
| 64 |
+
if next_entity['entity'] == current['entity'] and (next_entity['start'] == current['end'] + 1 or next_entity['start'] == current['end']):
|
| 65 |
+
current['word'] += ' ' + next_entity['word']
|
| 66 |
+
current['end'] = next_entity['end']
|
| 67 |
+
else:
|
| 68 |
+
merged.append(current)
|
| 69 |
+
current = next_entity
|
| 70 |
+
merged.append(current)
|
| 71 |
+
return merged
|
| 72 |
+
|
| 73 |
+
def ner(
|
| 74 |
+
text, labels: str, threshold: float, nested_ner: bool
|
| 75 |
+
) -> Dict[str, Union[str, int, float]]:
|
| 76 |
+
labels = labels.split(",")
|
| 77 |
+
r = {
|
| 78 |
+
"text": text,
|
| 79 |
+
"entities": [
|
| 80 |
+
{
|
| 81 |
+
"entity": entity["label"],
|
| 82 |
+
"word": entity["text"],
|
| 83 |
+
"start": entity["start"],
|
| 84 |
+
"end": entity["end"],
|
| 85 |
+
"score": 0,
|
| 86 |
+
}
|
| 87 |
+
for entity in model.predict_entities(
|
| 88 |
+
text, labels, flat_ner=not nested_ner, threshold=threshold
|
| 89 |
+
)
|
| 90 |
+
],
|
| 91 |
+
}
|
| 92 |
+
r["entities"] = merge_entities(r["entities"])
|
| 93 |
+
return r
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(title="Zero-Shot Demo", css=css) as demo: #, theme=gr.themes.Soft()
|
| 97 |
+
|
| 98 |
+
gr.Markdown(
|
| 99 |
+
"""
|
| 100 |
+
# Zero-Shot Model Demo
|
| 101 |
+
"""
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
#create input and output objects
|
| 105 |
+
with gr.Tab("Zero-Shot Text Classification"):
|
| 106 |
+
|
| 107 |
+
gr.Markdown(
|
| 108 |
+
"""
|
| 109 |
+
## Zero-Shot Text Classification
|
| 110 |
+
"""
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
input1 = gr.Textbox(label="Text", value=examples_text[0][0])
|
| 114 |
+
input2 = gr.Textbox(label="Labels",value=examples_text[0][1])
|
| 115 |
+
output = gr.Label(label="Output")
|
| 116 |
+
|
| 117 |
+
gui = gr.Interface(
|
| 118 |
+
# title="Zero-Shot Text Classification",
|
| 119 |
+
fn=zero_shot,
|
| 120 |
+
inputs=[input1, input2],
|
| 121 |
+
outputs=[output]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
examples = gr.Examples(
|
| 125 |
+
examples_text,
|
| 126 |
+
fn=zero_shot,
|
| 127 |
+
inputs=[input1, input2],
|
| 128 |
+
outputs=output,
|
| 129 |
+
cache_examples=True,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
with gr.Tab("Zero-Shot NER"):
|
| 134 |
+
gr.Markdown(
|
| 135 |
+
"""
|
| 136 |
+
## Zero-Shot Named Entity Recognition (NER)
|
| 137 |
+
"""
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
input_text = gr.Textbox(
|
| 141 |
+
value=examples_ner[0][0], label="Text input", placeholder="Enter your text here", lines=3
|
| 142 |
+
)
|
| 143 |
+
with gr.Row() as row:
|
| 144 |
+
labels = gr.Textbox(
|
| 145 |
+
value=examples_ner[0][1],
|
| 146 |
+
label="Labels",
|
| 147 |
+
placeholder="Enter your labels here (comma separated)",
|
| 148 |
+
scale=2,
|
| 149 |
+
)
|
| 150 |
+
threshold = gr.Slider(
|
| 151 |
+
0,
|
| 152 |
+
1,
|
| 153 |
+
value=examples_ner[0][2],
|
| 154 |
+
step=0.01,
|
| 155 |
+
label="Threshold",
|
| 156 |
+
info="Lower the threshold to increase how many entities get predicted.",
|
| 157 |
+
scale=1,
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
output = gr.HighlightedText(label="Predicted Entities")
|
| 161 |
+
|
| 162 |
+
submit_btn = gr.Button("Submit")
|
| 163 |
+
|
| 164 |
+
examples = gr.Examples(
|
| 165 |
+
examples_ner,
|
| 166 |
+
fn=ner,
|
| 167 |
+
inputs=[input_text, labels, threshold],
|
| 168 |
+
outputs=output,
|
| 169 |
+
cache_examples=True,
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
submit_btn.click(
|
| 173 |
+
fn=ner, inputs=[input_text, labels, threshold], outputs=output
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
demo.queue()
|
| 177 |
+
demo.launch(debug=True)
|