NER / app.py
Huu076's picture
Update app.py
cd0a19e verified
raw
history blame
4.02 kB
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)
#threshold = gr.Slider(0, 1, value=0.3, step=0.01, label="Threshold", info="Lower the threshold to increase how many entities get predicted.")
#nested_ner = gr.Checkbox(label="Nested NER", info="Allow for nested NER?")
output = gr.HighlightedText(label="Predicted Entities")
submit_btn = gr.Button("Submit")
examples = gr.Examples(
ner_examples,
fn=process,
#inputs=[input_text, labels, threshold, nested_ner],
inputs=[input_text, labels],
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()