Ruchi Toshniwal commited on
Commit
f031eb7
โ€ข
1 Parent(s): 1cfbd32

first commit

Browse files
Files changed (2) hide show
  1. app.py +80 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import packages
2
+ import streamlit as st
3
+ import altair as alt
4
+ import pandas as pd
5
+ import numpy as np
6
+ import joblib
7
+ from transformers import pipeline
8
+
9
+ # Load pipeline
10
+ pipe_lr = joblib.load(open("emotion_detector_pipe_lr.pkl","rb"))
11
+ emoroberta_emotion_classifier = pipeline(
12
+ "text-classification", model="arpanghoshal/EmoRoBERTa", return_all_scores=True
13
+ )
14
+
15
+ # Emojis
16
+ emotions_emoji_dict = {"anger":"๐Ÿ˜ ","disgust":"๐Ÿคฎ", "fear":"๐Ÿ˜จ๐Ÿ˜ฑ", "joy":"๐Ÿค—", "neutral":"๐Ÿ˜", "sadness":"๐Ÿ˜”", "shame":"๐Ÿ˜ณ", "surprise":"๐Ÿ˜ฎ"}
17
+
18
+ # Functions
19
+ def predict_emotions(text):
20
+ result = pipe_lr.predict([text])
21
+ return result[0]
22
+
23
+ def get_prediction_proba(text):
24
+ result = pipe_lr.predict_proba([text])
25
+ return result
26
+
27
+ def predict_emotions_and_score_emoroberta(text):
28
+ emotionlabel = emoroberta_emotion_classifier(text)
29
+ emotion_dataframe = pd.DataFrame.from_records(emotionlabel[0]).sort_values(by=["score"], ascending=False)
30
+ emotion = emotion_dataframe.iloc[0, 0]
31
+ score = emotion_dataframe.iloc[0, 1]
32
+ return emotion_dataframe, emotion, score
33
+
34
+ def main():
35
+ st.title("Emotion Detection App")
36
+ with st.form(key='emotion_detection_form'):
37
+ raw_text = st.text_area("Type here")
38
+ submit_text = st.form_submit_button(label = 'Submit')
39
+ st.subheader("Predictions from LR model trained on labeled data (8 emotions)")
40
+ if submit_text:
41
+ col1,col2 = st.columns(2)
42
+ prediction = predict_emotions(raw_text)
43
+ prediction_probability = get_prediction_proba(raw_text)
44
+ with col1:
45
+ st.success("Original Text")
46
+ st.write(raw_text)
47
+ st.success("Emotion")
48
+ emoji_icon = emotions_emoji_dict[prediction]
49
+ st.write("{} {}".format(prediction,emoji_icon))
50
+ st.success("Emotion Score")
51
+ st.write("{:.4f}".format(np.max(prediction_probability)))
52
+
53
+ with col2:
54
+ st.success("Prediction Probability")
55
+ proba_df = pd.DataFrame(prediction_probability,columns = pipe_lr.classes_)
56
+ proba_df_clean = proba_df.T.reset_index()
57
+ proba_df_clean.columns = ["emotions","probability"]
58
+ fig = alt.Chart(proba_df_clean).mark_bar().encode(y=alt.Y('emotions', sort='-x'),x='probability',color='emotions')
59
+ st.altair_chart(fig,use_container_width=True)
60
+
61
+ st.markdown("***")
62
+
63
+ st.subheader("Predictions from EmoRoBERTa - BERT based pre-trained model (28 emotions)")
64
+
65
+ col3,col4 = st.columns(2)
66
+ emotion_dataframe_emoroberta, predicted_emotion_emoroberta, prediction_probability_emoroberta = predict_emotions_and_score_emoroberta(raw_text)
67
+ with col3:
68
+ st.success("Emotion")
69
+ st.write(predicted_emotion_emoroberta)
70
+
71
+ with col4:
72
+ st.success("Emotion Score")
73
+ st.write("{:.4f}".format(np.max(prediction_probability_emoroberta)))
74
+ st.success("Prediction Probability")
75
+ emotion_dataframe_emoroberta.columns = ["emotions","probability"]
76
+ fig = alt.Chart(emotion_dataframe_emoroberta).mark_bar().encode(y=alt.Y('emotions', sort='-x'),x='probability',color='emotions')
77
+ st.altair_chart(fig,use_container_width=True)
78
+
79
+ if __name__ == '__main__':
80
+ main()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ altair
3
+ pandas
4
+ numpy
5
+ joblib
6
+ transformers