Spaces:
Runtime error
Runtime error
refactors
Browse files- app.py +14 -124
- src/__init__.py +4 -0
- src/create_statistics.py +59 -0
- src/lib.py +32 -0
- src/play_storytelling.py +53 -0
- src/probability_emote.py +86 -77
- src/story_gen.py +8 -9
app.py
CHANGED
@@ -1,141 +1,31 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from src.story_gen import StoryGenerator
|
3 |
-
from src.probability_emote import run_pe
|
4 |
-
import plotly.express as px
|
5 |
import random
|
|
|
6 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
st.set_page_config(page_title='Storytelling ' +
|
9 |
-
u'\U0001F5BC', page_icon=u'\U0001F5BC', layout="wide"
|
|
|
10 |
gen = StoryGenerator()
|
11 |
container_mode = st.sidebar.container()
|
12 |
container_guide = st.container()
|
13 |
container_param = st.sidebar.container()
|
14 |
container_button = st.sidebar.container()
|
|
|
15 |
mode = container_mode.radio(
|
16 |
"Select a mode",
|
17 |
('Probability Emote', 'Create Statistics', 'Play Storytelling'), index=0)
|
18 |
|
19 |
|
20 |
-
def initialise_storytelling():
|
21 |
-
choices_first_sentence = [
|
22 |
-
'Custom',
|
23 |
-
'Hello, I\'m a language model,',
|
24 |
-
'So I suppose you want to ask me how I did it.',
|
25 |
-
'I always wanted to be a giraffe - until that night.',
|
26 |
-
'My first tutor was a dragon with a terrible sense of humor.',
|
27 |
-
'Doctors told her she could never diet again.',
|
28 |
-
'Memory is all around us, as well as within.',
|
29 |
-
|
30 |
-
|
31 |
-
]
|
32 |
-
cfs = st.selectbox('Choose First Sentence', choices_first_sentence)
|
33 |
-
if cfs == 'Custom':
|
34 |
-
story_till_now = st.text_input(
|
35 |
-
label='First Sentence', key='first_sentence')
|
36 |
-
else:
|
37 |
-
st.session_state.first_sentence = cfs
|
38 |
-
story_till_now = cfs
|
39 |
-
first_sentence = story_till_now
|
40 |
-
first_emotion = gen.get_emotion(first_sentence)
|
41 |
-
|
42 |
-
length = container_param.slider(label='Length of the generated sentence',
|
43 |
-
min_value=1, max_value=100, value=10, step=1)
|
44 |
-
return first_sentence, first_emotion, length
|
45 |
-
|
46 |
-
|
47 |
if mode == 'Create Statistics':
|
48 |
-
|
49 |
-
|
50 |
-
num_generation = container_param.slider(
|
51 |
-
label='Number of generation', min_value=1, max_value=100, value=5, step=1)
|
52 |
-
num_tests = container_param.slider(
|
53 |
-
label='Number of tests', min_value=1, max_value=1000, value=3, step=1)
|
54 |
-
reaction_weight_mode = container_param.select_slider(
|
55 |
-
"Reaction Weight w:", ["Random", "Fixed"])
|
56 |
-
if reaction_weight_mode == "Fixed":
|
57 |
-
reaction_weight = container_param.slider(
|
58 |
-
label='Reaction Weight w', min_value=0.0, max_value=1.0, value=0.5, step=0.01)
|
59 |
-
elif reaction_weight_mode == "Random":
|
60 |
-
reaction_weight = -1
|
61 |
-
if container_button.button('Analyse'):
|
62 |
-
gen.get_stats(story_till_now=first_sentence,
|
63 |
-
num_generation=num_generation, length=length, reaction_weight=reaction_weight, num_tests=num_tests)
|
64 |
-
# if len(gen.stories) > 0:
|
65 |
-
# for si, story in enumerate(gen.stories):
|
66 |
-
# st.markdown(f'### Story no. {si}:', unsafe_allow_html=False)
|
67 |
-
# st.markdown(story, unsafe_allow_html=False)
|
68 |
-
# data=gen.stats_df[gen.stats_df.sentence_no==3]
|
69 |
-
# fig = px.violin(data_frame=data, x="reaction_weight", y="num_reactions", hover_data=data.columns)
|
70 |
-
# st.plotly_chart(fig, use_container_width=True)
|
71 |
-
# fig2 = px.box(data_frame=data, x="reaction_weight", y="num_reactions", hover_data=data.columns)
|
72 |
-
# st.plotly_chart(fig2, use_container_width=True)
|
73 |
-
if len(gen.data) > 0:
|
74 |
-
for si, story in enumerate(gen.data):
|
75 |
-
st.markdown(f'### Story {si}:', unsafe_allow_html=False)
|
76 |
-
for i, sentence in enumerate(story):
|
77 |
-
col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
|
78 |
-
col_turn.markdown(
|
79 |
-
sentence['turn'], unsafe_allow_html=False)
|
80 |
-
col_sentence.markdown(
|
81 |
-
sentence['sentence'], unsafe_allow_html=False)
|
82 |
-
col_emo.markdown(
|
83 |
-
f'{sentence["emotion"]} {np.round(sentence["confidence_score"], 3)}', unsafe_allow_html=False)
|
84 |
-
st.table(data=gen.stats_df, )
|
85 |
-
data = gen.stats_df[gen.stats_df.sentence_no == 3]
|
86 |
-
fig = px.violin(data_frame=data, x="reaction_weight",
|
87 |
-
y="num_reactions", hover_data=data.columns)
|
88 |
-
st.plotly_chart(fig, use_container_width=True)
|
89 |
-
fig2 = px.box(data_frame=data, x="reaction_weight",
|
90 |
-
y="num_reactions", hover_data=data.columns)
|
91 |
-
st.plotly_chart(fig2, use_container_width=True)
|
92 |
-
else:
|
93 |
-
container_guide.markdown(
|
94 |
-
'### You selected statistics. Now set your parameters and click the `Analyse` button.')
|
95 |
elif mode == 'Play Storytelling':
|
96 |
-
|
97 |
-
|
98 |
-
if 'sentence_list' not in st.session_state:
|
99 |
-
st.session_state.sentence_list = [{'sentence': first_sentence,
|
100 |
-
'emotion': first_emotion['label'],
|
101 |
-
'score': first_emotion['score']}]
|
102 |
-
if 'full_story' not in st.session_state:
|
103 |
-
st.session_state.full_story = first_sentence
|
104 |
-
container_button = container_button.columns([1, 1, 1])
|
105 |
-
heading_container = st.container()
|
106 |
-
col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
|
107 |
-
if container_button[0].button('Run'):
|
108 |
-
heading_container.markdown(f'### Story')
|
109 |
-
|
110 |
-
# st.text(story_till_now)
|
111 |
-
full_story, emotion, new_sentence = gen.next_sentence(
|
112 |
-
st.session_state.full_story, length)
|
113 |
-
st.session_state.full_story = full_story
|
114 |
-
st.session_state.sentence_list.append({
|
115 |
-
'sentence': new_sentence,
|
116 |
-
'emotion': emotion["label"],
|
117 |
-
'score': emotion["score"]})
|
118 |
-
# col_sentence.markdown(st.session_state.sentence_list)
|
119 |
-
for step in st.session_state.sentence_list:
|
120 |
-
col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
|
121 |
-
col_sentence.markdown(step['sentence'])
|
122 |
-
col_emo.markdown(
|
123 |
-
f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
|
124 |
-
|
125 |
-
else:
|
126 |
-
step = st.session_state.sentence_list[0]
|
127 |
-
# col_sentence.markdown(step['sentence'])
|
128 |
-
# col_emo.markdown(
|
129 |
-
# f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
|
130 |
-
container_guide.markdown(
|
131 |
-
'### Write the first sentence and then hit the `Run` button')
|
132 |
-
|
133 |
-
if container_button[2].button('Clear'):
|
134 |
-
|
135 |
-
st.session_state.full_story = first_sentence
|
136 |
-
st.session_state.sentence_list = [{'sentence': first_sentence,
|
137 |
-
'emotion': first_emotion['label'],
|
138 |
-
'score': first_emotion['score']}]
|
139 |
elif mode == 'Probability Emote':
|
140 |
-
|
141 |
-
run_pe(container_param)
|
|
|
|
|
|
|
|
|
|
|
1 |
import random
|
2 |
+
|
3 |
import numpy as np
|
4 |
+
import plotly.express as px
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
from src import (StoryGenerator, run_create_statistics, run_play_storytelling,
|
8 |
+
run_probability_emote)
|
9 |
|
10 |
st.set_page_config(page_title='Storytelling ' +
|
11 |
+
u'\U0001F5BC', page_icon=u'\U0001F5BC', layout="wide",
|
12 |
+
)
|
13 |
gen = StoryGenerator()
|
14 |
container_mode = st.sidebar.container()
|
15 |
container_guide = st.container()
|
16 |
container_param = st.sidebar.container()
|
17 |
container_button = st.sidebar.container()
|
18 |
+
|
19 |
mode = container_mode.radio(
|
20 |
"Select a mode",
|
21 |
('Probability Emote', 'Create Statistics', 'Play Storytelling'), index=0)
|
22 |
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
if mode == 'Create Statistics':
|
25 |
+
run_create_statistics(gen, container_guide,
|
26 |
+
container_param, container_button)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
elif mode == 'Play Storytelling':
|
28 |
+
run_play_storytelling(gen, container_guide,
|
29 |
+
container_param, container_button)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
elif mode == 'Probability Emote':
|
31 |
+
run_probability_emote(container_param)
|
|
src/__init__.py
CHANGED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .story_gen import StoryGenerator
|
2 |
+
from .probability_emote import run_probability_emote
|
3 |
+
from .create_statistics import run_create_statistics
|
4 |
+
from .play_storytelling import run_play_storytelling
|
src/create_statistics.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import plotly.express as px
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
from .lib import initialise_storytelling
|
8 |
+
|
9 |
+
|
10 |
+
def run_create_statistics(gen, container_guide, container_param, container_button):
|
11 |
+
|
12 |
+
first_sentence, first_emotion, length = initialise_storytelling(
|
13 |
+
gen, container_guide, container_param, container_button)
|
14 |
+
# story_till_now = first_sentence
|
15 |
+
num_generation = container_param.slider(
|
16 |
+
label='Number of generation', min_value=1, max_value=100, value=5, step=1)
|
17 |
+
num_tests = container_param.slider(
|
18 |
+
label='Number of tests', min_value=1, max_value=1000, value=3, step=1)
|
19 |
+
reaction_weight_mode = container_param.select_slider(
|
20 |
+
"Reaction Weight w:", ["Random", "Fixed"])
|
21 |
+
if reaction_weight_mode == "Fixed":
|
22 |
+
reaction_weight = container_param.slider(
|
23 |
+
label='Reaction Weight w', min_value=0.0, max_value=1.0, value=0.5, step=0.01)
|
24 |
+
elif reaction_weight_mode == "Random":
|
25 |
+
reaction_weight = -1
|
26 |
+
if container_button.button('Analyse'):
|
27 |
+
gen.get_stats(story_till_now=first_sentence,
|
28 |
+
num_generation=num_generation, length=length, reaction_weight=reaction_weight, num_tests=num_tests)
|
29 |
+
# if len(gen.stories) > 0:
|
30 |
+
# for si, story in enumerate(gen.stories):
|
31 |
+
# st.markdown(f'### Story no. {si}:', unsafe_allow_html=False)
|
32 |
+
# st.markdown(story, unsafe_allow_html=False)
|
33 |
+
# data=gen.stats_df[gen.stats_df.sentence_no==3]
|
34 |
+
# fig = px.violin(data_frame=data, x="reaction_weight", y="num_reactions", hover_data=data.columns)
|
35 |
+
# st.plotly_chart(fig, use_container_width=True)
|
36 |
+
# fig2 = px.box(data_frame=data, x="reaction_weight", y="num_reactions", hover_data=data.columns)
|
37 |
+
# st.plotly_chart(fig2, use_container_width=True)
|
38 |
+
if len(gen.data) > 0:
|
39 |
+
for si, story in enumerate(gen.data):
|
40 |
+
st.markdown(f'### Story {si}:', unsafe_allow_html=False)
|
41 |
+
for i, sentence in enumerate(story):
|
42 |
+
col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
|
43 |
+
col_turn.markdown(
|
44 |
+
sentence['turn'], unsafe_allow_html=False)
|
45 |
+
col_sentence.markdown(
|
46 |
+
sentence['sentence'], unsafe_allow_html=False)
|
47 |
+
col_emo.markdown(
|
48 |
+
f'{sentence["emotion"]} {np.round(sentence["confidence_score"], 3)}', unsafe_allow_html=False)
|
49 |
+
st.table(data=gen.stats_df, )
|
50 |
+
data = gen.stats_df[gen.stats_df.sentence_no == 3]
|
51 |
+
fig = px.violin(data_frame=data, x="reaction_weight",
|
52 |
+
y="num_reactions", hover_data=data.columns)
|
53 |
+
st.plotly_chart(fig, use_container_width=True)
|
54 |
+
fig2 = px.box(data_frame=data, x="reaction_weight",
|
55 |
+
y="num_reactions", hover_data=data.columns)
|
56 |
+
st.plotly_chart(fig2, use_container_width=True)
|
57 |
+
else:
|
58 |
+
container_guide.markdown(
|
59 |
+
'### You selected statistics. Now set your parameters and click the `Analyse` button.')
|
src/lib.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
from src import StoryGenerator
|
6 |
+
|
7 |
+
|
8 |
+
def initialise_storytelling(gen, container_guide, container_param, container_button):
|
9 |
+
choices_first_sentence = [
|
10 |
+
'Custom',
|
11 |
+
'Hello, I\'m a language model,',
|
12 |
+
'So I suppose you want to ask me how I did it.',
|
13 |
+
'I always wanted to be a giraffe - until that night.',
|
14 |
+
'My first tutor was a dragon with a terrible sense of humor.',
|
15 |
+
'Doctors told her she could never diet again.',
|
16 |
+
'Memory is all around us, as well as within.',
|
17 |
+
|
18 |
+
|
19 |
+
]
|
20 |
+
cfs = st.selectbox('Choose First Sentence', choices_first_sentence)
|
21 |
+
if cfs == 'Custom':
|
22 |
+
story_till_now = st.text_input(
|
23 |
+
label='First Sentence', key='first_sentence')
|
24 |
+
else:
|
25 |
+
st.session_state.first_sentence = cfs
|
26 |
+
story_till_now = cfs
|
27 |
+
first_sentence = story_till_now
|
28 |
+
first_emotion = gen.get_emotion(first_sentence)
|
29 |
+
|
30 |
+
length = container_param.slider(label='Length of the generated sentence',
|
31 |
+
min_value=1, max_value=100, value=10, step=1)
|
32 |
+
return first_sentence, first_emotion, length
|
src/play_storytelling.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
from .lib import initialise_storytelling
|
7 |
+
|
8 |
+
|
9 |
+
def run_play_storytelling(gen, container_guide, container_param, container_button):
|
10 |
+
first_sentence, first_emotion, length = initialise_storytelling(
|
11 |
+
gen, container_guide, container_param, container_button)
|
12 |
+
# story_till_now = first_sentence
|
13 |
+
if 'sentence_list' not in st.session_state:
|
14 |
+
st.session_state.sentence_list = [{'sentence': first_sentence,
|
15 |
+
'emotion': first_emotion['label'],
|
16 |
+
'score': first_emotion['score']}]
|
17 |
+
if 'full_story' not in st.session_state:
|
18 |
+
st.session_state.full_story = first_sentence
|
19 |
+
container_button = container_button.columns([1, 1, 1])
|
20 |
+
heading_container = st.container()
|
21 |
+
col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
|
22 |
+
if container_button[0].button('Run'):
|
23 |
+
heading_container.markdown(f'### Story')
|
24 |
+
|
25 |
+
# st.text(story_till_now)
|
26 |
+
full_story, emotion, new_sentence = gen.next_sentence(
|
27 |
+
st.session_state.full_story, length)
|
28 |
+
st.session_state.full_story = full_story
|
29 |
+
st.session_state.sentence_list.append({
|
30 |
+
'sentence': new_sentence,
|
31 |
+
'emotion': emotion["label"],
|
32 |
+
'score': emotion["score"]})
|
33 |
+
# col_sentence.markdown(st.session_state.sentence_list)
|
34 |
+
for step in st.session_state.sentence_list:
|
35 |
+
col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
|
36 |
+
col_sentence.markdown(step['sentence'])
|
37 |
+
col_emo.markdown(
|
38 |
+
f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
|
39 |
+
|
40 |
+
else:
|
41 |
+
step = st.session_state.sentence_list[0]
|
42 |
+
# col_sentence.markdown(step['sentence'])
|
43 |
+
# col_emo.markdown(
|
44 |
+
# f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
|
45 |
+
container_guide.markdown(
|
46 |
+
'### Write the first sentence and then hit the `Run` button')
|
47 |
+
|
48 |
+
if container_button[2].button('Clear'):
|
49 |
+
|
50 |
+
st.session_state.full_story = first_sentence
|
51 |
+
st.session_state.sentence_list = [{'sentence': first_sentence,
|
52 |
+
'emotion': first_emotion['label'],
|
53 |
+
'score': first_emotion['score']}]
|
src/probability_emote.py
CHANGED
@@ -1,10 +1,7 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import plotly.express as px
|
4 |
import plotly.graph_objects as go
|
|
|
5 |
|
6 |
-
import numpy as np
|
7 |
-
import numpy as np
|
8 |
|
9 |
@st.cache
|
10 |
def get_w(f, ec=0.86, rv=0.50):
|
@@ -13,6 +10,7 @@ def get_w(f, ec=0.86, rv=0.50):
|
|
13 |
print(f'w = {result}')
|
14 |
return result
|
15 |
|
|
|
16 |
@st.cache
|
17 |
def get_f(w, ec=0.86, rv=0.50):
|
18 |
result = 1+(ec*w-rv)/(1-w)
|
@@ -20,6 +18,7 @@ def get_f(w, ec=0.86, rv=0.50):
|
|
20 |
print(f'f = {result}')
|
21 |
return result
|
22 |
|
|
|
23 |
@st.cache
|
24 |
def get_pe(w, ec=0.86, f=0.50):
|
25 |
result = ec*w+(1-w)*(1-f)
|
@@ -27,44 +26,48 @@ def get_pe(w, ec=0.86, f=0.50):
|
|
27 |
print(f'f = {result}')
|
28 |
return result
|
29 |
|
|
|
30 |
xdata1 = np.arange(0, 1.1, step=0.01)
|
31 |
# rand = np.random.random_sample()
|
32 |
|
|
|
33 |
@st.cache
|
34 |
def proper_float(i):
|
35 |
return np.round(i, 2)
|
36 |
|
|
|
37 |
@st.cache
|
38 |
def get_text():
|
39 |
-
return
|
40 |
|
41 |
-
## Description
|
42 |
-
### Eric's proposal
|
43 |
-
> I would propose a scoring metric something like this:
|
44 |
-
> * `probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty`
|
45 |
-
>
|
46 |
-
> Where:
|
47 |
-
> * emotion_confidence: a score from 0.0 to 1.0 representing the emotion model’s confidence in it’s classification results
|
48 |
-
> * frequency_penalty: a score from 0.0 to 1.0 where a high score penalizes frequent emotes
|
49 |
-
> * `frequency_penalty = 1 - emotion_frequency`
|
50 |
-
> * w: a weight from 0.0 to 1.0 that controls the balance between emotion_confidence and frequency_penalty
|
51 |
-
> * Then you generate a random number between 0.0 and 1.0 and emote if it is greater than probability_emote
|
52 |
-
> * You will have to set frequency_penalty and w through trial and error, but you can start with setting w=0.5 and giving the emotion classifier and frequency penalty equal weight.
|
53 |
-
> * Setting w=1.0 would disable the frequency penalty altogether
|
54 |
'''
|
55 |
|
|
|
56 |
@st.cache
|
57 |
def get_equation_text(w=0.5, ec=0.7, rand=None, emotion_frequency=None):
|
58 |
-
text =
|
59 |
#### Equation
|
60 |
* frequency_penalty = 1 - emotion_frequency
|
61 |
* probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty
|
62 |
* **probability_emote** = {proper_float(w)} * {proper_float(ec)} + {proper_float(1-w)} * frequency_penalty
|
63 |
|
64 |
-
'''
|
65 |
if rand is not None:
|
66 |
-
frequency_penalty=proper_float(1-emotion_frequency)
|
67 |
-
probability_emote=proper_float((w)*(ec)+(1-w)*frequency_penalty)
|
68 |
text = f'''
|
69 |
#### Equation
|
70 |
* frequency_penalty = 1 - emotion_frequency = 1 - {emotion_frequency} = {frequency_penalty}
|
@@ -76,75 +79,79 @@ def get_equation_text(w=0.5, ec=0.7, rand=None, emotion_frequency=None):
|
|
76 |
* Random value = {rand}
|
77 |
* Show_Emotion = {probability_emote} > {rand}
|
78 |
* **Show_Emotion** = {probability_emote > rand}
|
79 |
-
'''
|
80 |
return text
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
88 |
def slider2input():
|
89 |
st.session_state[key_input] = st.session_state[key_slider]
|
|
|
90 |
def input2slider():
|
91 |
st.session_state[key_slider] = st.session_state[key_input]
|
92 |
-
container_param=container_param.columns([1.1, 1])
|
93 |
slider_input = container_param[1].slider(
|
94 |
-
label=label,
|
95 |
min_value=min_value,
|
96 |
max_value=max_value,
|
97 |
value=value,
|
98 |
step=step,
|
99 |
-
key
|
100 |
on_change=slider2input)
|
101 |
-
number_input= container_param[0].number_input(
|
102 |
-
label='',
|
103 |
min_value=min_value,
|
104 |
max_value=max_value,
|
105 |
value=value,
|
106 |
step=step,
|
107 |
-
key
|
108 |
on_change=input2slider)
|
109 |
return slider_input, number_input
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
124 |
# score = container_param.slider(
|
125 |
-
# label='Confidence Score',
|
126 |
# min_value=0.,
|
127 |
# max_value=1.,
|
128 |
# value=.5,
|
129 |
# step=.01)
|
130 |
calculate_check = container_param.checkbox(label='Calculate', value=False)
|
131 |
if calculate_check:
|
132 |
-
emotion_frequency_slider, emotion_frequency=set_input(container_param,
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
rand_slider, rand=set_input(container_param,
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
else:
|
145 |
-
emotion_frequency='emotion_frequency'
|
146 |
-
rand=None
|
147 |
-
st.markdown(get_equation_text(w=w, ec=score, rand=rand,
|
|
|
148 |
fig = go.Figure()
|
149 |
# fig.add_trace(go.Scatter(x=xdata1, y=np.ones_like(xdata1)*rand,
|
150 |
# mode='markers', name='Random',
|
@@ -152,25 +159,27 @@ def run_pe(container_param):
|
|
152 |
# ))
|
153 |
if calculate_check:
|
154 |
dd = 0.01
|
155 |
-
fig.add_hline(y=rand, line_width=3,
|
156 |
-
|
|
|
|
|
157 |
fig.add_trace(go.Scatter(
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
),)
|
162 |
fig.add_trace(go.Scatter(
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
),)
|
167 |
fig.add_trace(go.Scatter(
|
168 |
x=xdata1, y=get_pe(w=w, f=xdata1, ec=score),
|
169 |
mode='lines',
|
170 |
name='Probability-Emote',
|
171 |
line=dict(color='#00eeee', width=4)
|
172 |
-
|
173 |
-
|
174 |
|
175 |
fig.update_layout(
|
176 |
template='plotly_dark',
|
@@ -181,4 +190,4 @@ def run_pe(container_param):
|
|
181 |
showlegend=False
|
182 |
)
|
183 |
st.plotly_chart(fig, use_container_width=True)
|
184 |
-
st.markdown(get_text())
|
|
|
1 |
+
import numpy as np
|
|
|
|
|
2 |
import plotly.graph_objects as go
|
3 |
+
import streamlit as st
|
4 |
|
|
|
|
|
5 |
|
6 |
@st.cache
|
7 |
def get_w(f, ec=0.86, rv=0.50):
|
|
|
10 |
print(f'w = {result}')
|
11 |
return result
|
12 |
|
13 |
+
|
14 |
@st.cache
|
15 |
def get_f(w, ec=0.86, rv=0.50):
|
16 |
result = 1+(ec*w-rv)/(1-w)
|
|
|
18 |
print(f'f = {result}')
|
19 |
return result
|
20 |
|
21 |
+
|
22 |
@st.cache
|
23 |
def get_pe(w, ec=0.86, f=0.50):
|
24 |
result = ec*w+(1-w)*(1-f)
|
|
|
26 |
print(f'f = {result}')
|
27 |
return result
|
28 |
|
29 |
+
|
30 |
xdata1 = np.arange(0, 1.1, step=0.01)
|
31 |
# rand = np.random.random_sample()
|
32 |
|
33 |
+
|
34 |
@st.cache
|
35 |
def proper_float(i):
|
36 |
return np.round(i, 2)
|
37 |
|
38 |
+
|
39 |
@st.cache
|
40 |
def get_text():
|
41 |
+
return '''
|
42 |
|
43 |
+
## Description
|
44 |
+
### Eric's proposal
|
45 |
+
> I would propose a scoring metric something like this:
|
46 |
+
> * `probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty`
|
47 |
+
>
|
48 |
+
> Where:
|
49 |
+
> * emotion_confidence: a score from 0.0 to 1.0 representing the emotion model’s confidence in it’s classification results
|
50 |
+
> * frequency_penalty: a score from 0.0 to 1.0 where a high score penalizes frequent emotes
|
51 |
+
> * `frequency_penalty = 1 - emotion_frequency`
|
52 |
+
> * w: a weight from 0.0 to 1.0 that controls the balance between emotion_confidence and frequency_penalty
|
53 |
+
> * Then you generate a random number between 0.0 and 1.0 and emote if it is greater than probability_emote
|
54 |
+
> * You will have to set frequency_penalty and w through trial and error, but you can start with setting w=0.5 and giving the emotion classifier and frequency penalty equal weight.
|
55 |
+
> * Setting w=1.0 would disable the frequency penalty altogether
|
56 |
'''
|
57 |
|
58 |
+
|
59 |
@st.cache
|
60 |
def get_equation_text(w=0.5, ec=0.7, rand=None, emotion_frequency=None):
|
61 |
+
text = f'''
|
62 |
#### Equation
|
63 |
* frequency_penalty = 1 - emotion_frequency
|
64 |
* probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty
|
65 |
* **probability_emote** = {proper_float(w)} * {proper_float(ec)} + {proper_float(1-w)} * frequency_penalty
|
66 |
|
67 |
+
'''
|
68 |
if rand is not None:
|
69 |
+
frequency_penalty = proper_float(1-emotion_frequency)
|
70 |
+
probability_emote = proper_float((w)*(ec)+(1-w)*frequency_penalty)
|
71 |
text = f'''
|
72 |
#### Equation
|
73 |
* frequency_penalty = 1 - emotion_frequency = 1 - {emotion_frequency} = {frequency_penalty}
|
|
|
79 |
* Random value = {rand}
|
80 |
* Show_Emotion = {probability_emote} > {rand}
|
81 |
* **Show_Emotion** = {probability_emote > rand}
|
82 |
+
'''
|
83 |
return text
|
84 |
|
85 |
+
|
86 |
+
def set_input(container_param,
|
87 |
+
label, key_slider, key_input,
|
88 |
+
min_value=0.,
|
89 |
+
max_value=1.,
|
90 |
+
value=.5,
|
91 |
+
step=.01,):
|
92 |
def slider2input():
|
93 |
st.session_state[key_input] = st.session_state[key_slider]
|
94 |
+
|
95 |
def input2slider():
|
96 |
st.session_state[key_slider] = st.session_state[key_input]
|
97 |
+
container_param = container_param.columns([1.1, 1])
|
98 |
slider_input = container_param[1].slider(
|
99 |
+
label=label,
|
100 |
min_value=min_value,
|
101 |
max_value=max_value,
|
102 |
value=value,
|
103 |
step=step,
|
104 |
+
key=key_slider,
|
105 |
on_change=slider2input)
|
106 |
+
number_input = container_param[0].number_input(
|
107 |
+
label='',
|
108 |
min_value=min_value,
|
109 |
max_value=max_value,
|
110 |
value=value,
|
111 |
step=step,
|
112 |
+
key=key_input,
|
113 |
on_change=input2slider)
|
114 |
return slider_input, number_input
|
115 |
+
|
116 |
+
|
117 |
+
def run_probability_emote(container_param):
|
118 |
+
w_slider, w = set_input(container_param,
|
119 |
+
label='Weight w', key_slider='w_slider', key_input='w_input',
|
120 |
+
min_value=0.,
|
121 |
+
max_value=1.,
|
122 |
+
value=.5,
|
123 |
+
step=.01,)
|
124 |
+
score_slider, score = set_input(container_param,
|
125 |
+
label='Confidence Score', key_slider='score_slider', key_input='score_input',
|
126 |
+
min_value=0.,
|
127 |
+
max_value=1.,
|
128 |
+
value=.5,
|
129 |
+
step=.01,)
|
130 |
# score = container_param.slider(
|
131 |
+
# label='Confidence Score',
|
132 |
# min_value=0.,
|
133 |
# max_value=1.,
|
134 |
# value=.5,
|
135 |
# step=.01)
|
136 |
calculate_check = container_param.checkbox(label='Calculate', value=False)
|
137 |
if calculate_check:
|
138 |
+
emotion_frequency_slider, emotion_frequency = set_input(container_param,
|
139 |
+
label='Emotion Frequency', key_slider='emotion_frequency_slider_slider', key_input='emotion_frequency_slider_input',
|
140 |
+
min_value=0.,
|
141 |
+
max_value=1.,
|
142 |
+
value=.5,
|
143 |
+
step=.01,)
|
144 |
+
rand_slider, rand = set_input(container_param,
|
145 |
+
label='Weight w', key_slider='rand_slider', key_input='rand_input',
|
146 |
+
min_value=0.,
|
147 |
+
max_value=1.,
|
148 |
+
value=.5,
|
149 |
+
step=.01,)
|
150 |
else:
|
151 |
+
emotion_frequency = 'emotion_frequency'
|
152 |
+
rand = None
|
153 |
+
st.markdown(get_equation_text(w=w, ec=score, rand=rand,
|
154 |
+
emotion_frequency=emotion_frequency))
|
155 |
fig = go.Figure()
|
156 |
# fig.add_trace(go.Scatter(x=xdata1, y=np.ones_like(xdata1)*rand,
|
157 |
# mode='markers', name='Random',
|
|
|
159 |
# ))
|
160 |
if calculate_check:
|
161 |
dd = 0.01
|
162 |
+
fig.add_hline(y=rand, line_width=3,
|
163 |
+
line_dash="dash", line_color="#ff8300")
|
164 |
+
fig.add_vline(x=emotion_frequency, line_width=3,
|
165 |
+
line_dash="dash", line_color="green")
|
166 |
fig.add_trace(go.Scatter(
|
167 |
+
x=[emotion_frequency-dd, emotion_frequency+dd], y=[rand-dd, rand+dd],
|
168 |
+
mode='lines',
|
169 |
+
line=dict(color='#ee00ee', width=8)
|
170 |
),)
|
171 |
fig.add_trace(go.Scatter(
|
172 |
+
x=[emotion_frequency+dd, emotion_frequency-dd], y=[rand-dd, rand+dd],
|
173 |
+
mode='lines',
|
174 |
+
line=dict(color='#ee00ee', width=8)
|
175 |
),)
|
176 |
fig.add_trace(go.Scatter(
|
177 |
x=xdata1, y=get_pe(w=w, f=xdata1, ec=score),
|
178 |
mode='lines',
|
179 |
name='Probability-Emote',
|
180 |
line=dict(color='#00eeee', width=4)
|
181 |
+
),
|
182 |
+
)
|
183 |
|
184 |
fig.update_layout(
|
185 |
template='plotly_dark',
|
|
|
190 |
showlegend=False
|
191 |
)
|
192 |
st.plotly_chart(fig, use_container_width=True)
|
193 |
+
st.markdown(get_text())
|
src/story_gen.py
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
|
|
|
|
|
2 |
import sys
|
3 |
import time
|
4 |
|
5 |
-
import printj
|
6 |
-
from transformers import pipeline # , set_seed
|
7 |
import numpy as np
|
8 |
import pandas as pd
|
9 |
-
|
10 |
-
import re
|
11 |
import streamlit as st
|
|
|
12 |
|
13 |
|
14 |
class StoryGenerator:
|
@@ -83,18 +83,17 @@ class StoryGenerator:
|
|
83 |
# print(story_to_print)
|
84 |
# printj.purple(f'Emotion: {emotion}')
|
85 |
return story_till_now, emotion
|
86 |
-
|
87 |
def next_sentence(self,
|
88 |
-
|
89 |
-
|
90 |
last_length = len(story_till_now)
|
91 |
genreate_robot_sentence = self.generator(story_till_now, max_length=self.get_num_token(story_till_now) +
|
92 |
-
|
93 |
story_till_now = genreate_robot_sentence[0]['generated_text']
|
94 |
new_sentence = story_till_now[last_length:]
|
95 |
emotion = self.get_emotion(new_sentence)
|
96 |
return story_till_now, emotion, new_sentence
|
97 |
-
|
98 |
|
99 |
def auto_ist(self,
|
100 |
story_till_now="Hello, I'm a language model,",
|
|
|
1 |
|
2 |
+
# import nltk
|
3 |
+
import re
|
4 |
import sys
|
5 |
import time
|
6 |
|
|
|
|
|
7 |
import numpy as np
|
8 |
import pandas as pd
|
9 |
+
import printj
|
|
|
10 |
import streamlit as st
|
11 |
+
from transformers import pipeline # , set_seed
|
12 |
|
13 |
|
14 |
class StoryGenerator:
|
|
|
83 |
# print(story_to_print)
|
84 |
# printj.purple(f'Emotion: {emotion}')
|
85 |
return story_till_now, emotion
|
86 |
+
|
87 |
def next_sentence(self,
|
88 |
+
story_till_now="Hello, I'm a language model,",
|
89 |
+
length=10):
|
90 |
last_length = len(story_till_now)
|
91 |
genreate_robot_sentence = self.generator(story_till_now, max_length=self.get_num_token(story_till_now) +
|
92 |
+
length, num_return_sequences=1)
|
93 |
story_till_now = genreate_robot_sentence[0]['generated_text']
|
94 |
new_sentence = story_till_now[last_length:]
|
95 |
emotion = self.get_emotion(new_sentence)
|
96 |
return story_till_now, emotion, new_sentence
|
|
|
97 |
|
98 |
def auto_ist(self,
|
99 |
story_till_now="Hello, I'm a language model,",
|