Spaces:
Runtime error
Runtime error
Commit
·
9811800
1
Parent(s):
a57c1e5
optimize app
Browse files- .gitignore +1 -1
- app.py +27 -113
- sentiment_clf_helper.py +3 -3
- zeroshot_clf_helper.py +4 -6
.gitignore
CHANGED
@@ -6,4 +6,4 @@ zeroshot_onnx_dir/
|
|
6 |
sent_clf_onnx_dir/
|
7 |
zs_onnx_dir/
|
8 |
sent_onnx_mdl_dir/
|
9 |
-
sent_mdl_dir/
|
|
|
6 |
sent_clf_onnx_dir/
|
7 |
zs_onnx_dir/
|
8 |
sent_onnx_mdl_dir/
|
9 |
+
sent_mdl_dir/
|
app.py
CHANGED
@@ -136,9 +136,9 @@ def sentiment_task_selected(task,
|
|
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 model_sentiment,tokenizer_sentiment,sentiment_session
|
142 |
|
143 |
############## Pre-Download & instantiate objects for sentiment analysis ********************* END **********************************
|
144 |
|
@@ -167,35 +167,30 @@ def zs_task_selected(task,
|
|
167 |
|
168 |
#create inference session from onnx model
|
169 |
zs_session = ort.InferenceSession(f"{zs_onnx_mdl_dir}/{zs_onnx_mdl_name}")
|
170 |
-
zs_session_quant = ort.InferenceSession(f"{zs_onnx_mdl_dir}/{zs_onnx_quant_mdl_name}")
|
171 |
|
172 |
-
return tokenizer_zs,zs_session
|
173 |
|
174 |
############## Pre-Download & instantiate objects for Zero shot analysis ********************* END **********************************
|
175 |
|
176 |
|
177 |
if select_task == 'Detect Sentiment':
|
178 |
-
|
179 |
t1=time.time()
|
180 |
model_sentiment,tokenizer_sentiment,\
|
181 |
-
sentiment_session
|
182 |
t2 = time.time()
|
183 |
st.write(f"Total time to load Model is {(t2-t1)*1000:.1f} ms")
|
184 |
|
185 |
st.header("You are now performing Sentiment Analysis")
|
186 |
input_texts = st.text_input(label="Input texts separated by comma")
|
187 |
-
c1,c2,
|
188 |
|
189 |
with c1:
|
190 |
response1=st.button("Normal runtime")
|
191 |
with c2:
|
192 |
response2=st.button("ONNX runtime")
|
193 |
-
with c3:
|
194 |
-
response3=st.button("ONNX runtime with Quantization")
|
195 |
-
with c4:
|
196 |
-
response4 = st.button("Simulate 100 runs each runtime")
|
197 |
|
198 |
-
if any([response1,response2
|
199 |
if response1:
|
200 |
start=time.time()
|
201 |
sentiments = classify_sentiment(input_texts,
|
@@ -211,65 +206,6 @@ if select_task == 'Detect Sentiment':
|
|
211 |
_tokenizer=tokenizer_sentiment)
|
212 |
end = time.time()
|
213 |
st.write(f"Time taken for computation {(end - start) * 1000:.1f} ms")
|
214 |
-
elif response3:
|
215 |
-
start = time.time()
|
216 |
-
sentiments=classify_sentiment_onnx(input_texts,
|
217 |
-
_session=sentiment_session_quant,
|
218 |
-
_tokenizer=tokenizer_sentiment)
|
219 |
-
end = time.time()
|
220 |
-
st.write(f"Time taken for computation {(end - start) * 1000:.1f} ms")
|
221 |
-
elif response4:
|
222 |
-
normal_runtime=[]
|
223 |
-
for i in range(100):
|
224 |
-
start=time.time()
|
225 |
-
sentiments = classify_sentiment(input_texts,
|
226 |
-
model=model_sentiment,
|
227 |
-
tokenizer=tokenizer_sentiment)
|
228 |
-
end=time.time()
|
229 |
-
t = (end - start) * 1000
|
230 |
-
normal_runtime.append(t)
|
231 |
-
normal_runtime=np.clip(normal_runtime,10,60)
|
232 |
-
|
233 |
-
onnx_runtime=[]
|
234 |
-
for i in range(100):
|
235 |
-
start=time.time()
|
236 |
-
sentiments = classify_sentiment_onnx(input_texts,
|
237 |
-
_session=sentiment_session,
|
238 |
-
_tokenizer=tokenizer_sentiment)
|
239 |
-
end=time.time()
|
240 |
-
t=(end-start)*1000
|
241 |
-
onnx_runtime.append(t)
|
242 |
-
onnx_runtime = np.clip(onnx_runtime, 0, 20)
|
243 |
-
|
244 |
-
onnx_runtime_quant=[]
|
245 |
-
for i in range(100):
|
246 |
-
start=time.time()
|
247 |
-
sentiments = classify_sentiment_onnx(input_texts,
|
248 |
-
_session=sentiment_session_quant,
|
249 |
-
_tokenizer=tokenizer_sentiment)
|
250 |
-
end=time.time()
|
251 |
-
|
252 |
-
t=(end-start)*1000
|
253 |
-
onnx_runtime_quant.append(t)
|
254 |
-
onnx_runtime_quant = np.clip(onnx_runtime_quant, 0, 20)
|
255 |
-
|
256 |
-
|
257 |
-
temp_df=pd.DataFrame({'Normal Runtime (ms)':normal_runtime,
|
258 |
-
'ONNX Runtime (ms)':onnx_runtime,
|
259 |
-
'ONNX Quant Runtime (ms)':onnx_runtime_quant})
|
260 |
-
|
261 |
-
from plotly.subplots import make_subplots
|
262 |
-
fig = make_subplots(rows=1, cols=3, start_cell="bottom-left",
|
263 |
-
subplot_titles=['Normal Runtime','ONNX Runtime','ONNX Runtime with Quantization'])
|
264 |
-
|
265 |
-
fig.add_trace(go.Histogram(x=temp_df['Normal Runtime (ms)']),row=1,col=1)
|
266 |
-
fig.add_trace(go.Histogram(x=temp_df['ONNX Runtime (ms)']),row=1,col=2)
|
267 |
-
fig.add_trace(go.Histogram(x=temp_df['ONNX Quant Runtime (ms)']),row=1,col=3)
|
268 |
-
fig.update_layout(height=400, width=1000,
|
269 |
-
title_text="100 Simulations of different Runtimes",
|
270 |
-
showlegend=False)
|
271 |
-
st.plotly_chart(fig,config=_plotly_config )
|
272 |
-
|
273 |
else:
|
274 |
pass
|
275 |
for i,t in enumerate(input_texts.split(',')):
|
@@ -281,9 +217,8 @@ if select_task == 'Detect Sentiment':
|
|
281 |
color_background='rgb(233, 116, 81)',key=t)
|
282 |
|
283 |
if select_task=='Zero Shot Classification':
|
284 |
-
|
285 |
t1=time.time()
|
286 |
-
tokenizer_zs,zs_session
|
287 |
t2 = time.time()
|
288 |
st.write(f"Total time to load Model is {(t2-t1)*1000:.1f} ms")
|
289 |
|
@@ -291,46 +226,25 @@ if select_task=='Zero Shot Classification':
|
|
291 |
input_texts = st.text_input(label="Input text to classify into topics")
|
292 |
input_lables = st.text_input(label="Enter labels separated by commas")
|
293 |
|
294 |
-
c1,
|
295 |
|
296 |
with c1:
|
297 |
-
response1=st.button("ONNX runtime")
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
text='Probability',
|
316 |
-
data_frame=df_output,
|
317 |
-
title='Zero Shot Normalized Probabilities')
|
318 |
-
|
319 |
-
st.plotly_chart(fig,config=_plotly_config)
|
320 |
-
elif response2:
|
321 |
-
start = time.time()
|
322 |
-
df_output = zero_shot_classification_onnx(premise=input_texts, labels=input_lables, _session=zs_session_quant,
|
323 |
-
_tokenizer=tokenizer_zs)
|
324 |
-
end = time.time()
|
325 |
-
st.write("")
|
326 |
-
st.write(f"Time taken for computation {(end-start)*1000:.1f} ms")
|
327 |
-
fig = px.bar(x='Probability',
|
328 |
-
y='labels',
|
329 |
-
text='Probability',
|
330 |
-
data_frame=df_output,
|
331 |
-
title='Zero Shot Normalized Probabilities')
|
332 |
-
|
333 |
-
st.plotly_chart(fig, config=_plotly_config)
|
334 |
-
else:
|
335 |
-
pass
|
336 |
|
|
|
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 model_sentiment,tokenizer_sentiment,sentiment_session
|
142 |
|
143 |
############## Pre-Download & instantiate objects for sentiment analysis ********************* END **********************************
|
144 |
|
|
|
167 |
|
168 |
#create inference session from onnx model
|
169 |
zs_session = ort.InferenceSession(f"{zs_onnx_mdl_dir}/{zs_onnx_mdl_name}")
|
170 |
+
# zs_session_quant = ort.InferenceSession(f"{zs_onnx_mdl_dir}/{zs_onnx_quant_mdl_name}")
|
171 |
|
172 |
+
return tokenizer_zs,zs_session
|
173 |
|
174 |
############## Pre-Download & instantiate objects for Zero shot analysis ********************* END **********************************
|
175 |
|
176 |
|
177 |
if select_task == 'Detect Sentiment':
|
|
|
178 |
t1=time.time()
|
179 |
model_sentiment,tokenizer_sentiment,\
|
180 |
+
sentiment_session = sentiment_task_selected(task=select_task)
|
181 |
t2 = time.time()
|
182 |
st.write(f"Total time to load Model is {(t2-t1)*1000:.1f} ms")
|
183 |
|
184 |
st.header("You are now performing Sentiment Analysis")
|
185 |
input_texts = st.text_input(label="Input texts separated by comma")
|
186 |
+
c1,c2,_,_=st.columns(4)
|
187 |
|
188 |
with c1:
|
189 |
response1=st.button("Normal runtime")
|
190 |
with c2:
|
191 |
response2=st.button("ONNX runtime")
|
|
|
|
|
|
|
|
|
192 |
|
193 |
+
if any([response1,response2]):
|
194 |
if response1:
|
195 |
start=time.time()
|
196 |
sentiments = classify_sentiment(input_texts,
|
|
|
206 |
_tokenizer=tokenizer_sentiment)
|
207 |
end = time.time()
|
208 |
st.write(f"Time taken for computation {(end - start) * 1000:.1f} ms")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
else:
|
210 |
pass
|
211 |
for i,t in enumerate(input_texts.split(',')):
|
|
|
217 |
color_background='rgb(233, 116, 81)',key=t)
|
218 |
|
219 |
if select_task=='Zero Shot Classification':
|
|
|
220 |
t1=time.time()
|
221 |
+
tokenizer_zs,zs_session = zs_task_selected(task=select_task)
|
222 |
t2 = time.time()
|
223 |
st.write(f"Total time to load Model is {(t2-t1)*1000:.1f} ms")
|
224 |
|
|
|
226 |
input_texts = st.text_input(label="Input text to classify into topics")
|
227 |
input_lables = st.text_input(label="Enter labels separated by commas")
|
228 |
|
229 |
+
c1,_,_,_=st.columns(4)
|
230 |
|
231 |
with c1:
|
232 |
+
response1=st.button("Compute with ONNX runtime")
|
233 |
+
|
234 |
+
if response1:
|
235 |
+
start = time.time()
|
236 |
+
df_output = zero_shot_classification_onnx(premise=input_texts, labels=input_lables, _session=zs_session,
|
237 |
+
_tokenizer=tokenizer_zs)
|
238 |
+
end = time.time()
|
239 |
+
st.write("")
|
240 |
+
st.write(f"Time taken for computation {(end-start)*1000:.1f} ms")
|
241 |
+
fig = px.bar(x='Probability',
|
242 |
+
y='labels',
|
243 |
+
text='Probability',
|
244 |
+
data_frame=df_output,
|
245 |
+
title='Zero Shot Normalized Probabilities')
|
246 |
+
|
247 |
+
st.plotly_chart(fig, config=_plotly_config)
|
248 |
+
else:
|
249 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
|
sentiment_clf_helper.py
CHANGED
@@ -61,9 +61,9 @@ def create_onnx_model_sentiment(_model, _tokenizer,sent_onnx_mdl_dir=sent_onnx_m
|
|
61 |
use_external_format=False
|
62 |
)
|
63 |
|
64 |
-
quantize_dynamic(f"{sent_onnx_mdl_dir}/{sent_onnx_mdl_name}",
|
65 |
-
|
66 |
-
|
67 |
else:
|
68 |
pass
|
69 |
|
|
|
61 |
use_external_format=False
|
62 |
)
|
63 |
|
64 |
+
# quantize_dynamic(f"{sent_onnx_mdl_dir}/{sent_onnx_mdl_name}",
|
65 |
+
# f"{sent_onnx_mdl_dir}/{sent_onnx_quant_mdl_name}",
|
66 |
+
# weight_type=QuantType.QUInt8)
|
67 |
else:
|
68 |
pass
|
69 |
|
zeroshot_clf_helper.py
CHANGED
@@ -65,15 +65,13 @@ def create_onnx_model_zs(zs_onnx_mdl_dir=zs_onnx_mdl_dir):
|
|
65 |
except Exception as e:
|
66 |
print(e)
|
67 |
|
68 |
-
#create quanitzed model from vanila onnx
|
69 |
-
quantize_dynamic(f"{zs_onnx_mdl_dir}/{zs_onnx_mdl_name}",
|
70 |
-
|
71 |
-
|
72 |
else:
|
73 |
pass
|
74 |
|
75 |
-
create_onnx_model_zs(zs_onnx_mdl_dir=zs_onnx_mdl_dir)
|
76 |
-
|
77 |
def zero_shot_classification_onnx(premise,labels,_session,_tokenizer):
|
78 |
try:
|
79 |
labels=labels.split(',')
|
|
|
65 |
except Exception as e:
|
66 |
print(e)
|
67 |
|
68 |
+
# #create quanitzed model from vanila onnx
|
69 |
+
# quantize_dynamic(f"{zs_onnx_mdl_dir}/{zs_onnx_mdl_name}",
|
70 |
+
# f"{zs_onnx_mdl_dir}/{zs_onnx_quant_mdl_name}",
|
71 |
+
# weight_type=QuantType.QUInt8)
|
72 |
else:
|
73 |
pass
|
74 |
|
|
|
|
|
75 |
def zero_shot_classification_onnx(premise,labels,_session,_tokenizer):
|
76 |
try:
|
77 |
labels=labels.split(',')
|