HHansi commited on
Commit
dea7dd8
1 Parent(s): e039d7e

initial commit

Browse files
Files changed (2) hide show
  1. app.py +73 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by Hansi at 30/08/2023
2
+ import nltk
3
+ nltk.download('punkt')
4
+ nltk.download('averaged_perceptron_tagger')
5
+
6
+ import streamlit as st
7
+ from accord_nlp.information_extraction.convertor import entity_pairing, graph_building
8
+ from accord_nlp.information_extraction.ie_pipeline import InformationExtractor
9
+
10
+
11
+ @st.cache_resource
12
+ def init():
13
+ return InformationExtractor()
14
+
15
+
16
+ st.set_page_config(
17
+ page_title='ACCORD NLP Demo',
18
+ initial_sidebar_state='expanded',
19
+ layout='wide',
20
+ )
21
+
22
+ with st.spinner(text="Initialising..."):
23
+ ie = init()
24
+
25
+
26
+ def main():
27
+ st.sidebar.title("ACCORD-NLP")
28
+ st.sidebar.markdown("Extract information from text")
29
+ st.sidebar.markdown(
30
+ "[code](https://github.com/Accord-Project/NLP-Framework)"
31
+ )
32
+
33
+ st.header("Input a sentence")
34
+
35
+ txt = st.text_area('Sentence')
36
+
37
+ # st.write(txt)
38
+
39
+ # with st.spinner(text="Processing..."):
40
+ # graph = ie.sentence_to_graph(txt)
41
+
42
+ if txt:
43
+ # preprocess
44
+ sentence = ie.preprocess(txt)
45
+ st.write(sentence)
46
+
47
+ # NER
48
+ with st.spinner(text="Recognising entities..."):
49
+ ner_predictions, ner_raw_outputs = ie.ner_model.predict([sentence])
50
+
51
+ st.write(ner_predictions)
52
+
53
+ with st.spinner(text="Extracting relations..."):
54
+ # pair entities to predict their relations
55
+ entity_pair_df = entity_pairing(sentence, ner_predictions[0])
56
+ st.write('entity paired')
57
+
58
+ # relation extraction
59
+ re_predictions, re_raw_outputs = ie.re_model.predict(entity_pair_df['output'].tolist())
60
+ entity_pair_df['prediction'] = re_predictions
61
+ st.write(re_predictions)
62
+
63
+ with st.spinner(text="Building graph..."):
64
+ # build graph
65
+ graph = graph_building(entity_pair_df, view=False)
66
+ # st.success()
67
+
68
+ st.header('Entity-Relation Representation')
69
+ st.graphviz_chart(graph)
70
+
71
+
72
+ if __name__ == '__main__':
73
+ main()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # PyTorch - https://pytorch.org/get-started/locally/
2
+ torch==1.7.0+cpu
3
+ streamlit==1.26.0
4
+ accord-nlp==0.1.8