Spaces:
Runtime error
Runtime error
ashishraics
commited on
Commit
•
8bb7965
1
Parent(s):
356e503
optimize app
Browse files- .gitignore +3 -3
- app.py +33 -40
- sentiment_model_dir/config.json +34 -0
- sentiment_model_dir/special_tokens_map.json +1 -0
- sentiment_model_dir/tokenizer.json +0 -0
- sentiment_model_dir/tokenizer_config.json +1 -0
- sentiment_model_dir/vocab.txt +0 -0
- zs_model_dir/config.json +58 -0
- zs_model_dir/merges.txt +0 -0
- zs_model_dir/special_tokens_map.json +1 -0
- zs_model_dir/tokenizer.json +0 -0
- zs_model_dir/tokenizer_config.json +1 -0
- zs_model_dir/vocab.json +0 -0
.gitignore
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
venv/
|
2 |
-
|
3 |
-
|
4 |
-
zs_model_dir/
|
5 |
#sent_clf_onnx_dir/
|
6 |
#zs_onnx_dir/
|
|
|
1 |
venv/
|
2 |
+
#exclude model files as they are large
|
3 |
+
sentiment_model_dir/pytorch_model.bin
|
4 |
+
zs_model_dir/pytorch_model.bin
|
5 |
#sent_clf_onnx_dir/
|
6 |
#zs_onnx_dir/
|
app.py
CHANGED
@@ -103,10 +103,12 @@ def create_model_dir(chkpt, model_dir):
|
|
103 |
pass
|
104 |
|
105 |
|
106 |
-
|
107 |
-
|
|
|
108 |
with st.sidebar:
|
109 |
-
|
|
|
110 |
select_task=st.selectbox(label="Select task from drop down menu",
|
111 |
options=['README',
|
112 |
'Detect Sentiment','Zero Shot Classification'])
|
@@ -114,7 +116,7 @@ with st.sidebar:
|
|
114 |
############### Pre-Download & instantiate objects for sentiment analysis *********************** START **********************
|
115 |
|
116 |
# #create model/token dir for sentiment classification for faster inference
|
117 |
-
|
118 |
|
119 |
|
120 |
@st.cache(allow_output_mutation=True, suppress_st_warning=True, max_entries=None, ttl=None)
|
@@ -125,26 +127,26 @@ def sentiment_task_selected(task,
|
|
125 |
sent_onnx_mdl_name=sent_onnx_mdl_name,
|
126 |
sent_onnx_quant_mdl_name=sent_onnx_quant_mdl_name):
|
127 |
#model & tokenizer initialization for normal sentiment classification
|
128 |
-
model_sentiment=AutoModelForSequenceClassification.from_pretrained(sent_chkpt)
|
129 |
-
tokenizer_sentiment=AutoTokenizer.from_pretrained(sent_chkpt)
|
|
|
130 |
|
131 |
-
# create onnx model for sentiment classification
|
132 |
-
create_onnx_model_sentiment(_model=model_sentiment, _tokenizer=tokenizer_sentiment)
|
133 |
|
134 |
#create inference session
|
135 |
sentiment_session = ort.InferenceSession(f"{sent_onnx_mdl_dir}/{sent_onnx_mdl_name}")
|
136 |
# sentiment_session_quant = ort.InferenceSession(f"{sent_onnx_mdl_dir}/{sent_onnx_quant_mdl_name}")
|
137 |
|
138 |
-
return
|
139 |
|
140 |
############## Pre-Download & instantiate objects for sentiment analysis ********************* END **********************************
|
141 |
|
142 |
|
143 |
############### Pre-Download & instantiate objects for Zero shot clf *********************** START **********************
|
144 |
|
145 |
-
#
|
146 |
-
|
147 |
-
|
148 |
|
149 |
@st.cache(allow_output_mutation=True, suppress_st_warning=True, max_entries=None, ttl=None)
|
150 |
def zs_task_selected(task,
|
@@ -157,10 +159,11 @@ def zs_task_selected(task,
|
|
157 |
##model & tokenizer initialization for normal ZS classification
|
158 |
# model_zs=AutoModelForSequenceClassification.from_pretrained(zs_chkpt)
|
159 |
# we just need tokenizer for inference and not model since onnx model is already saved
|
160 |
-
tokenizer_zs=AutoTokenizer.from_pretrained(zs_chkpt)
|
|
|
161 |
|
162 |
-
# create onnx model for zeroshot
|
163 |
-
create_onnx_model_zs()
|
164 |
|
165 |
#create inference session from onnx model
|
166 |
zs_session = ort.InferenceSession(f"{zs_onnx_mdl_dir}/{zs_onnx_mdl_name}")
|
@@ -172,11 +175,11 @@ def zs_task_selected(task,
|
|
172 |
|
173 |
if select_task=='README':
|
174 |
st.header("NLP Summary")
|
|
|
175 |
|
176 |
if select_task == 'Detect Sentiment':
|
177 |
t1=time.time()
|
178 |
-
|
179 |
-
sentiment_session = sentiment_task_selected(task=select_task)
|
180 |
t2 = time.time()
|
181 |
st.write(f"Total time to load Model is {(t2-t1)*1000:.1f} ms")
|
182 |
|
@@ -185,28 +188,16 @@ if select_task == 'Detect Sentiment':
|
|
185 |
c1,c2,_,_=st.columns(4)
|
186 |
|
187 |
with c1:
|
188 |
-
response1=st.button("
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
)
|
199 |
-
end=time.time()
|
200 |
-
st.write(f"Time taken for computation {(end-start)*1000:.1f} ms")
|
201 |
-
elif response2:
|
202 |
-
start = time.time()
|
203 |
-
sentiments=classify_sentiment_onnx(input_texts,
|
204 |
-
_session=sentiment_session,
|
205 |
-
_tokenizer=tokenizer_sentiment)
|
206 |
-
end = time.time()
|
207 |
-
st.write(f"Time taken for computation {(end - start) * 1000:.1f} ms")
|
208 |
-
else:
|
209 |
-
pass
|
210 |
for i,t in enumerate(input_texts.split(',')):
|
211 |
if sentiments[i]=='Positive':
|
212 |
response=st_text_rater(t + f"--> This statement is {sentiments[i]}",
|
@@ -214,6 +205,8 @@ if select_task == 'Detect Sentiment':
|
|
214 |
else:
|
215 |
response = st_text_rater(t + f"--> This statement is {sentiments[i]}",
|
216 |
color_background='rgb(233, 116, 81)',key=t)
|
|
|
|
|
217 |
|
218 |
if select_task=='Zero Shot Classification':
|
219 |
t1=time.time()
|
@@ -228,7 +221,7 @@ if select_task=='Zero Shot Classification':
|
|
228 |
c1,_,_,_=st.columns(4)
|
229 |
|
230 |
with c1:
|
231 |
-
response1=st.button("Compute
|
232 |
|
233 |
if response1:
|
234 |
start = time.time()
|
|
|
103 |
pass
|
104 |
|
105 |
|
106 |
+
#title using markdown
|
107 |
+
st.markdown("<h1 style='text-align: center; color: #3366ff;'>NLP Basic Use Cases</h1>", unsafe_allow_html=True)
|
108 |
+
st.markdown("---")
|
109 |
with st.sidebar:
|
110 |
+
# title using markdown
|
111 |
+
st.markdown("<h1 style='text-align: left; color: ;'>NLP Tasks</h1>", unsafe_allow_html=True)
|
112 |
select_task=st.selectbox(label="Select task from drop down menu",
|
113 |
options=['README',
|
114 |
'Detect Sentiment','Zero Shot Classification'])
|
|
|
116 |
############### Pre-Download & instantiate objects for sentiment analysis *********************** START **********************
|
117 |
|
118 |
# #create model/token dir for sentiment classification for faster inference
|
119 |
+
create_model_dir(chkpt=sent_chkpt, model_dir=sent_mdl_dir)
|
120 |
|
121 |
|
122 |
@st.cache(allow_output_mutation=True, suppress_st_warning=True, max_entries=None, ttl=None)
|
|
|
127 |
sent_onnx_mdl_name=sent_onnx_mdl_name,
|
128 |
sent_onnx_quant_mdl_name=sent_onnx_quant_mdl_name):
|
129 |
#model & tokenizer initialization for normal sentiment classification
|
130 |
+
# model_sentiment=AutoModelForSequenceClassification.from_pretrained(sent_chkpt)
|
131 |
+
# tokenizer_sentiment=AutoTokenizer.from_pretrained(sent_chkpt)
|
132 |
+
tokenizer_sentiment = AutoTokenizer.from_pretrained(sent_mdl_dir)
|
133 |
|
134 |
+
# # create onnx model for sentiment classification but once created in your local app comment this out
|
135 |
+
# create_onnx_model_sentiment(_model=model_sentiment, _tokenizer=tokenizer_sentiment)
|
136 |
|
137 |
#create inference session
|
138 |
sentiment_session = ort.InferenceSession(f"{sent_onnx_mdl_dir}/{sent_onnx_mdl_name}")
|
139 |
# sentiment_session_quant = ort.InferenceSession(f"{sent_onnx_mdl_dir}/{sent_onnx_quant_mdl_name}")
|
140 |
|
141 |
+
return tokenizer_sentiment,sentiment_session
|
142 |
|
143 |
############## Pre-Download & instantiate objects for sentiment analysis ********************* END **********************************
|
144 |
|
145 |
|
146 |
############### Pre-Download & instantiate objects for Zero shot clf *********************** START **********************
|
147 |
|
148 |
+
# create model/token dir for zeroshot clf -- already created so not required
|
149 |
+
create_model_dir(chkpt=zs_chkpt, model_dir=zs_mdl_dir)
|
|
|
150 |
|
151 |
@st.cache(allow_output_mutation=True, suppress_st_warning=True, max_entries=None, ttl=None)
|
152 |
def zs_task_selected(task,
|
|
|
159 |
##model & tokenizer initialization for normal ZS classification
|
160 |
# model_zs=AutoModelForSequenceClassification.from_pretrained(zs_chkpt)
|
161 |
# we just need tokenizer for inference and not model since onnx model is already saved
|
162 |
+
# tokenizer_zs=AutoTokenizer.from_pretrained(zs_chkpt)
|
163 |
+
tokenizer_zs = AutoTokenizer.from_pretrained(zs_mdl_dir)
|
164 |
|
165 |
+
# # create onnx model for zeroshot but once created locally comment it out.
|
166 |
+
# create_onnx_model_zs()
|
167 |
|
168 |
#create inference session from onnx model
|
169 |
zs_session = ort.InferenceSession(f"{zs_onnx_mdl_dir}/{zs_onnx_mdl_name}")
|
|
|
175 |
|
176 |
if select_task=='README':
|
177 |
st.header("NLP Summary")
|
178 |
+
# st.write()
|
179 |
|
180 |
if select_task == 'Detect Sentiment':
|
181 |
t1=time.time()
|
182 |
+
tokenizer_sentiment,sentiment_session = sentiment_task_selected(task=select_task)
|
|
|
183 |
t2 = time.time()
|
184 |
st.write(f"Total time to load Model is {(t2-t1)*1000:.1f} ms")
|
185 |
|
|
|
188 |
c1,c2,_,_=st.columns(4)
|
189 |
|
190 |
with c1:
|
191 |
+
response1=st.button("Compute (ONNX runtime)")
|
192 |
+
|
193 |
+
if response1:
|
194 |
+
start = time.time()
|
195 |
+
sentiments=classify_sentiment_onnx(input_texts,
|
196 |
+
_session=sentiment_session,
|
197 |
+
_tokenizer=tokenizer_sentiment)
|
198 |
+
end = time.time()
|
199 |
+
st.write(f"Time taken for computation {(end - start) * 1000:.1f} ms")
|
200 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
for i,t in enumerate(input_texts.split(',')):
|
202 |
if sentiments[i]=='Positive':
|
203 |
response=st_text_rater(t + f"--> This statement is {sentiments[i]}",
|
|
|
205 |
else:
|
206 |
response = st_text_rater(t + f"--> This statement is {sentiments[i]}",
|
207 |
color_background='rgb(233, 116, 81)',key=t)
|
208 |
+
else:
|
209 |
+
pass
|
210 |
|
211 |
if select_task=='Zero Shot Classification':
|
212 |
t1=time.time()
|
|
|
221 |
c1,_,_,_=st.columns(4)
|
222 |
|
223 |
with c1:
|
224 |
+
response1=st.button("Compute (ONNX runtime)")
|
225 |
|
226 |
if response1:
|
227 |
start = time.time()
|
sentiment_model_dir/config.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "distilbert-base-uncased-finetuned-sst-2-english",
|
3 |
+
"activation": "gelu",
|
4 |
+
"architectures": [
|
5 |
+
"DistilBertForSequenceClassification"
|
6 |
+
],
|
7 |
+
"attention_dropout": 0.1,
|
8 |
+
"dim": 768,
|
9 |
+
"dropout": 0.1,
|
10 |
+
"finetuning_task": "sst-2",
|
11 |
+
"hidden_dim": 3072,
|
12 |
+
"id2label": {
|
13 |
+
"0": "NEGATIVE",
|
14 |
+
"1": "POSITIVE"
|
15 |
+
},
|
16 |
+
"initializer_range": 0.02,
|
17 |
+
"label2id": {
|
18 |
+
"NEGATIVE": 0,
|
19 |
+
"POSITIVE": 1
|
20 |
+
},
|
21 |
+
"max_position_embeddings": 512,
|
22 |
+
"model_type": "distilbert",
|
23 |
+
"n_heads": 12,
|
24 |
+
"n_layers": 6,
|
25 |
+
"output_past": true,
|
26 |
+
"pad_token_id": 0,
|
27 |
+
"qa_dropout": 0.1,
|
28 |
+
"seq_classif_dropout": 0.2,
|
29 |
+
"sinusoidal_pos_embds": false,
|
30 |
+
"tie_weights_": true,
|
31 |
+
"torch_dtype": "float32",
|
32 |
+
"transformers_version": "4.18.0",
|
33 |
+
"vocab_size": 30522
|
34 |
+
}
|
sentiment_model_dir/special_tokens_map.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
|
sentiment_model_dir/tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
sentiment_model_dir/tokenizer_config.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"do_lower_case": true, "unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "tokenize_chinese_chars": true, "strip_accents": null, "model_max_length": 512, "special_tokens_map_file": null, "name_or_path": "distilbert-base-uncased-finetuned-sst-2-english", "do_basic_tokenize": true, "never_split": null, "tokenizer_class": "DistilBertTokenizer"}
|
sentiment_model_dir/vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
zs_model_dir/config.json
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "valhalla/distilbart-mnli-12-1",
|
3 |
+
"_num_labels": 3,
|
4 |
+
"activation_dropout": 0.0,
|
5 |
+
"activation_function": "gelu",
|
6 |
+
"add_bias_logits": false,
|
7 |
+
"add_final_layer_norm": false,
|
8 |
+
"architectures": [
|
9 |
+
"BartForSequenceClassification"
|
10 |
+
],
|
11 |
+
"attention_dropout": 0.1,
|
12 |
+
"bos_token_id": 0,
|
13 |
+
"classif_dropout": 0.0,
|
14 |
+
"classifier_dropout": 0.0,
|
15 |
+
"d_model": 1024,
|
16 |
+
"decoder_attention_heads": 16,
|
17 |
+
"decoder_ffn_dim": 4096,
|
18 |
+
"decoder_layerdrop": 0.0,
|
19 |
+
"decoder_layers": 1,
|
20 |
+
"decoder_start_token_id": 2,
|
21 |
+
"dropout": 0.1,
|
22 |
+
"encoder_attention_heads": 16,
|
23 |
+
"encoder_ffn_dim": 4096,
|
24 |
+
"encoder_layerdrop": 0.0,
|
25 |
+
"encoder_layers": 12,
|
26 |
+
"eos_token_id": 2,
|
27 |
+
"extra_pos_embeddings": 2,
|
28 |
+
"finetuning_task": "mnli",
|
29 |
+
"force_bos_token_to_be_generated": false,
|
30 |
+
"forced_eos_token_id": 2,
|
31 |
+
"gradient_checkpointing": false,
|
32 |
+
"id2label": {
|
33 |
+
"0": "contradiction",
|
34 |
+
"1": "neutral",
|
35 |
+
"2": "entailment"
|
36 |
+
},
|
37 |
+
"init_std": 0.02,
|
38 |
+
"is_encoder_decoder": true,
|
39 |
+
"label2id": {
|
40 |
+
"contradiction": 0,
|
41 |
+
"entailment": 2,
|
42 |
+
"neutral": 1
|
43 |
+
},
|
44 |
+
"max_position_embeddings": 1024,
|
45 |
+
"model_type": "bart",
|
46 |
+
"normalize_before": false,
|
47 |
+
"normalize_embedding": true,
|
48 |
+
"num_hidden_layers": 12,
|
49 |
+
"output_past": false,
|
50 |
+
"pad_token_id": 1,
|
51 |
+
"scale_embedding": false,
|
52 |
+
"static_position_embeddings": false,
|
53 |
+
"torch_dtype": "float32",
|
54 |
+
"total_flos": 153130534133111808,
|
55 |
+
"transformers_version": "4.18.0",
|
56 |
+
"use_cache": true,
|
57 |
+
"vocab_size": 50265
|
58 |
+
}
|
zs_model_dir/merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
zs_model_dir/special_tokens_map.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"bos_token": {"content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "eos_token": {"content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "unk_token": {"content": "<unk>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "sep_token": {"content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "pad_token": {"content": "<pad>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "cls_token": {"content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": true}}
|
zs_model_dir/tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
zs_model_dir/tokenizer_config.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"errors": "replace", "bos_token": {"content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "eos_token": {"content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "sep_token": {"content": "</s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "cls_token": {"content": "<s>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "unk_token": {"content": "<unk>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "pad_token": {"content": "<pad>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "add_prefix_space": false, "trim_offsets": true, "model_max_length": 1024, "special_tokens_map_file": "/Users/ashishrai/.cache/huggingface/transformers/1897a33c6ca1e896797e7f370753103e4fb6980c6371197c5658ff3a8269dc4a.cb2244924ab24d706b02fd7fcedaea4531566537687a539ebb94db511fd122a0", "name_or_path": "valhalla/distilbart-mnli-12-1", "tokenizer_class": "BartTokenizer"}
|
zs_model_dir/vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|