Spaces:
Sleeping
Sleeping
updating app
Browse files- app.py +65 -23
- requirements.txt +2 -1
app.py
CHANGED
@@ -10,6 +10,8 @@ from PIL import Image
|
|
10 |
from accord_nlp.information_extraction.convertor import entity_pairing, graph_building
|
11 |
from accord_nlp.information_extraction.ie_pipeline import InformationExtractor
|
12 |
|
|
|
|
|
13 |
ner_args = {
|
14 |
"labels_list": ["O", "B-quality", "B-property", "I-property", "I-quality", "B-object", "I-object", "B-value", "I-value"],
|
15 |
"use_multiprocessing": False,
|
@@ -40,6 +42,14 @@ with st.spinner(text="Initialising..."):
|
|
40 |
ie = init()
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
def main():
|
44 |
image = Image.open(os.path.join(os.path.dirname(__file__), 'accord_logo.png'))
|
45 |
st.sidebar.image(image)
|
@@ -48,36 +58,68 @@ def main():
|
|
48 |
st.sidebar.header("Information Extractor")
|
49 |
st.sidebar.markdown("Extract entities and their relations from textual data")
|
50 |
st.sidebar.markdown(
|
51 |
-
"[
|
|
|
|
|
|
|
52 |
)
|
53 |
|
|
|
|
|
|
|
|
|
|
|
54 |
st.header("Input a sentence")
|
55 |
|
56 |
txt = st.text_area('Sentence')
|
57 |
|
58 |
if txt:
|
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 |
if __name__ == '__main__':
|
|
|
10 |
from accord_nlp.information_extraction.convertor import entity_pairing, graph_building
|
11 |
from accord_nlp.information_extraction.ie_pipeline import InformationExtractor
|
12 |
|
13 |
+
from trubrics.integrations.streamlit import FeedbackCollector
|
14 |
+
|
15 |
ner_args = {
|
16 |
"labels_list": ["O", "B-quality", "B-property", "I-property", "I-quality", "B-object", "I-object", "B-value", "I-value"],
|
17 |
"use_multiprocessing": False,
|
|
|
42 |
ie = init()
|
43 |
|
44 |
|
45 |
+
collector = FeedbackCollector(
|
46 |
+
# component_name="default",
|
47 |
+
email=st.secrets["TRUBRICS_EMAIL"],
|
48 |
+
password=st.secrets["TRUBRICS_PASSWORD"],
|
49 |
+
project="test"
|
50 |
+
)
|
51 |
+
|
52 |
+
|
53 |
def main():
|
54 |
image = Image.open(os.path.join(os.path.dirname(__file__), 'accord_logo.png'))
|
55 |
st.sidebar.image(image)
|
|
|
58 |
st.sidebar.header("Information Extractor")
|
59 |
st.sidebar.markdown("Extract entities and their relations from textual data")
|
60 |
st.sidebar.markdown(
|
61 |
+
"[codebase](https://github.com/Accord-Project/NLP-Framework)"
|
62 |
+
)
|
63 |
+
st.sidebar.markdown(
|
64 |
+
"[models](https://huggingface.co/ACCORD-NLP)"
|
65 |
)
|
66 |
|
67 |
+
if 'text' not in st.session_state:
|
68 |
+
st.session_state['text'] = ''
|
69 |
+
if 'graph' not in st.session_state:
|
70 |
+
st.session_state['graph'] = None
|
71 |
+
|
72 |
st.header("Input a sentence")
|
73 |
|
74 |
txt = st.text_area('Sentence')
|
75 |
|
76 |
if txt:
|
77 |
+
if txt == st.session_state['text']:
|
78 |
+
st.header('Entity-Relation Representation')
|
79 |
+
st.graphviz_chart(st.session_state['graph'], use_container_width=True)
|
80 |
+
|
81 |
+
else:
|
82 |
+
st.session_state['text'] = txt
|
83 |
+
st.session_state['graph'] = None
|
84 |
+
|
85 |
+
# preprocess
|
86 |
+
sentence = ie.preprocess(txt)
|
87 |
+
|
88 |
+
# NER
|
89 |
+
with st.spinner(text="Recognising entities..."):
|
90 |
+
ner_predictions, ner_raw_outputs = ie.ner_model.predict([sentence])
|
91 |
+
|
92 |
+
with st.spinner(text="Extracting relations..."):
|
93 |
+
# pair entities to predict their relations
|
94 |
+
entity_pair_df = entity_pairing(sentence, ner_predictions[0])
|
95 |
+
|
96 |
+
# relation extraction
|
97 |
+
re_predictions, re_raw_outputs = ie.re_model.predict(entity_pair_df['output'].tolist())
|
98 |
+
entity_pair_df['prediction'] = re_predictions
|
99 |
+
|
100 |
+
with st.spinner(text="Building graph..."):
|
101 |
+
# build graph
|
102 |
+
graph = graph_building(entity_pair_df, view=False)
|
103 |
+
|
104 |
+
st.header('Entity-Relation Representation')
|
105 |
+
# st.graphviz_chart(graph)
|
106 |
+
st.graphviz_chart(graph, use_container_width=True)
|
107 |
+
|
108 |
+
st.session_state['graph'] = graph
|
109 |
+
|
110 |
+
st.divider()
|
111 |
+
st.write("Does this prediction look correct?")
|
112 |
+
collector.st_feedback(
|
113 |
+
component="default",
|
114 |
+
feedback_type="thumbs",
|
115 |
+
model="accord-nlp-ie",
|
116 |
+
align="flex-start",
|
117 |
+
metadata={
|
118 |
+
"sentence": txt
|
119 |
+
},
|
120 |
+
open_feedback_label="[Optional] Provide additional feedback",
|
121 |
+
single_submit=False
|
122 |
+
)
|
123 |
|
124 |
|
125 |
if __name__ == '__main__':
|
requirements.txt
CHANGED
@@ -2,4 +2,5 @@
|
|
2 |
--find-links https://download.pytorch.org/whl/torch_stable.html
|
3 |
torch==2.0.1+cpu
|
4 |
streamlit==1.26.0
|
5 |
-
accord-nlp==0.1.8
|
|
|
|
2 |
--find-links https://download.pytorch.org/whl/torch_stable.html
|
3 |
torch==2.0.1+cpu
|
4 |
streamlit==1.26.0
|
5 |
+
accord-nlp==0.1.8
|
6 |
+
trubrics[streamlit]==1.5.1
|