Spaces:
Build error
Build error
am4nsolanki
commited on
Commit
•
af4ccd5
1
Parent(s):
bf0c392
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import pickle
|
5 |
+
|
6 |
+
from utils import get_image_arrays, get_image_predictions, show_image
|
7 |
+
|
8 |
+
st.title('Hateful Memes Classification')
|
9 |
+
image_path = './'
|
10 |
+
demo_data_file = 'demo_data.csv'
|
11 |
+
demo_data = pd.read_csv('demo_data.csv')
|
12 |
+
TFLITE_FILE_PATH = 'image_model.tflite'
|
13 |
+
|
14 |
+
demo_data = demo_data.sample(1)
|
15 |
+
y_true = demo_data['label']
|
16 |
+
image_id = demo_data['image_id']
|
17 |
+
text = demo_data['text']
|
18 |
+
|
19 |
+
image_id_dict = dict(image_id).values()
|
20 |
+
image_id_string = list(image_id_dict)[0]
|
21 |
+
st.write('Meme:')
|
22 |
+
st.image(image_path+image_id_string)
|
23 |
+
|
24 |
+
# Image Unimodel
|
25 |
+
image_array = get_image_arrays(image_id, image_path)
|
26 |
+
image_prediction = get_image_predictions(image_array, TFLITE_FILE_PATH)
|
27 |
+
y_pred_image = np.argmax(image_prediction, axis=1)
|
28 |
+
print('Image Prediction Probabilities:')
|
29 |
+
print(image_prediction)
|
30 |
+
|
31 |
+
# TFIDF Model
|
32 |
+
model = 'tfidf_model.pickle'
|
33 |
+
vectorizer = 'tfidf_vectorizer.pickle'
|
34 |
+
tfidf_model = pickle.load(open(model, 'rb'))
|
35 |
+
tfidf_vectorizer = pickle.load(open(vectorizer, 'rb'))
|
36 |
+
transformed_text = tfidf_vectorizer.transform(text)
|
37 |
+
text_prediction = tfidf_model.predict_proba(transformed_text)
|
38 |
+
y_pred_text = np.argmax(text_prediction, axis=1)
|
39 |
+
print('Text Prediction Probabilities:')
|
40 |
+
print(text_prediction)
|
41 |
+
|
42 |
+
# Ensemble Probabilities
|
43 |
+
ensemble_prediction = np.mean(np.array([image_prediction, text_prediction]), axis=0)
|
44 |
+
y_pred_ensemble = np.argmax(ensemble_prediction, axis=1)
|
45 |
+
print(ensemble_prediction)
|
46 |
+
|
47 |
+
# StreamLit Display
|
48 |
+
st.write('Image Model Predictions:')
|
49 |
+
st.write(np.round(np.array(image_prediction), 4))
|
50 |
+
|
51 |
+
st.write('Text Model Predictions:')
|
52 |
+
st.write(np.round(np.array(text_prediction), 4))
|
53 |
+
|
54 |
+
st.write('Ensemble Model Predictions:')
|
55 |
+
st.write(np.round(np.array(ensemble_prediction), 4))
|
56 |
+
|
57 |
+
true_label = list(dict(y_true).values())[0]
|
58 |
+
predicted_label = y_pred_ensemble[0]
|
59 |
+
|
60 |
+
st.write('True Label', true_label)
|
61 |
+
st.write('Predicted Label', predicted_label)
|
62 |
+
st.write('0: non-hateful, 1: hateful')
|
63 |
+
|
64 |
+
st.button('Random Meme')
|