Spaces:
Runtime error
Runtime error
Poe Dator
commited on
Commit
·
4643ef6
1
Parent(s):
ece0adb
buttons introduced
Browse files
app.py
CHANGED
|
@@ -41,9 +41,8 @@ def build_model():
|
|
| 41 |
st.markdown("Model weights loaded")
|
| 42 |
return model
|
| 43 |
|
| 44 |
-
def inference(txt
|
| 45 |
-
# infers classes for text topic based on
|
| 46 |
-
# has separate mode 'print' for just output
|
| 47 |
t2 = tokenizer(txt.lower().replace('\n', ''),
|
| 48 |
padding='max_length', max_length = 512, truncation=True,
|
| 49 |
return_tensors="pt")
|
|
@@ -56,38 +55,51 @@ def inference(txt, mode=None):
|
|
| 56 |
out = out/out.sum() * 100
|
| 57 |
res = [(l, o) for l, o in zip (list(labels.keys()), out.tolist())]
|
| 58 |
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
image = Image.open('dilbert_big_data.jpg')
|
| 64 |
st.image(image)
|
|
|
|
| 65 |
# st.markdown("<img width=200px src='https://i.pinimg.com/736x/11/33/19/113319f0ffe91f4bb0f468914b9916da.jpg'>", unsafe_allow_html=True)
|
|
|
|
| 66 |
|
| 67 |
-
text = st.text_area("ENTER
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
st.
|
| 75 |
-
for lbl, score in res:
|
| 76 |
-
if score >=1:
|
| 77 |
-
st.markdown(f"[ {lbl:<7}] {labels_decoder[lbl]:<35} {score:.1f}%")
|
| 78 |
-
|
| 79 |
-
res_plot = []
|
| 80 |
-
total=0
|
| 81 |
-
for r in res:
|
| 82 |
-
if total < 95:
|
| 83 |
-
res_plot.append(r)
|
| 84 |
-
total += r[1]
|
| 85 |
-
else:
|
| 86 |
-
break
|
| 87 |
|
| 88 |
-
fig, ax = plt.subplots(figsize=(10, len(res_plot)+1))
|
| 89 |
-
for r in res_plot :
|
| 90 |
-
ax.barh(r[0], r[1])
|
| 91 |
-
st.pyplot(fig)
|
| 92 |
|
| 93 |
-
st.markdown(f"cycle time = {time() - start_time:.2f} s.")
|
|
|
|
| 41 |
st.markdown("Model weights loaded")
|
| 42 |
return model
|
| 43 |
|
| 44 |
+
def inference(txt):
|
| 45 |
+
# infers classes for text topic based on loaded trained model
|
|
|
|
| 46 |
t2 = tokenizer(txt.lower().replace('\n', ''),
|
| 47 |
padding='max_length', max_length = 512, truncation=True,
|
| 48 |
return_tensors="pt")
|
|
|
|
| 55 |
out = out/out.sum() * 100
|
| 56 |
res = [(l, o) for l, o in zip (list(labels.keys()), out.tolist())]
|
| 57 |
return res
|
| 58 |
+
|
| 59 |
+
def infer_and_display_result(txt):
|
| 60 |
+
start_time = time()
|
| 61 |
+
|
| 62 |
+
res = inference(txt, mode=None)
|
| 63 |
+
res.sort(key = lambda x : - x[1])
|
| 64 |
|
| 65 |
+
st.markdown("###Inference results:")
|
| 66 |
+
for lbl, score in res:
|
| 67 |
+
if score >=1:
|
| 68 |
+
st.write(f"[ {lbl:<7}] {labels_decoder[lbl]:<35} {score:.1f}%")
|
| 69 |
|
| 70 |
+
res_plot = [] # storage for plot data
|
| 71 |
+
total=0
|
| 72 |
+
for r in res:
|
| 73 |
+
if total < 95:
|
| 74 |
+
res_plot.append(r)
|
| 75 |
+
total += r[1]
|
| 76 |
+
else:
|
| 77 |
+
break
|
| 78 |
+
res.sort(key = lambda x : x[1])
|
| 79 |
+
|
| 80 |
+
fig, ax = plt.subplots(figsize=(10, len(res_plot)))
|
| 81 |
+
for r in res_plot :
|
| 82 |
+
ax.barh(r[0], r[1])
|
| 83 |
+
st.pyplot(fig)
|
| 84 |
+
st.markdown(f"cycle time = {time() - start_time:.2f} s.")
|
| 85 |
+
|
| 86 |
+
# ======================================
|
| 87 |
+
model = build_model()
|
| 88 |
+
|
| 89 |
+
st.title('Big-data cloud application for scientific article topic inference using in-memory computing and stuff.')
|
| 90 |
image = Image.open('dilbert_big_data.jpg')
|
| 91 |
st.image(image)
|
| 92 |
+
st.write('test application for ML-2 class, YSDA-2022' )
|
| 93 |
# st.markdown("<img width=200px src='https://i.pinimg.com/736x/11/33/19/113319f0ffe91f4bb0f468914b9916da.jpg'>", unsafe_allow_html=True)
|
| 94 |
+
# st.markdown("###Predict topic by abstract.")
|
| 95 |
|
| 96 |
+
text = st.text_area("ENTER ARTICLE TITLE OR ABSTRACT HERE")
|
| 97 |
+
action = st.button('click here to infer topic')
|
| 98 |
+
if action:
|
| 99 |
+
infer_and_display_result(text)
|
| 100 |
+
|
| 101 |
+
action2 = st.button('click here to infer topic')
|
| 102 |
+
if action2:
|
| 103 |
+
st.write(text.upper())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
|
|