Spaces:
Runtime error
Runtime error
Commit
โข
3539b08
1
Parent(s):
bf536c7
first commit
Browse files- app.py +115 -0
- requirements.txt +11 -0
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Core Pkgs
|
2 |
+
import streamlit as st
|
3 |
+
import altair as alt
|
4 |
+
import plotly.express as px
|
5 |
+
|
6 |
+
|
7 |
+
# EDA Pkgs
|
8 |
+
import pandas as pd
|
9 |
+
import numpy as np
|
10 |
+
from datetime import datetime
|
11 |
+
|
12 |
+
# Utilsลล
|
13 |
+
import joblib
|
14 |
+
pipe_lr = joblib.load(open("models/emotion_classifier_pipe_lr_03_june_2021.pkl","rb"))
|
15 |
+
|
16 |
+
|
17 |
+
# Track Utils
|
18 |
+
from track_utils import create_page_visited_table,add_page_visited_details,view_all_page_visited_details,add_prediction_details,view_all_prediction_details,create_emotionclf_table
|
19 |
+
|
20 |
+
# Fxn
|
21 |
+
def predict_emotions(docx):
|
22 |
+
results = pipe_lr.predict([docx])
|
23 |
+
return results[0]
|
24 |
+
|
25 |
+
def get_prediction_proba(docx):
|
26 |
+
results = pipe_lr.predict_proba([docx])
|
27 |
+
return results
|
28 |
+
|
29 |
+
emotions_emoji_dict = {"anger":"๐ ","disgust":"๐คฎ", "fear":"๐จ๐ฑ", "happy":"๐ค", "joy":"๐", "neutral":"๐", "sad":"๐", "sadness":"๐", "shame":"๐ณ", "surprise":"๐ฎ"}
|
30 |
+
|
31 |
+
|
32 |
+
# Main Application
|
33 |
+
def main():
|
34 |
+
st.title("Text Emotion Detection \n Mini-Project By Sanyam, Aditya & Manas")
|
35 |
+
|
36 |
+
menu = ["Home","Monitor","About"]
|
37 |
+
choice = st.sidebar.selectbox("Menu",menu)
|
38 |
+
create_page_visited_table()
|
39 |
+
create_emotionclf_table()
|
40 |
+
if choice == "Home":
|
41 |
+
add_page_visited_details("Home",datetime.now())
|
42 |
+
st.subheader("Home-Emotion In Text")
|
43 |
+
|
44 |
+
with st.form(key='emotion_clf_form'):
|
45 |
+
raw_text = st.text_area("Type Here")
|
46 |
+
submit_text = st.form_submit_button(label='Submit')
|
47 |
+
|
48 |
+
if submit_text:
|
49 |
+
col1,col2 = st.beta_columns(2)
|
50 |
+
|
51 |
+
# Apply Fxn Here
|
52 |
+
prediction = predict_emotions(raw_text)
|
53 |
+
probability = get_prediction_proba(raw_text)
|
54 |
+
|
55 |
+
add_prediction_details(raw_text,prediction,np.max(probability),datetime.now())
|
56 |
+
|
57 |
+
with col1:
|
58 |
+
st.success("Original Text")
|
59 |
+
st.write(raw_text)
|
60 |
+
|
61 |
+
st.success("Prediction")
|
62 |
+
emoji_icon = emotions_emoji_dict[prediction]
|
63 |
+
st.write("{}:{}".format(prediction,emoji_icon))
|
64 |
+
st.write("Confidence:{}".format(np.max(probability)))
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
with col2:
|
69 |
+
st.success("Prediction Probability")
|
70 |
+
# st.write(probability)
|
71 |
+
proba_df = pd.DataFrame(probability,columns=pipe_lr.classes_)
|
72 |
+
# st.write(proba_df.T)
|
73 |
+
proba_df_clean = proba_df.T.reset_index()
|
74 |
+
proba_df_clean.columns = ["emotions","probability"]
|
75 |
+
|
76 |
+
fig = alt.Chart(proba_df_clean).mark_bar().encode(x='emotions',y='probability',color='emotions')
|
77 |
+
st.altair_chart(fig,use_container_width=True)
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
elif choice == "Monitor":
|
82 |
+
add_page_visited_details("Monitor",datetime.now())
|
83 |
+
st.subheader("Monitor App")
|
84 |
+
|
85 |
+
with st.beta_expander("Page Metrics"):
|
86 |
+
page_visited_details = pd.DataFrame(view_all_page_visited_details(),columns=['Pagename','Time_of_Visit'])
|
87 |
+
st.dataframe(page_visited_details)
|
88 |
+
|
89 |
+
pg_count = page_visited_details['Pagename'].value_counts().rename_axis('Pagename').reset_index(name='Counts')
|
90 |
+
c = alt.Chart(pg_count).mark_bar().encode(x='Pagename',y='Counts',color='Pagename')
|
91 |
+
st.altair_chart(c,use_container_width=True)
|
92 |
+
|
93 |
+
p = px.pie(pg_count,values='Counts',names='Pagename')
|
94 |
+
st.plotly_chart(p,use_container_width=True)
|
95 |
+
|
96 |
+
with st.beta_expander('Emotion Classifier Metrics'):
|
97 |
+
df_emotions = pd.DataFrame(view_all_prediction_details(),columns=['Rawtext','Prediction','Probability','Time_of_Visit'])
|
98 |
+
st.dataframe(df_emotions)
|
99 |
+
|
100 |
+
prediction_count = df_emotions['Prediction'].value_counts().rename_axis('Prediction').reset_index(name='Counts')
|
101 |
+
pc = alt.Chart(prediction_count).mark_bar().encode(x='Prediction',y='Counts',color='Prediction')
|
102 |
+
st.altair_chart(pc,use_container_width=True)
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
else:
|
107 |
+
st.subheader("About")
|
108 |
+
add_page_visited_details("About",datetime.now())
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
if __name__ == '__main__':
|
115 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
altair
|
5 |
+
plotlv
|
6 |
+
joblib
|
7 |
+
datetime
|
8 |
+
seaborn
|
9 |
+
neattext
|
10 |
+
sklearn
|
11 |
+
sqlite
|