Spaces:
Sleeping
Sleeping
Update interfaces/relation_e.py
Browse files- interfaces/relation_e.py +98 -98
interfaces/relation_e.py
CHANGED
@@ -1,99 +1,99 @@
|
|
1 |
-
from utca.core import RenameAttribute
|
2 |
-
from utca.implementation.predictors import TokenSearcherPredictor, TokenSearcherPredictorConfig
|
3 |
-
from utca.implementation.tasks import TokenSearcherNER, TokenSearcherNERPostprocessor, TokenSearcherRelationExtraction, TokenSearcherRelationExtractionPostprocessor
|
4 |
-
from typing import Dict, Union
|
5 |
-
import gradio as gr
|
6 |
-
|
7 |
-
text = """
|
8 |
-
Dr. Paul Hammond, a renowned neurologist at Johns Hopkins University, has recently published a paper in the prestigious journal \"Nature Neuroscience\".
|
9 |
-
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.
|
10 |
-
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.
|
11 |
-
Funded by the National Institutes of Health, their research could potentially open new avenues for Alzheimer's treatment.
|
12 |
-
"""
|
13 |
-
predictor = TokenSearcherPredictor(
|
14 |
-
TokenSearcherPredictorConfig(
|
15 |
-
device="
|
16 |
-
model="knowledgator/UTC-DeBERTa-large-v2"
|
17 |
-
)
|
18 |
-
)
|
19 |
-
|
20 |
-
pipe = (
|
21 |
-
TokenSearcherNER( # TokenSearcherNER task produces classified entities that will be at the "output" key.
|
22 |
-
predictor=predictor,
|
23 |
-
postprocess=TokenSearcherNERPostprocessor(
|
24 |
-
threshold=0.5 # Entity threshold
|
25 |
-
)
|
26 |
-
)
|
27 |
-
| RenameAttribute("output", "entities") # Rename output entities from TokenSearcherNER task to use them as inputs in TokenSearcherRelationExtraction
|
28 |
-
| TokenSearcherRelationExtraction( # TokenSearcherRelationExtraction is used for relation extraction.
|
29 |
-
predictor=predictor,
|
30 |
-
postprocess=TokenSearcherRelationExtractionPostprocessor(
|
31 |
-
threshold=0.5 # Relation threshold
|
32 |
-
)
|
33 |
-
)
|
34 |
-
)
|
35 |
-
|
36 |
-
|
37 |
-
def process(
|
38 |
-
relation: str, text, distance_threshold: str, pairs_filter: str, labels: str
|
39 |
-
) -> Dict[str, Union[str, int, float]]:
|
40 |
-
pairs_filter = [tuple(pair.strip() for pair in pair.split("->")) for pair in pairs_filter.split(",")]
|
41 |
-
|
42 |
-
if len(distance_threshold) < 1 or not distance_threshold or not distance_threshold.strip().isdigit():
|
43 |
-
r = pipe.run({
|
44 |
-
"text": text,
|
45 |
-
"labels": [label.strip() for label in labels.split(",")],
|
46 |
-
"relations": [{
|
47 |
-
"relation": relation,
|
48 |
-
"pairs_filter": pairs_filter
|
49 |
-
}]
|
50 |
-
})
|
51 |
-
elif int(distance_threshold.strip()):
|
52 |
-
r = pipe.run({
|
53 |
-
"text": text,
|
54 |
-
"labels": [label.strip() for label in labels.split(",")],
|
55 |
-
"relations": [{
|
56 |
-
"relation": relation,
|
57 |
-
"pairs_filter": pairs_filter,
|
58 |
-
"distance_threshold": int(distance_threshold.replace(" ", ""))
|
59 |
-
}]
|
60 |
-
})
|
61 |
-
|
62 |
-
return r["output"]
|
63 |
-
|
64 |
-
relation_e_examples = [
|
65 |
-
[
|
66 |
-
"worked at",
|
67 |
-
text,
|
68 |
-
"None",
|
69 |
-
"scientist -> university, scientist -> other",
|
70 |
-
"scientist, university, city, research, journal"]
|
71 |
-
]
|
72 |
-
|
73 |
-
with gr.Blocks(title="Open Information Extracting") as relation_e_interface:
|
74 |
-
relation = gr.Textbox(label="Relation", placeholder="Enter relation you want to extract here")
|
75 |
-
input_text = gr.Textbox(label="Text input", placeholder="Enter your text here")
|
76 |
-
labels = gr.Textbox(label="Labels", placeholder="Enter your labels here (comma separated)", scale=2)
|
77 |
-
pairs_filter = gr.Textbox(label="Pairs Filter", placeholder="It specifies possible members of relations by their entity labels. Write as: source -> target,..")
|
78 |
-
distance_threshold = gr.Textbox(label="Distance Threshold", placeholder="It specifies the max distance in characters between spans in the text")
|
79 |
-
output = gr.Textbox(label="Predicted Relation")
|
80 |
-
submit_btn = gr.Button("Submit")
|
81 |
-
examples = gr.Examples(
|
82 |
-
relation_e_examples,
|
83 |
-
fn=process,
|
84 |
-
inputs=[relation, input_text, distance_threshold, pairs_filter, labels],
|
85 |
-
outputs=output,
|
86 |
-
cache_examples=True
|
87 |
-
)
|
88 |
-
theme=gr.themes.Base()
|
89 |
-
|
90 |
-
input_text.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
91 |
-
labels.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
92 |
-
pairs_filter.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
93 |
-
submit_btn.click(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
94 |
-
distance_threshold.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
95 |
-
|
96 |
-
|
97 |
-
if __name__ == "__main__":
|
98 |
-
|
99 |
relation_e_interface.launch()
|
|
|
1 |
+
from utca.core import RenameAttribute
|
2 |
+
from utca.implementation.predictors import TokenSearcherPredictor, TokenSearcherPredictorConfig
|
3 |
+
from utca.implementation.tasks import TokenSearcherNER, TokenSearcherNERPostprocessor, TokenSearcherRelationExtraction, TokenSearcherRelationExtractionPostprocessor
|
4 |
+
from typing import Dict, Union
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
text = """
|
8 |
+
Dr. Paul Hammond, a renowned neurologist at Johns Hopkins University, has recently published a paper in the prestigious journal \"Nature Neuroscience\".
|
9 |
+
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.
|
10 |
+
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.
|
11 |
+
Funded by the National Institutes of Health, their research could potentially open new avenues for Alzheimer's treatment.
|
12 |
+
"""
|
13 |
+
predictor = TokenSearcherPredictor(
|
14 |
+
TokenSearcherPredictorConfig(
|
15 |
+
device="cpu",
|
16 |
+
model="knowledgator/UTC-DeBERTa-large-v2"
|
17 |
+
)
|
18 |
+
)
|
19 |
+
|
20 |
+
pipe = (
|
21 |
+
TokenSearcherNER( # TokenSearcherNER task produces classified entities that will be at the "output" key.
|
22 |
+
predictor=predictor,
|
23 |
+
postprocess=TokenSearcherNERPostprocessor(
|
24 |
+
threshold=0.5 # Entity threshold
|
25 |
+
)
|
26 |
+
)
|
27 |
+
| RenameAttribute("output", "entities") # Rename output entities from TokenSearcherNER task to use them as inputs in TokenSearcherRelationExtraction
|
28 |
+
| TokenSearcherRelationExtraction( # TokenSearcherRelationExtraction is used for relation extraction.
|
29 |
+
predictor=predictor,
|
30 |
+
postprocess=TokenSearcherRelationExtractionPostprocessor(
|
31 |
+
threshold=0.5 # Relation threshold
|
32 |
+
)
|
33 |
+
)
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
def process(
|
38 |
+
relation: str, text, distance_threshold: str, pairs_filter: str, labels: str
|
39 |
+
) -> Dict[str, Union[str, int, float]]:
|
40 |
+
pairs_filter = [tuple(pair.strip() for pair in pair.split("->")) for pair in pairs_filter.split(",")]
|
41 |
+
|
42 |
+
if len(distance_threshold) < 1 or not distance_threshold or not distance_threshold.strip().isdigit():
|
43 |
+
r = pipe.run({
|
44 |
+
"text": text,
|
45 |
+
"labels": [label.strip() for label in labels.split(",")],
|
46 |
+
"relations": [{
|
47 |
+
"relation": relation,
|
48 |
+
"pairs_filter": pairs_filter
|
49 |
+
}]
|
50 |
+
})
|
51 |
+
elif int(distance_threshold.strip()):
|
52 |
+
r = pipe.run({
|
53 |
+
"text": text,
|
54 |
+
"labels": [label.strip() for label in labels.split(",")],
|
55 |
+
"relations": [{
|
56 |
+
"relation": relation,
|
57 |
+
"pairs_filter": pairs_filter,
|
58 |
+
"distance_threshold": int(distance_threshold.replace(" ", ""))
|
59 |
+
}]
|
60 |
+
})
|
61 |
+
|
62 |
+
return r["output"]
|
63 |
+
|
64 |
+
relation_e_examples = [
|
65 |
+
[
|
66 |
+
"worked at",
|
67 |
+
text,
|
68 |
+
"None",
|
69 |
+
"scientist -> university, scientist -> other",
|
70 |
+
"scientist, university, city, research, journal"]
|
71 |
+
]
|
72 |
+
|
73 |
+
with gr.Blocks(title="Open Information Extracting") as relation_e_interface:
|
74 |
+
relation = gr.Textbox(label="Relation", placeholder="Enter relation you want to extract here")
|
75 |
+
input_text = gr.Textbox(label="Text input", placeholder="Enter your text here")
|
76 |
+
labels = gr.Textbox(label="Labels", placeholder="Enter your labels here (comma separated)", scale=2)
|
77 |
+
pairs_filter = gr.Textbox(label="Pairs Filter", placeholder="It specifies possible members of relations by their entity labels. Write as: source -> target,..")
|
78 |
+
distance_threshold = gr.Textbox(label="Distance Threshold", placeholder="It specifies the max distance in characters between spans in the text")
|
79 |
+
output = gr.Textbox(label="Predicted Relation")
|
80 |
+
submit_btn = gr.Button("Submit")
|
81 |
+
examples = gr.Examples(
|
82 |
+
relation_e_examples,
|
83 |
+
fn=process,
|
84 |
+
inputs=[relation, input_text, distance_threshold, pairs_filter, labels],
|
85 |
+
outputs=output,
|
86 |
+
cache_examples=True
|
87 |
+
)
|
88 |
+
theme=gr.themes.Base()
|
89 |
+
|
90 |
+
input_text.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
91 |
+
labels.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
92 |
+
pairs_filter.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
93 |
+
submit_btn.click(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
94 |
+
distance_threshold.submit(fn=process, inputs=[relation, input_text, distance_threshold, pairs_filter, labels], outputs=output)
|
95 |
+
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
|
99 |
relation_e_interface.launch()
|