add emotional arc analysis
Browse files- app.py +14 -6
- emotions.py +26 -1
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,13 +1,21 @@
|
|
1 |
import streamlit as st
|
2 |
# from emoji import get_emoji
|
3 |
-
from emotions import get_emotion
|
|
|
|
|
4 |
|
5 |
st.title("I don't even know what this is yet")
|
6 |
|
7 |
-
name = st.text_input('Who are you?', '
|
8 |
-
text = st.text_area('Submit your stories', '''
|
9 |
-
|
10 |
-
st.write(
|
11 |
-
st.write(get_emotion(text)['label'])
|
12 |
|
|
|
|
|
|
|
|
|
|
|
13 |
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
# from emoji import get_emoji
|
3 |
+
from emotions import get_emotion, get_sentiment_arc_evaluation
|
4 |
+
from nltk.tokenize import sent_tokenize
|
5 |
+
nltk.download('punkt')
|
6 |
|
7 |
st.title("I don't even know what this is yet")
|
8 |
|
9 |
+
name = st.text_input('Who are you?', 'Right, who am I?')
|
10 |
+
text = st.text_area('Submit your stories', '''Words and symbols are meant to be here''')
|
11 |
+
emotion_result = get_emotion(text)
|
12 |
+
st.write(f"Overall emotion of your story: {emotion_result[0]['label']}")
|
|
|
13 |
|
14 |
+
sents = sent_tokenize(text)
|
15 |
+
emo_arc = []
|
16 |
+
for sent in sents:
|
17 |
+
emo_arc.append(emotion_result[0]['label'])
|
18 |
+
sentiment_arc_eval = get_sentiment_arc_evaluation(emo_arc)
|
19 |
|
20 |
+
st.write(f"Emotional arc of your story: {' - '.join(emo_arc)}")
|
21 |
+
st.write(sentiment_arc_eval)
|
emotions.py
CHANGED
@@ -7,5 +7,30 @@ classifier = pipeline("text-classification",
|
|
7 |
|
8 |
def get_emotion(text='No text yet'):
|
9 |
prediction = classifier(text)[0]
|
10 |
-
result =
|
11 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def get_emotion(text='No text yet'):
|
9 |
prediction = classifier(text)[0]
|
10 |
+
result = sorted(prediction, key=lambda x: x['score'])
|
11 |
return result
|
12 |
+
|
13 |
+
|
14 |
+
sentiment_map = {'anger': 'neg', 'sadness': 'neg', 'fear': 'neg',
|
15 |
+
'joy': 'pos', 'love': 'pos', 'surprise': 'pos'}
|
16 |
+
good_arcs = ['neg - pos', 'pos - neg']
|
17 |
+
great_arcs = ['pos - neg - pos', 'neg - pos - neg']
|
18 |
+
|
19 |
+
|
20 |
+
def get_sentiment_arc_evaluation(emotions):
|
21 |
+
sentiment_arc = []
|
22 |
+
for emo in emotions:
|
23 |
+
sentiment = sentiment_map[emo]
|
24 |
+
if sentiment_arc and sentiment_arc[-1] == sentiment:
|
25 |
+
continue
|
26 |
+
sentiment_arc.append(sentiment)
|
27 |
+
sentiment_arc_str = '\n'.join(sentiment_arc)
|
28 |
+
if sentiment_arc_str in great_arcs:
|
29 |
+
return 'What a great plot! Excellent! π'
|
30 |
+
elif sentiment_arc_str in good_arcs:
|
31 |
+
return 'Story plot seems nice! But you can do better. π'
|
32 |
+
elif len(sentiment_arc) < 2:
|
33 |
+
return "No judgment, but... The plot might be too simple! π€"
|
34 |
+
else:
|
35 |
+
return "The plot seems complicated. π€ But maybe I am just too stupid to understand!"
|
36 |
+
|
requirements.txt
CHANGED
@@ -2,4 +2,5 @@ streamlit
|
|
2 |
transformers
|
3 |
torch
|
4 |
numpy
|
5 |
-
scipy
|
|
|
|
2 |
transformers
|
3 |
torch
|
4 |
numpy
|
5 |
+
scipy
|
6 |
+
nltk
|