-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (58 loc) · 1.91 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
import streamlit as st
import pandas as pd
import numpy as np
import altair as alt
import joblib
import os
app_dir = os.path.dirname(__file__)
model_path = "models/emotion_classifier_pipeline.pkl"
file_path = os.path.join(app_dir, model_path)
print("file_path", file_path)
pipe_lr = joblib.load(open(file_path, "rb"))
def predict_emotion(docx):
return pipe_lr.predict([docx])
def get_prediction_proba(docx):
return pipe_lr.predict_proba([docx])
emotions_dict = {
"anger": "Raiva 😠",
"disgust": "Aversão 😖",
"fear": "Medo 😨",
"happy": "Felicidade 😁",
"joy": "Alegria 😊",
"neutral": "Neutro 😐",
"sad": "Triste 😢",
"sadness": "Tristeza 😥",
"shame": "Vergonha 😳",
"surprise": "Surpresa 😲"
}
def main():
st.title("Emotion Classifier")
st.subheader("Emotion Classifier")
with st.form(key='emotion_clf_form'):
raw_text = st.text_area("Enter your Text:")
submit_text = st.form_submit_button(label='Send')
if submit_text:
st.markdown("""---""")
# Prediction
prediction = predict_emotion(raw_text)
probability = get_prediction_proba(raw_text)
st.subheader("Original Text")
st.write(raw_text)
st.subheader("Prediction")
emotion = emotions_dict[prediction[0]]
st.write(emotion)
st.write("Confidence: {} %".format(np.max(probability) * 100))
# Visualization
st.subheader("Prediction Probability")
proba_df = pd.DataFrame(probability, columns=pipe_lr.classes_)
st.write(proba_df)
proba_df_clean = proba_df.T.reset_index()
proba_df_clean.columns = ["Emotion", "Probability"]
fig = alt.Chart(proba_df_clean).mark_bar().encode(
x='Emotion',
y='Probability',
color='Emotion'
)
st.altair_chart(fig, use_container_width=True)
if __name__ == '__main__':
main()