-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
76 lines (55 loc) · 2.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import streamlit as st
import tensorflow as tf
import pandas as pd
from clean import token_word
import nltk_download_utils
MAX_LENGTH = 25
def load_model():
loaded_model = tf.keras.models.load_model('models/my_model.h5')
return loaded_model
def predict_sarcasm(text):
x_final = pd.DataFrame({"headline": [text]})
test_lines = token_word(x_final)
tokenizer_obj = tf.keras.preprocessing.text.Tokenizer()
tokenizer_obj.fit_on_texts(test_lines)
test_sequences = tokenizer_obj.texts_to_sequences(test_lines)
test_review_pad = tf.keras.preprocessing.sequence.pad_sequences(
test_sequences, maxlen=MAX_LENGTH, padding='post')
model = load_model()
pred = model.predict(test_review_pad)
predict_sarcasm.probab = pred
if pred[0][0] >= 0.5:
st.image('static/images/bazinga.jpg', width=200, caption="Shelly")
return "It's a sarcasm!"
else:
return "It's not a sarcasm."
def main():
st.sidebar.title("Created By:")
st.sidebar.subheader("Rupesh Gelal")
st.sidebar.subheader(
"[GitHub Repository](https://github.com/rgrupesh/sarcasm-detector)")
st.sidebar.subheader("[Personal Site](https://rupeshgelal.com.np/)")
st.title("Sarcasm detector")
with st.form(key='sarcasm_form'):
raw_text = st.text_area("Enter your text below:")
submit_text = st.form_submit_button(label='Submit')
if submit_text:
if raw_text == "":
st.exception(NameError("Please enter some text to test."))
else:
col1, col2 = st.columns(2)
prediction = predict_sarcasm(raw_text)
with col1:
st.success("Your input")
st.write(raw_text)
st.success("Prediction")
st.write(prediction)
with col2:
st.success("Probability")
st.write(float(predict_sarcasm.probab))
st.subheader("Sample prediction:")
st.success("You broke my laptop. Thank you! \n-sarcastic \n \nHi, I’m Chandler. I make jokes when I’m uncomfortable. \n-sarcastic")
st.success(
"You saved my dog. \n-not sarcastic \n \nSarcasm is easy. \n-not sarcastic")
if __name__ == "__main__":
main()