jitesh commited on
Commit
bf902a7
1 Parent(s): 1115bb5

adds all emotions

Browse files
data/df/ist_logs.txt CHANGED
The diff for this file is too large to render. See raw diff
 
src/probability_emote.py CHANGED
@@ -7,7 +7,7 @@ from .lib import set_input
7
  def get_w(f, ec=0.86, rv=0.50):
8
  result = (rv + f-1)/(ec + f-1)
9
  result = np.clip(result, 0, 1)
10
- print(f'w = {result}')
11
  return result
12
 
13
 
@@ -15,7 +15,7 @@ def get_w(f, ec=0.86, rv=0.50):
15
  def get_f(w, ec=0.86, rv=0.50):
16
  result = 1+(ec*w-rv)/(1-w)
17
  result = np.clip(result, 0, 1)
18
- print(f'f = {result}')
19
  return result
20
 
21
 
@@ -23,7 +23,7 @@ def get_f(w, ec=0.86, rv=0.50):
23
  def get_pe(w, ec=0.86, f=0.50):
24
  result = ec*w+(1-w)*(1-f)
25
  result = np.clip(result, 0, 1)
26
- print(f'f = {result}')
27
  return result
28
 
29
 
 
7
  def get_w(f, ec=0.86, rv=0.50):
8
  result = (rv + f-1)/(ec + f-1)
9
  result = np.clip(result, 0, 1)
10
+ # print(f'w = {result}')
11
  return result
12
 
13
 
 
15
  def get_f(w, ec=0.86, rv=0.50):
16
  result = 1+(ec*w-rv)/(1-w)
17
  result = np.clip(result, 0, 1)
18
+ # print(f'f = {result}')
19
  return result
20
 
21
 
 
23
  def get_pe(w, ec=0.86, f=0.50):
24
  result = ec*w+(1-w)*(1-f)
25
  result = np.clip(result, 0, 1)
26
+ # print(f'f = {result}')
27
  return result
28
 
29
 
src/read_logs.py CHANGED
@@ -44,10 +44,10 @@ class LogAnalyser:
44
  # self.container_param.markdown(
45
  # f'st.session_state.button1_counter: {st.session_state.button1_counter}')
46
 
47
- if exists(self.df_path):
48
- self.df = pd.read_csv(self.df_path)
49
- else:
50
  self.df = self.get_log()
 
 
51
 
52
  # if 'path' not in st.session_state:
53
  # st.session_state.path=self.path
@@ -80,6 +80,7 @@ class LogAnalyser:
80
  step=.01,)
81
  table_mode = self.container_param.radio(
82
  "Table Style:", ["Dataframe", "Table"])
 
83
  self.show_pe_data = self.container_param.checkbox(
84
  'Show Probability Emote', value=True, key='show_pe_data_log')
85
  self.score_threshold = set_input(self.container_param,
@@ -145,16 +146,18 @@ class LogAnalyser:
145
  dfs.drop(c, axis=1, inplace=True)
146
 
147
  st.markdown(f'#### Story {story_id}')
148
- dfs = dfs.style
149
- dfs = dfs.hide_index()
150
- if self.show_pe_data:
151
- dfs = dfs.apply(self.dfstyle_color_text_col, axis=1)
152
- # dfs = dfs.applymap(self.dfstyle_color_text)
153
- dfs = dfs.apply(self.rower, axis=None)
154
- dfs = dfs.set_table_styles([{
155
- 'selector': 'tr:hover',
156
- 'props': 'color: #000000' # background-color: #eeee66;font-size: 1.01em;
157
- }]) # .hide_index()
 
 
158
 
159
  if table_mode == 'Dataframe':
160
  st.dataframe(dfs)
@@ -177,14 +180,14 @@ class LogAnalyser:
177
  ['color: #333333']+['color: #fcfcfc']*3
178
  if s.Score < self.score_threshold and s.Turn == 'user':
179
  result[3:-1] = ['color: #992222'] + ['color: #fcfcfc']*3
180
- printj.red(result)
181
  if s.reaction_show == 1:
182
  result[-1] = 'color: #222222'
183
  elif s.reaction_show == 0:
184
  result[-1] = 'color: #222222'
185
  else:
186
- print(s.reaction_show)
187
- print(type(s.reaction_show))
188
  result[4:] = ['color: #fcfcfc']*4
189
  # if s.probability_emote!=s.probability_emote:
190
  # result[5] = 'color: #eeeeee'
@@ -230,9 +233,21 @@ class LogAnalyser:
230
  continue
231
  log_dict['Sentence'] = line[3:]
232
  log_dict['Story'] = story_num
233
- emotion = self.gen.get_emotion(log_dict['Sentence'])
234
- log_dict['Emotion'] = emotion['label']
235
- log_dict['Score'] = emotion['score']
 
 
 
 
 
 
 
 
 
 
 
 
236
  df = pd.concat(
237
  [df, pd.DataFrame(log_dict, index=[f'idx_{i}'])])
238
  df = df.reset_index(drop=True)
@@ -255,4 +270,4 @@ if __name__ == '__main__':
255
 
256
  # Create DataFrame
257
  df = pd.DataFrame(data)
258
- print(df, type(df))
 
44
  # self.container_param.markdown(
45
  # f'st.session_state.button1_counter: {st.session_state.button1_counter}')
46
 
47
+ if not exists(self.df_path) or self.container_button.button('Detect Emotion'):
 
 
48
  self.df = self.get_log()
49
+ # else:
50
+ self.df = pd.read_csv(self.df_path)
51
 
52
  # if 'path' not in st.session_state:
53
  # st.session_state.path=self.path
 
80
  step=.01,)
81
  table_mode = self.container_param.radio(
82
  "Table Style:", ["Dataframe", "Table"])
83
+ self.emotion_type = self.container_param.select_slider('Emotion', ['Max-only', '2', '3', '4', '5', '6', 'All 7'])
84
  self.show_pe_data = self.container_param.checkbox(
85
  'Show Probability Emote', value=True, key='show_pe_data_log')
86
  self.score_threshold = set_input(self.container_param,
 
146
  dfs.drop(c, axis=1, inplace=True)
147
 
148
  st.markdown(f'#### Story {story_id}')
149
+
150
+
151
+ # dfs = dfs.style
152
+ # dfs = dfs.hide_index()
153
+ # if self.show_pe_data:
154
+ # dfs = dfs.apply(self.dfstyle_color_text_col, axis=1)
155
+ # # dfs = dfs.applymap(self.dfstyle_color_text)
156
+ # dfs = dfs.apply(self.rower, axis=None)
157
+ # dfs = dfs.set_table_styles([{
158
+ # 'selector': 'tr:hover',
159
+ # 'props': 'color: #000000' # background-color: #eeee66;font-size: 1.01em;
160
+ # }]) # .hide_index()
161
 
162
  if table_mode == 'Dataframe':
163
  st.dataframe(dfs)
 
180
  ['color: #333333']+['color: #fcfcfc']*3
181
  if s.Score < self.score_threshold and s.Turn == 'user':
182
  result[3:-1] = ['color: #992222'] + ['color: #fcfcfc']*3
183
+ # printj.red(result)
184
  if s.reaction_show == 1:
185
  result[-1] = 'color: #222222'
186
  elif s.reaction_show == 0:
187
  result[-1] = 'color: #222222'
188
  else:
189
+ # print(s.reaction_show)
190
+ # print(type(s.reaction_show))
191
  result[4:] = ['color: #fcfcfc']*4
192
  # if s.probability_emote!=s.probability_emote:
193
  # result[5] = 'color: #eeeeee'
 
233
  continue
234
  log_dict['Sentence'] = line[3:]
235
  log_dict['Story'] = story_num
236
+ emotion_type = 'sorted' # 'max'
237
+ if self.emotion_type == 'max':
238
+ emotion_type = 'max'
239
+ else:
240
+ emotion_type = 'sorted' #
241
+ emotion = self.gen.get_emotion(log_dict['Sentence'], filter_by=emotion_type)
242
+ if emotion_type =='max':
243
+ log_dict['Emotion'] = emotion['label']
244
+ log_dict['Score'] = emotion['score']
245
+ elif emotion_type =='sorted':
246
+ log_dict['Emotion'] = emotion[0]['label']
247
+ log_dict['Score'] = emotion[0]['score']
248
+ for sorted_i in range(1, len(emotion)):
249
+ log_dict[f'Emotion_{sorted_i+1}'] = emotion[sorted_i]['label']
250
+ log_dict[f'Score_{sorted_i+1}'] = emotion[sorted_i]['score']
251
  df = pd.concat(
252
  [df, pd.DataFrame(log_dict, index=[f'idx_{i}'])])
253
  df = df.reset_index(drop=True)
 
270
 
271
  # Create DataFrame
272
  df = pd.DataFrame(data)
273
+ # print(df, type(df))
src/story_gen.py CHANGED
@@ -52,11 +52,15 @@ class StoryGenerator:
52
  def clear_stats(self):
53
  self.stats_df = pd.DataFrame(data=[], columns=[])
54
 
55
- def get_emotion(self, text):
56
  emotions = self.classifier(text)
57
- emotion = max(emotions[0], key=lambda x: x['score'])
58
- return emotion
 
 
 
59
 
 
60
  @staticmethod
61
  def get_num_token(text):
62
  # return len(nltk.word_tokenize(text))
@@ -117,14 +121,14 @@ class StoryGenerator:
117
  # Text generation for User
118
  last_length = len(story_till_now)
119
  printj.cyan(story_till_now)
120
- printj.red.bold_on_white(
121
- f'loop: {i}; generate user text; length: {last_length}')
122
  genreate_user_sentence = self.generator(story_till_now, max_length=self.get_num_token(
123
  story_till_now)+length, num_return_sequences=1)
124
  story_till_now = genreate_user_sentence[0]['generated_text']
125
  new_sentence_user = story_till_now[last_length:]
126
 
127
- printj.red.bold_on_white(f'loop: {i}; check emotion')
128
  # Emotion self.classifier for User
129
  emotion_user = self.get_emotion(new_sentence_user)
130
  if emotion_user['label'] == 'neutral':
@@ -156,8 +160,8 @@ class StoryGenerator:
156
  # Text generation for Robot
157
  last_length = len(story_till_now)
158
  printj.cyan(story_till_now)
159
- printj.red.bold_on_white(
160
- f'loop: {i}; generate robot text; length: {last_length}')
161
  genreate_robot_sentence = self.generator(story_till_now, max_length=self.get_num_token(
162
  story_till_now)+length, num_return_sequences=1)
163
  story_till_now = genreate_robot_sentence[0]['generated_text']
@@ -211,7 +215,7 @@ class StoryGenerator:
211
  self.stories.append(_story_till_now)
212
  self.data.append(story_data)
213
  self.stats_df = self.stats_df.reset_index(drop=True)
214
- print(self.stats_df)
215
 
216
  def save_stats(self, path='pandas_simple.xlsx'):
217
  writer = pd.ExcelWriter(path, engine='xlsxwriter')
 
52
  def clear_stats(self):
53
  self.stats_df = pd.DataFrame(data=[], columns=[])
54
 
55
+ def get_emotion(self, text, filter_by='max'):
56
  emotions = self.classifier(text)
57
+ if filter_by=='max':
58
+ emotion = max(emotions[0], key=lambda x: x['score'])
59
+ return emotion
60
+ elif filter_by=='sorted':
61
+ return sorted(emotions[0], key=lambda x: x['score'], reverse=True)
62
 
63
+
64
  @staticmethod
65
  def get_num_token(text):
66
  # return len(nltk.word_tokenize(text))
 
121
  # Text generation for User
122
  last_length = len(story_till_now)
123
  printj.cyan(story_till_now)
124
+ # printj.red.bold_on_white(
125
+ # f'loop: {i}; generate user text; length: {last_length}')
126
  genreate_user_sentence = self.generator(story_till_now, max_length=self.get_num_token(
127
  story_till_now)+length, num_return_sequences=1)
128
  story_till_now = genreate_user_sentence[0]['generated_text']
129
  new_sentence_user = story_till_now[last_length:]
130
 
131
+ # printj.red.bold_on_white(f'loop: {i}; check emotion')
132
  # Emotion self.classifier for User
133
  emotion_user = self.get_emotion(new_sentence_user)
134
  if emotion_user['label'] == 'neutral':
 
160
  # Text generation for Robot
161
  last_length = len(story_till_now)
162
  printj.cyan(story_till_now)
163
+ # printj.red.bold_on_white(
164
+ # f'loop: {i}; generate robot text; length: {last_length}')
165
  genreate_robot_sentence = self.generator(story_till_now, max_length=self.get_num_token(
166
  story_till_now)+length, num_return_sequences=1)
167
  story_till_now = genreate_robot_sentence[0]['generated_text']
 
215
  self.stories.append(_story_till_now)
216
  self.data.append(story_data)
217
  self.stats_df = self.stats_df.reset_index(drop=True)
218
+ # print(self.stats_df)
219
 
220
  def save_stats(self, path='pandas_simple.xlsx'):
221
  writer = pd.ExcelWriter(path, engine='xlsxwriter')
src/test.py CHANGED
@@ -49,3 +49,14 @@ iplot([{'x': x, 'y': 1-np.exp(-x)}])
49
 
50
  # df.style.apply(highlight_greaterthan_1, axis=1)
51
  # %%
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  # df.style.apply(highlight_greaterthan_1, axis=1)
51
  # %%
52
+ from transformers import pipeline
53
+ classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
54
+ emotion = classifier("the sentence")
55
+ # %%
56
+ emotion[0]
57
+ # %%
58
+ emotion[0][0]
59
+ # %%
60
+ sorted(emotion[0], key=lambda x: x['score'], reverse=True)
61
+
62
+ # %%