TheBeast commited on
Commit
48be4c4
1 Parent(s): 63067f8

Adding a version 1 of demo for testing

Browse files
Files changed (2) hide show
  1. app.py +126 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Demo for NER4OPT."""
2
+ import re
3
+ import warnings
4
+
5
+ warnings.filterwarnings("ignore", category=DeprecationWarning)
6
+
7
+ import spacy
8
+ from spacy import displacy
9
+ from spacy.training import iob_to_biluo, biluo_tags_to_offsets
10
+ from spacy.tokenizer import Tokenizer
11
+
12
+ import streamlit as st
13
+
14
+ from simpletransformers.ner import NERModel, NERArgs
15
+
16
+ HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
17
+
18
+
19
+ @st.cache_resource
20
+ def load_models():
21
+ """Load custom built NER4OPT model."""
22
+ custom_labels = [
23
+ 'O',
24
+ 'B-CONST_DIR',
25
+ 'I-CONST_DIR',
26
+ 'B-LIMIT',
27
+ 'I-LIMIT',
28
+ 'B-VAR',
29
+ 'I-VAR',
30
+ 'B-OBJ_DIR',
31
+ 'B-OBJ_NAME',
32
+ 'I-OBJ_NAME',
33
+ 'B-PARAM',
34
+ 'I-PARAM',
35
+ ]
36
+ # # create model
37
+ model_args = NERArgs()
38
+ model_args.use_early_stopping = True
39
+ model_args.early_stopping_delta = 0.01
40
+ model_args.early_stopping_metric = "eval_loss"
41
+ model_args.early_stopping_metric_minimize = False
42
+ model_args.early_stopping_patience = 5
43
+ model_args.evaluate_during_training_steps = 2000
44
+ model_args.overwrite_output_dir = True
45
+ model_args.reprocess_input_data = True
46
+ model_args.num_train_epochs = 11
47
+ model_args.adafactor_beta1 = 0.9
48
+ model_args.weight_decay = 0.01
49
+ model_args.max_seq_length = 512
50
+ model_args.learning_rate = 4e-5
51
+ model_args.train_batch_size = 1
52
+ model_args.eval_batch_size = 1
53
+ model_args.manual_seed = 123456789
54
+ model_args.output_dir = "trained_transformer_model"
55
+ model_args.use_cuda = True
56
+ model_args.use_multiprocessing = False
57
+ model = NERModel("roberta",
58
+ "skadio/ner4opt-roberta",
59
+ labels=custom_labels,
60
+ use_cuda=True,
61
+ args=model_args)
62
+ spacy_model = spacy.load("en_core_web_sm")
63
+ spacy_model.tokenizer = Tokenizer(spacy_model.vocab,
64
+ token_match=re.compile(r'\S+').match)
65
+ spacy_blank_model = spacy.blank('en')
66
+ spacy_blank_model.tokenizer = Tokenizer(
67
+ spacy_blank_model.vocab, token_match=re.compile(r'\S+').match)
68
+ return model, spacy_model, spacy_blank_model
69
+
70
+
71
+ def main():
72
+
73
+ st.sidebar.title("""
74
+ NER4OPT Demo: \nFull code will be available at https://github.com/skadio/Ner4Opt
75
+ """)
76
+
77
+ text = st.text_area(
78
+ "Text",
79
+ "Cautious Asset Investment has a total of $ 150,000 to manage and decides to invest it in money market fund , which yields a 2 % return as well as in foreign bonds , which gives and average rate of return of 10.2 % . Internal policies require PAI to diversify the asset allocation so that the minimum investment in money market fund is 40 % of the total investment . Due to the risk of default of foreign countries , no more than 40 % of the total investment should be allocated to foreign bonds . How much should the Cautious Asset Investment allocate in each asset so as to maximize its average return ?"
80
+ )
81
+ if text == "":
82
+ st.write("Please write a valid sentence.")
83
+ model, spacy_model, spacy_blank_model = load_models()
84
+
85
+ # Augmented Text
86
+ spacy_doc = spacy_model(text)
87
+ if len(list(spacy_doc.sents)) >= 2:
88
+ last_two_sentences = ' '.join(
89
+ [item.text for item in list(spacy_doc.sents)[-2::]])
90
+ else:
91
+ last_two_sentences = ' '.join(
92
+ [item.text for item in list(spacy_doc.sents)[-1::]])
93
+ to_skip_count = len(last_two_sentences.split())
94
+ augmented_sent = last_two_sentences + " " + text
95
+
96
+ if st.button("Get Named Entities"):
97
+ predictions, raw_outputs = model.predict([augmented_sent],
98
+ split_on_space=True)
99
+ transformer_predictions = [
100
+ list(val.values())[0] for val in predictions[0]
101
+ ]
102
+ transformer_predictions = transformer_predictions[to_skip_count::]
103
+ biluo_tags = iob_to_biluo(transformer_predictions)
104
+ doc = spacy_blank_model.make_doc(text)
105
+ entities = biluo_tags_to_offsets(doc, biluo_tags)
106
+ entities_formatted = []
107
+ for tag in entities:
108
+ entities_formatted.append({
109
+ "start": tag[0],
110
+ "end": tag[1],
111
+ "label": tag[2],
112
+ "score": 1.0
113
+ })
114
+ ner_for_display = [{
115
+ "text": doc.text,
116
+ "ents": entities_formatted,
117
+ "title": None
118
+ }]
119
+ st.title("Named Entity Results")
120
+ html_ner = displacy.render(ner_for_display, style="ent", manual=True)
121
+ html_ner = html_ner.replace("\n", " ")
122
+ st.write(HTML_WRAPPER.format(html_ner), unsafe_allow_html=True)
123
+
124
+
125
+ if __name__ == '__main__':
126
+ main()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ simpletransformers==0.63.7
3
+ spacy==3.2.0