abspython commited on
Commit
5ff81af
·
1 Parent(s): 7f39e6e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import joblib
4
+ import easyocr
5
+ from transformers import pipeline
6
+ from keras.utils import load_img
7
+ from keras.utils import img_to_array
8
+ from PIL import Image
9
+ import io
10
+
11
+ from tempfile import NamedTemporaryFile
12
+
13
+ st.set_option('deprecation.showfileUploaderEncoding', False)
14
+
15
+ def load_image(image_file):
16
+ img = Image.open(image_file)
17
+ return img
18
+
19
+ def get_img_prediction(imgpath):
20
+ img = load_img(imgpath,target_size=(64,64,3))
21
+ img = img_to_array(img)
22
+ img = img/255
23
+
24
+ X_pred_image = np.array(img)
25
+
26
+ X_pred_imaged = X_pred_image.reshape(1,64*64*3)
27
+ y_pred_pro = loaded_lgbm.predict_proba(X_pred_imaged)
28
+ return y_pred_pro[0].tolist()
29
+
30
+
31
+ def get_text_prediction(text,imgpath):
32
+ if text == '':
33
+ result = reader.readtext(imgpath,paragraph="False")
34
+ text = []
35
+ for i in result:
36
+ text.append(i[1])
37
+ text = " ".join(text)
38
+ t_pred = get_inference(text)[0]
39
+ t_pred_c = []
40
+ for c in t_pred:
41
+ for a in c.values():
42
+ if a not in ['NEGATIVE','POSITIVE']:
43
+ t_pred_c.append(a)
44
+ return t_pred_c[::-1]
45
+
46
+
47
+ def pred_label_mean(i_pred,t_pred):
48
+ ensemble_pro = [(g + h) / 2 for g, h in zip(i_pred, t_pred)]
49
+ return ensemble_pro
50
+
51
+ def get_inference(input_text):
52
+ return bert(input_text)
53
+
54
+
55
+ loaded_lgbm = joblib.load('lgbm.sav')
56
+
57
+ bert = pipeline("text-classification", return_all_scores=True)
58
+ reader = easyocr.Reader(['en'])
59
+ st.title('Hateful Memes Classification')
60
+
61
+ image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"])
62
+ temp_file = NamedTemporaryFile(delete=False)
63
+
64
+ if image_file is not None:
65
+ # To View Uploaded Image
66
+ st.write('Meme Image:')
67
+ temp_file.write(image_file.getvalue())
68
+ imgu = load_img(temp_file.name)
69
+ st.image(imgu)
70
+
71
+ with st.spinner('Predicting Label..'):
72
+ i_pred = get_img_prediction(temp_file.name)
73
+ t_pred = get_text_prediction('',temp_file.name)
74
+ y_pred_both = pred_label_mean(i_pred,t_pred)
75
+ y_pred = y_pred_both.index(max(y_pred_both))
76
+
77
+ st.write(np.round(np.array(y_pred_both),4))
78
+ if y_pred == 0:
79
+ st.success('Predicted Label: non-hateful meme')
80
+ if y_pred == 1:
81
+ st.success('Predicted Label: hateful meme')