|
from typing import Dict, Union |
|
from gliner import GLiNER |
|
import gradio as gr |
|
|
|
model = GLiNER.from_pretrained("xomad/gliner-model-merge-large-v1.0").to('cpu') |
|
|
|
|
|
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( |
|
prompt:str, text, threshold: float, nested_ner: bool, labels: str = ["match"] |
|
) -> Dict[str, Union[str, int, float]]: |
|
text = prompt + "\n" + text |
|
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="Open Information Extracting") as open_ie_interface: |
|
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here") |
|
input_text = gr.Textbox(label="Text input", placeholder="Enter your text here") |
|
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") |
|
|
|
theme=gr.themes.Base() |
|
|
|
input_text.submit(fn=process, inputs=[prompt, input_text, threshold, nested_ner], outputs=output) |
|
prompt.submit(fn=process, inputs=[prompt, input_text, threshold, nested_ner], outputs=output) |
|
threshold.release(fn=process, inputs=[prompt, input_text, threshold, nested_ner], outputs=output) |
|
submit_btn.click(fn=process, inputs=[prompt, input_text, threshold, nested_ner], outputs=output) |
|
nested_ner.change(fn=process, inputs=[prompt, input_text, threshold, nested_ner], outputs=output) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
open_ie_interface.launch() |