jitesh commited on
Commit
441ec1f
1 Parent(s): 121edbc

adds probability_emote page

Browse files
Files changed (5) hide show
  1. app.py +36 -28
  2. src/__init__.py +0 -0
  3. src/probability_emote.py +184 -0
  4. src/story_gen.py +221 -0
  5. src/story_gen_test.py +34 -0
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
- from lib.story_gen import StoryGenerator
 
3
  import plotly.express as px
4
  import random
5
  import numpy as np
@@ -11,36 +12,41 @@ container_mode = st.sidebar.container()
11
  container_guide = st.container()
12
  container_param = st.sidebar.container()
13
  container_button = st.sidebar.container()
14
-
15
  mode = container_mode.radio(
16
  "Select a mode",
17
  ('Probability Emote', 'Create Statistics', 'Play Storytelling'), index=0)
18
 
19
- choices_first_sentence = [
20
- 'Custom',
21
- 'Hello, I\'m a language model,',
22
- 'So I suppose you want to ask me how I did it.',
23
- 'I always wanted to be a giraffe - until that night.',
24
- 'My first tutor was a dragon with a terrible sense of humor.',
25
- 'Doctors told her she could never diet again.',
26
- 'Memory is all around us, as well as within.',
27
 
 
 
 
 
 
 
 
 
 
28
 
29
- ]
30
- cfs = st.selectbox('Choose First Sentence', choices_first_sentence)
31
- if cfs == 'Custom':
32
- story_till_now = st.text_input(
33
- label='First Sentence', key='first_sentence')
34
- else:
35
- st.session_state.first_sentence = cfs
36
- story_till_now = cfs
37
- first_sentence = story_till_now
38
- first_emotion = gen.get_emotion(first_sentence)
39
 
40
- length = container_param.slider(label='Length of the generated sentence',
41
- min_value=1, max_value=100, value=10, step=1)
42
- if mode == 'Create Statistics':
 
 
 
 
 
 
 
 
 
 
 
43
 
 
 
 
 
44
  num_generation = container_param.slider(
45
  label='Number of generation', min_value=1, max_value=100, value=5, step=1)
46
  num_tests = container_param.slider(
@@ -53,7 +59,7 @@ if mode == 'Create Statistics':
53
  elif reaction_weight_mode == "Random":
54
  reaction_weight = -1
55
  if container_button.button('Analyse'):
56
- gen.get_stats(story_till_now=story_till_now,
57
  num_generation=num_generation, length=length, reaction_weight=reaction_weight, num_tests=num_tests)
58
  # if len(gen.stories) > 0:
59
  # for si, story in enumerate(gen.stories):
@@ -87,13 +93,14 @@ if mode == 'Create Statistics':
87
  container_guide.markdown(
88
  '### You selected statistics. Now set your parameters and click the `Analyse` button.')
89
  elif mode == 'Play Storytelling':
90
-
 
91
  if 'sentence_list' not in st.session_state:
92
  st.session_state.sentence_list = [{'sentence': first_sentence,
93
  'emotion': first_emotion['label'],
94
  'score': first_emotion['score']}]
95
  if 'full_story' not in st.session_state:
96
- st.session_state.full_story = story_till_now
97
  container_button = container_button.columns([1, 1, 1])
98
  heading_container = st.container()
99
  col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
@@ -129,5 +136,6 @@ elif mode == 'Play Storytelling':
129
  st.session_state.sentence_list = [{'sentence': first_sentence,
130
  'emotion': first_emotion['label'],
131
  'score': first_emotion['score']}]
132
- # elif mode == 'Analyse Emotions':
133
- # container_mode.write('Let\'s play storytelling.')
 
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
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
+ first_sentence, first_emotion, length = initialise_storytelling()
49
+ # story_till_now = first_sentence
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(
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):
93
  container_guide.markdown(
94
  '### You selected statistics. Now set your parameters and click the `Analyse` button.')
95
  elif mode == 'Play Storytelling':
96
+ first_sentence, first_emotion, length = initialise_storytelling()
97
+ # story_till_now = first_sentence
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])
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
+ # container_mode.write('Let\'s play storytelling.')
141
+ run_pe(container_param)
src/__init__.py ADDED
File without changes
src/probability_emote.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
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):
11
+ result = (rv + f-1)/(ec + f-1)
12
+ result = np.clip(result, 0, 1)
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)
19
+ result = np.clip(result, 0, 1)
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)
26
+ result = np.clip(result, 0, 1)
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 = f'''
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}
71
+ * probability_emote = w * emotion_confidence + (1 - w) * frequency_penalty
72
+ * probability_emote = {proper_float(w)} * {proper_float(ec)} + {proper_float(1-w)} * {frequency_penalty}
73
+ * **probability_emote** = {probability_emote}
74
+ * Show_Emotion
75
+ = probability_emote > (Random value between 0 and 1)
76
+ * Random value = {rand}
77
+ * Show_Emotion = {probability_emote} > {rand}
78
+ * **Show_Emotion** = {probability_emote > rand}
79
+ '''
80
+ return text
81
+
82
+ def set_input(container_param,
83
+ label, key_slider, key_input,
84
+ min_value=0.,
85
+ max_value=1.,
86
+ value=.5,
87
+ step=.01,):
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 = key_slider,
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 = key_input,
108
+ on_change=input2slider)
109
+ return slider_input, number_input
110
+
111
+ def run_pe(container_param):
112
+ w_slider, w=set_input(container_param,
113
+ label='Weight w', key_slider='w_slider', key_input='w_input',
114
+ min_value=0.,
115
+ max_value=1.,
116
+ value=.5,
117
+ step=.01,)
118
+ score_slider, score=set_input(container_param,
119
+ label='Confidence Score', key_slider='score_slider', key_input='score_input',
120
+ min_value=0.,
121
+ max_value=1.,
122
+ value=.5,
123
+ step=.01,)
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
+ label='Emotion Frequency', key_slider='emotion_frequency_slider_slider', key_input='emotion_frequency_slider_input',
134
+ min_value=0.,
135
+ max_value=1.,
136
+ value=.5,
137
+ step=.01,)
138
+ rand_slider, rand=set_input(container_param,
139
+ label='Weight w', key_slider='rand_slider', key_input='rand_input',
140
+ min_value=0.,
141
+ max_value=1.,
142
+ value=.5,
143
+ step=.01,)
144
+ else:
145
+ emotion_frequency='emotion_frequency'
146
+ rand=None
147
+ st.markdown(get_equation_text(w=w, ec=score, rand=rand, emotion_frequency=emotion_frequency))
148
+ fig = go.Figure()
149
+ # fig.add_trace(go.Scatter(x=xdata1, y=np.ones_like(xdata1)*rand,
150
+ # mode='markers', name='Random',
151
+ # line=dict(color='#ff8300', width=2)
152
+ # ))
153
+ if calculate_check:
154
+ dd = 0.01
155
+ fig.add_hline(y=rand, line_width=3, line_dash="dash", line_color="#ff8300")
156
+ fig.add_vline(x=emotion_frequency, line_width=3, line_dash="dash", line_color="green")
157
+ fig.add_trace(go.Scatter(
158
+ x=[emotion_frequency-dd, emotion_frequency+dd], y=[rand-dd, rand+dd],
159
+ mode='lines',
160
+ line=dict(color='#ee00ee', width=8)
161
+ ),)
162
+ fig.add_trace(go.Scatter(
163
+ x=[emotion_frequency+dd, emotion_frequency-dd], y=[rand-dd, rand+dd],
164
+ mode='lines',
165
+ line=dict(color='#ee00ee', width=8)
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',
177
+ xaxis_range=[0., 1.],
178
+ yaxis_range=[0., 1.],
179
+ xaxis_title="Emotion Frequency",
180
+ yaxis_title="Probability Emote",
181
+ showlegend=False
182
+ )
183
+ st.plotly_chart(fig, use_container_width=True)
184
+ st.markdown(get_text())
src/story_gen.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # import nltk
10
+ import re
11
+ import streamlit as st
12
+
13
+
14
+ class StoryGenerator:
15
+ def __init__(self):
16
+ self.initialise_models()
17
+ self.stats_df = pd.DataFrame(data=[], columns=[])
18
+ self.stories = []
19
+ self.data = []
20
+
21
+ @staticmethod
22
+ @st.cache(allow_output_mutation=True)
23
+ def get_generator():
24
+ return pipeline('text-generation', model='gpt2')
25
+
26
+ @staticmethod
27
+ @st.cache(allow_output_mutation=True)
28
+ def get_classifier():
29
+ return pipeline("text-classification",
30
+ model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
31
+
32
+ def initialise_models(self):
33
+ # start = time.time()
34
+ self.generator = self.get_generator()
35
+ self.classifier = self.get_classifier()
36
+ # initialising_time = time.time()-start
37
+ # print(f'Initialising Time: {initialising_time}')
38
+ # set_seed(42)
39
+ # sys.exit()
40
+
41
+ def reset():
42
+ self.clear_stories()
43
+ self.clear_stats()
44
+
45
+ def clear_stories(self):
46
+ self.data = []
47
+ self.stories = []
48
+
49
+ def clear_stats(self):
50
+ self.stats_df = pd.DataFrame(data=[], columns=[])
51
+
52
+ def get_emotion(self, text):
53
+ emotions = self.classifier(text)
54
+ emotion = max(emotions[0], key=lambda x: x['score'])
55
+ return emotion
56
+
57
+ @staticmethod
58
+ def get_num_token(text):
59
+ # return len(nltk.word_tokenize(text))
60
+ return len(re.findall(r'\w+', text))
61
+
62
+ @staticmethod
63
+ def check_show_emotion(confidence_score, frequency, w):
64
+ frequency_penalty = 1 - frequency
65
+ probability_emote = w * confidence_score + (1-w) * frequency_penalty
66
+ return probability_emote > np.random.random_sample()
67
+
68
+ def story(self,
69
+ story_till_now="Hello, I'm a language model,",
70
+ num_generation=4,
71
+ length=10):
72
+ # last_length = 0
73
+
74
+ for i in range(num_generation):
75
+ last_length = len(story_till_now)
76
+ genreate_robot_sentence = self.generator(story_till_now, max_length=self.get_num_token(story_till_now) +
77
+ length, num_return_sequences=1)
78
+ story_till_now = genreate_robot_sentence[0]['generated_text']
79
+ new_sentence = story_till_now[last_length:]
80
+ emotion = self.get_emotion(new_sentence)
81
+ # printj.yellow(f'Sentence {i}:')
82
+ # story_to_print = f'{printj.ColorText.cyan(story_till_now[:last_length])}{printj.ColorText.green(story_till_now[last_length:])}\n'
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
+
99
+ def auto_ist(self,
100
+ story_till_now="Hello, I'm a language model,",
101
+ num_generation=4,
102
+ length=20, reaction_weight=0.5):
103
+ stats_df = pd.DataFrame(data=[], columns=[])
104
+ stats_dict = dict()
105
+ num_reactions = 0
106
+ reaction_frequency = 0
107
+ emotion = self.get_emotion(story_till_now) # first line emotion
108
+ story_data = [{
109
+ 'sentence': story_till_now,
110
+ 'turn': 'first',
111
+ 'emotion': emotion['label'],
112
+ 'confidence_score': emotion['score'],
113
+ }]
114
+ for i in range(num_generation):
115
+ # Text generation for User
116
+ last_length = len(story_till_now)
117
+ printj.cyan(story_till_now)
118
+ printj.red.bold_on_white(
119
+ f'loop: {i}; generate user text; length: {last_length}')
120
+ genreate_user_sentence = self.generator(story_till_now, max_length=self.get_num_token(
121
+ story_till_now)+length, num_return_sequences=1)
122
+ story_till_now = genreate_user_sentence[0]['generated_text']
123
+ new_sentence_user = story_till_now[last_length:]
124
+
125
+ printj.red.bold_on_white(f'loop: {i}; check emotion')
126
+ # Emotion self.classifier for User
127
+ emotion_user = self.get_emotion(new_sentence_user)
128
+ if emotion_user['label'] == 'neutral':
129
+ show_emotion_user = False
130
+ else:
131
+ reaction_frequency = num_reactions/(i+1)
132
+ show_emotion_user = self.check_show_emotion(
133
+ confidence_score=emotion_user['score'], frequency=reaction_frequency, w=reaction_weight)
134
+ if show_emotion_user:
135
+ num_reactions += 1
136
+
137
+ story_data.append({
138
+ 'sentence': new_sentence_user,
139
+ 'turn': 'user',
140
+ 'emotion': emotion_user['label'],
141
+ 'confidence_score': emotion_user['score'],
142
+ })
143
+ stats_dict['sentence_no'] = i
144
+ stats_dict['turn'] = 'user'
145
+ stats_dict['sentence'] = new_sentence_user
146
+ stats_dict['show_emotion'] = show_emotion_user
147
+ stats_dict['emotion_label'] = emotion_user['label']
148
+ stats_dict['emotion_score'] = emotion_user['score']
149
+ stats_dict['num_reactions'] = num_reactions
150
+ stats_dict['reaction_frequency'] = reaction_frequency
151
+ stats_dict['reaction_weight'] = reaction_weight
152
+ stats_df = pd.concat(
153
+ [stats_df, pd.DataFrame(stats_dict, index=[f'idx_{i}'])])
154
+ # Text generation for Robot
155
+ last_length = len(story_till_now)
156
+ printj.cyan(story_till_now)
157
+ printj.red.bold_on_white(
158
+ f'loop: {i}; generate robot text; length: {last_length}')
159
+ genreate_robot_sentence = self.generator(story_till_now, max_length=self.get_num_token(
160
+ story_till_now)+length, num_return_sequences=1)
161
+ story_till_now = genreate_robot_sentence[0]['generated_text']
162
+ new_sentence_robot = story_till_now[last_length:]
163
+ emotion_robot = self.get_emotion(new_sentence_robot)
164
+
165
+ story_data.append({
166
+ 'sentence': new_sentence_robot,
167
+ 'turn': 'robot',
168
+ 'emotion': emotion_robot['label'],
169
+ 'confidence_score': emotion_robot['score'],
170
+ })
171
+ stats_dict['sentence_no'] = i
172
+ stats_dict['turn'] = 'robot'
173
+ stats_dict['sentence'] = new_sentence_robot
174
+ stats_dict['show_emotion'] = None
175
+ stats_dict['emotion_label'] = emotion_robot['label']
176
+ stats_dict['emotion_score'] = emotion_robot['score']
177
+ stats_dict['num_reactions'] = None
178
+ stats_dict['reaction_frequency'] = None
179
+ stats_dict['reaction_weight'] = None
180
+ stats_df = pd.concat(
181
+ [stats_df, pd.DataFrame(stats_dict, index=[f'idx_{i}'])])
182
+
183
+ return stats_df, story_till_now, story_data
184
+
185
+ def get_stats(self,
186
+ story_till_now="Hello, I'm a language model,",
187
+ num_generation=4,
188
+ length=20, reaction_weight=-1, num_tests=2):
189
+ use_random_w = reaction_weight == -1
190
+ # self.stories = []
191
+ try:
192
+ num_rows = max(self.stats_df.story_id)+1
193
+ except Exception:
194
+ num_rows = 0
195
+ for story_id in range(num_tests):
196
+ if use_random_w:
197
+ # reaction_weight = np.random.random_sample()
198
+ reaction_weight = np.round(np.random.random_sample(), 1)
199
+ stats_df0, _story_till_now, story_data = self.auto_ist(
200
+ story_till_now=story_till_now,
201
+ num_generation=num_generation,
202
+ length=length, reaction_weight=reaction_weight)
203
+ stats_df0.insert(loc=0, column='story_id', value=story_id+num_rows)
204
+
205
+ # stats_df0['story_id'] = story_id
206
+ self.stats_df = pd.concat([self.stats_df, stats_df0])
207
+ printj.yellow(f'story_id: {story_id}')
208
+ printj.green(stats_df0)
209
+ self.stories.append(_story_till_now)
210
+ self.data.append(story_data)
211
+ self.stats_df = self.stats_df.reset_index(drop=True)
212
+ print(self.stats_df)
213
+
214
+ def save_stats(self, path='pandas_simple.xlsx'):
215
+ writer = pd.ExcelWriter(path, engine='xlsxwriter')
216
+
217
+ # Convert the dataframe to an XlsxWriter Excel object.
218
+ self.stats_df.to_excel(writer, sheet_name='IST')
219
+
220
+ # Close the Pandas Excel writer and output the Excel file.
221
+ writer.save()
src/story_gen_test.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%
2
+ import printj
3
+ from story_gen import StoryGenerator
4
+
5
+ gen = StoryGenerator()
6
+ # # %%
7
+ # story_till_now, emotion = gen.story(story_till_now='Hello, I\'m a language model,', num_generation=3, length=10)
8
+ # printj.purple(story_till_now)
9
+ # printj.yellow(emotion)
10
+
11
+
12
+ # %%
13
+ gen.get_stats(story_till_now="For myriad of eons i’ve forgotten who I really was, harvesting the essence of all existence.",
14
+ length=10, num_generation=3, num_tests=50)
15
+
16
+ # %%
17
+ gen.save_stats('/home/jitesh/haru/ist/results/a.xlsx')
18
+
19
+
20
+
21
+
22
+ # %%
23
+ data=gen.stats_df[gen.stats_df.sentence_no==3]
24
+ import seaborn as sns
25
+ sns.set_theme(style="whitegrid")
26
+ # ax = sns.violinplot(x="day", y="total_bill", data=tips)
27
+ ax = sns.violinplot(x="reaction_weight", y="num_reactions", data=data).set_title('Analysing ProbabilityEmote (Max reactions=3)')
28
+ # %%
29
+
30
+ gen.stats_df[gen.stats_df.sentence_no==3]
31
+ # %%
32
+ import re
33
+ len(re.findall(r'\w+', 'line ive '))
34
+ # %%