File size: 3,735 Bytes
dc46405 4220c38 cd0a19e 4220c38 cd0a19e 4220c38 cd0a19e 4220c38 ca829f1 4220c38 ca829f1 4220c38 ca829f1 4220c38 dc46405 4220c38 |
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 |
import gradio as gr
from typing import Dict, Union
from gliner import GLiNER
import gradio as gr
model = GLiNER.from_pretrained("knowledgator/gliner-multitask-large-v0.5").to('cpu')
text1 = """
"The key driving factor creating a positive outlook for the global automotive market is the surge in demand for electric vehicles. In one of our previous articles about the best selling cars, trucks, and SUVs, we briefly discussed the growing interest in efficient electric vehicles. According to a report by the International Energy Agency, electric car sales are expected to continue growing strongly this year. More than 2.3 million electric cars were sold in the first quarter of 2023, which is almost a 25% increase from the same period last year.
On October 5, CNBC reported that LG Energy Solution Ltd (KRX:373220), one of the largest battery makers in the world, entered into an agreement to supply Toyota Motor Corporation (NYSE:TM) with EV batteries for electric cars that will be assembled in the US. According to the report, this agreement will help Toyota Motor Corporation (NYSE:TM), one of the best-selling car brands in the US, expand its battery EV line-up. Toyota Motor Corporation (NYSE:TM) intends to offer as many as 30 battery electric vehicle models across its Toyota and Lexus brands and produce up to 3.5 million battery electric vehicles each year by 2030."""
ner_examples = [[
text1,
"brand, date, quantity, car kind", 0.5, False
]]
def merge_entities(entities):
if not entities:
return []
merged = []
current = entities[0]
for next_entity in entities[1:]:
if next_entity['entity'] == current['entity'] and (next_entity['start'] == current['end'] + 1 or next_entity['start'] == current['end']):
current['word'] += ' ' + next_entity['word']
current['end'] = next_entity['end']
else:
merged.append(current)
current = next_entity
merged.append(current)
return merged
def process(
text, labels: str, threshold: float, nested_ner: bool
) -> Dict[str, Union[str, int, float]]:
labels = [label.strip() for label in labels.split(",")]
r = {
"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
)
],
}
r["entities"] = merge_entities(r["entities"])
return r
with gr.Blocks(title="NER Task") as ner_interface:
input_text = gr.Textbox(label="Text input", placeholder="Enter your text here")
labels = gr.Textbox(label="Labels", placeholder="Enter your labels here (comma separated)", scale=2)
output = gr.HighlightedText(label="Predicted Entities")
submit_btn = gr.Button("Submit")
examples = gr.Examples(
ner_examples,
fn=process,
inputs=[input_text, labels, 0.5, False],
outputs=output,
cache_examples=True
)
theme=gr.themes.Base()
input_text.submit(fn=process, inputs=[input_text, labels, threshold, nested_ner], outputs=output)
labels.submit(fn=process, inputs=[input_text, labels, threshold, nested_ner], outputs=output)
threshold.release(fn=process, inputs=[input_text, labels, threshold, nested_ner], outputs=output)
submit_btn.click(fn=process, inputs=[input_text, labels, threshold, nested_ner], outputs=output)
nested_ner.change(fn=process, inputs=[input_text, labels, threshold, nested_ner], outputs=output)
ner_interface.launch()
|