File size: 4,840 Bytes
1218161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from utca.core import RenameAttribute
from utca.implementation.predictors import (
    GLiNERPredictor,
    GLiNERPredictorConfig
)
from utca.implementation.tasks import (
    GLiNER,
    GLiNERPreprocessor,
    GLiNERRelationExtraction,
    GLiNERRelationExtractionPreprocessor,
)
from typing import Dict, Union
import gradio as gr

text = """

Dr. Paul Hammond, a renowned neurologist at Johns Hopkins University, has recently published a paper in the prestigious journal \"Nature Neuroscience\".

 His research focuses on a rare genetic mutation, found in less than 0.01% of the population, that appears to prevent the development of Alzheimer's disease.

 Collaborating with researchers at the University of California, San Francisco, the team is now working to understand the mechanism by which this mutation confers its protective effect.

 Funded by the National Institutes of Health, their research could potentially open new avenues for Alzheimer's treatment.

"""

predictor = GLiNERPredictor( # Predictor manages the model that will be used by tasks
    GLiNERPredictorConfig(
        model_name = "knowledgator/gliner-multitask-large-v0.5", # Model to use
        device = "cpu", # Device to use
    )
)

pipe = (
    GLiNER( # GLiNER task produces classified entities that will be at the "output" key.
        predictor=predictor,
        preprocess=GLiNERPreprocessor(threshold=0.7) # Entities threshold
    ) 
    | RenameAttribute("output", "entities") # Rename output entities from GLiNER task to use them as inputs in GLiNERRelationExtraction
    | GLiNERRelationExtraction( # GLiNERRelationExtraction is used for relation extraction.
        predictor=predictor,
        preprocess=(
            GLiNERPreprocessor(threshold=0.7) # Relations threshold
            | GLiNERRelationExtractionPreprocessor()
        )
    )
)


def process(

    relation: str, text, distance_threshold: str, pairs_filter: str, labels: str

) -> Dict[str, Union[str, int, float]]:
    pairs_filter = [tuple(pair.strip() for pair in pair.split("->")) for pair in pairs_filter.split(",")]

    if len(distance_threshold) < 1 or not distance_threshold or not distance_threshold.strip().isdigit():
        r = pipe.run({
            "text": text, 
            "labels": [label.strip() for label in labels.split(",")],
            "relations": [{
                "relation": relation,
                "pairs_filter": pairs_filter
                }]
                })
    elif int(distance_threshold.strip()):
        r = pipe.run({
            "text": text, 
            "labels": [label.strip() for label in labels.split(",")],
            "relations": [{
                "relation": relation,
                "pairs_filter": pairs_filter,
                "distance_threshold": int(distance_threshold.replace(" ", ""))
                }]
                })
   
    return r["output"]

relation_e_examples = [
    [
    "worked at",
    text,
    "None",
    "scientist -> university, scientist -> other",
    "scientist, university, city, research, journal"]
    ]

with gr.Blocks(title="Open Information Extracting") as relation_e_interface:
    relation = gr.Textbox(label="Relation", placeholder="Enter relation you want to extract here")
    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)
    pairs_filter = gr.Textbox(label="Pairs Filter", placeholder="It specifies possible members of relations by their entity labels. Write as: source -> target,..")
    distance_threshold =  gr.Textbox(label="Distance Threshold", placeholder="It specifies the max distance in characters between spans in the text")
    output = gr.Textbox(label="Predicted Relation")
    submit_btn = gr.Button("Submit")
    examples = gr.Examples(
    relation_e_examples,
    fn=process,
    inputs=[relation, input_text, distance_threshold, pairs_filter, labels],
    outputs=output,
    cache_examples=True
    )
    theme=gr.themes.Base()

    input_text.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
    labels.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
    pairs_filter.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
    submit_btn.click(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
    distance_threshold.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)


if __name__ == "__main__":
    
    relation_e_interface.launch()