-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
156 lines (136 loc) · 4.46 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import streamlit as st
import pandas as pd
import numpy as np
import joblib
import altair as alt
alt.themes.enable("dark")
st.set_page_config(
page_title="Social Media Sentiment Analyzer",
layout="wide",
initial_sidebar_state="expanded",
)
st.markdown('<style>div.block-container{padding-top:0.5rem;}</style>', unsafe_allow_html=True)
white_color = "#fff"
h1 = "1.8rem"
h2 = "1.5rem"
h3 = "1.1rem"
p = "0.9rem"
font_css = f"""
<style>
body {{
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}}
.title-box {{
padding: 10px;
margin: 10px;
background-color: #333;
color: {white_color};
border-radius: 10px;
box-shadow: 0.4px 6px rgba(0,0,0,0.1);
transition: all 0.3s ease-in-out;
}}
h1 {{
font-family: 'Helvetica Neue', Arial, sans-serif !important;
font-size: {h1};
font-weight: bold;
font-stretch: condensed;
margin: 0;
letter-spacing: 0.08rem;
}}
h2 {{
font-family: 'Helvetica Neue', Arial, sans-serif !important;
font-size: {h2};
font-weight: bold;
font-stretch: condensed;
letter-spacing: 0.02rem;
}}
h3 {{
font-family: 'Helvetica Neue', Arial, sans-serif !important;
font-size: {h3};
font-weight: bold;
font-stretch: condensed;
letter-spacing: 0.01rem;
color: #edcce8;
}}
h5, p {{
font-family: 'Helvetica Neue', Arial, sans-serif !important;
color: #85888c;
font-size: {p};
}}
@media (max-width: 480px) {{
.title-box {{
padding: 10px;
margin: 10px;
}}
h1 {{
font-size: 1.4rem;
}}
}}
h2 {{
font-family: 'Helvetica Neue', Arial, sans-serif !important;
font-size: {h2};
font-weight: bold;
font-stretch: condensed;
letter-spacing: 0.02rem;
}}
h3 {{
font-family: 'Helvetica Neue', Arial, sans-serif !important;
font-size: {h3};
font-weight: bold;
font-stretch: condensed;
letter-spacing: 0.01rem;
}}
h5, p {{
font-family: 'Helvetica Neue', Arial, sans-serif !important;
color: #dbdbdb;
font-size: {p};
}}
</style>
"""
st.markdown(font_css, unsafe_allow_html=True)
# Load your trained model
pipe_lr = joblib.load(open("models/emotion_classifier_pipe_lr_Mar29_2024.pkl", "rb"))
# Prediction Function
def predict_emotions(docx):
results = pipe_lr.predict([docx])
return results[0] # Return the first and only item
# Get Prediction Probabilities
def get_prediction_proba(docx):
results = pipe_lr.predict_proba([docx])
return results
# Emoji Dictionary for Emotions
emotions_emoji_dict = {"anger": "😤", "disgust": "🤢", "fear": "😨", "joy": "😸", "surprise": "😻", "neutral": "😶", "sadness": "ðŸ˜", "shame": "🫣"}
def main():
st.title("Social Media Sentiment Analyzer AI-Bot")
st.subheader("Collection of ML Projects Created by [Fay Cai](https://www.faycai.com)")
st.write("🤖:'Trying my best to understand human emotion - [Info about my Training Data](https://www.faycai.com/data-science/the-mosaic-mind-of-ai-app)'")
for _ in range(3):
st.write("")
with st.form(key='emotion_clf_form'):
raw_text = st.text_area("Please Type Any Text Here")
submit_text = st.form_submit_button(label='submit')
if submit_text:
col1, col2 = st.columns(2)
prediction = predict_emotions(raw_text)
probability = get_prediction_proba(raw_text)
with col1:
st.success("Your Text")
st.write(raw_text)
st.success("My Prediction")
emoji_icon = emotions_emoji_dict.get(prediction, "😶") # Safely get the emoji, default to neutral if not found
st.write(f"{prediction} {emoji_icon}")
st.write("My Confidence:{}".format(np.max(probability)))
with col2:
st.success("Prediction Probability")
proba_df = pd.DataFrame(probability, columns=pipe_lr.classes_)
proba_df_clean = proba_df.T.reset_index()
proba_df_clean.columns = ["emotions", "probability"]
fig = alt.Chart(proba_df_clean).mark_bar().encode(x='emotions', y='probability', color='emotions')
st.altair_chart(fig, use_container_width=True)
if __name__ == '__main__':
main()