File size: 1,957 Bytes
dea7dd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Created by Hansi at 30/08/2023
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')

import streamlit as st
from accord_nlp.information_extraction.convertor import entity_pairing, graph_building
from accord_nlp.information_extraction.ie_pipeline import InformationExtractor


@st.cache_resource
def init():
    return InformationExtractor()


st.set_page_config(
    page_title='ACCORD NLP Demo',
    initial_sidebar_state='expanded',
    layout='wide',
)

with st.spinner(text="Initialising..."):
    ie = init()


def main():
    st.sidebar.title("ACCORD-NLP")
    st.sidebar.markdown("Extract information from text")
    st.sidebar.markdown(
        "[code](https://github.com/Accord-Project/NLP-Framework)"
    )

    st.header("Input a sentence")

    txt = st.text_area('Sentence')

    # st.write(txt)

    # with st.spinner(text="Processing..."):
    # graph = ie.sentence_to_graph(txt)

    if txt:
        # preprocess
        sentence = ie.preprocess(txt)
        st.write(sentence)

        # NER
        with st.spinner(text="Recognising entities..."):
            ner_predictions, ner_raw_outputs = ie.ner_model.predict([sentence])

        st.write(ner_predictions)

        with st.spinner(text="Extracting relations..."):
            # pair entities to predict their relations
            entity_pair_df = entity_pairing(sentence, ner_predictions[0])
            st.write('entity paired')

            # relation extraction
            re_predictions, re_raw_outputs = ie.re_model.predict(entity_pair_df['output'].tolist())
            entity_pair_df['prediction'] = re_predictions
            st.write(re_predictions)

        with st.spinner(text="Building graph..."):
            # build graph
            graph = graph_building(entity_pair_df, view=False)
            # st.success()

        st.header('Entity-Relation Representation')
        st.graphviz_chart(graph)


if __name__ == '__main__':
    main()