jitesh commited on
Commit
0c73fdd
1 Parent(s): 4fb52dd

updates "Play Storytelling" mode and adds next_sentence function

Browse files
Files changed (2) hide show
  1. app.py +63 -20
  2. story_gen.py +12 -0
app.py CHANGED
@@ -8,7 +8,6 @@ import numpy as np
8
  st.set_page_config(page_title='Storytelling ' +
9
  u'\U0001F5BC', page_icon=u'\U0001F5BC', layout="wide")
10
  gen = StoryGenerator()
11
-
12
  container_mode = st.sidebar.container()
13
  container_guide = st.sidebar.container()
14
  container_param = st.sidebar.container()
@@ -34,14 +33,15 @@ if cfs == 'Custom':
34
  else:
35
  st.session_state.first_sentence = cfs
36
  story_till_now = cfs
 
 
37
 
38
-
39
- num_generation = container_param.slider(
40
- label='Number of generation', min_value=1, max_value=100, value=5, step=1)
41
  length = container_param.slider(label='Length of the generated sentence',
42
  min_value=1, max_value=100, value=10, step=1)
43
  if mode == 'Create Statistics':
44
 
 
 
45
  num_tests = container_param.slider(
46
  label='Number of tests', min_value=1, max_value=1000, value=3, step=1)
47
  reaction_weight_mode = container_param.select_slider(
@@ -85,23 +85,66 @@ if mode == 'Create Statistics':
85
  else:
86
  container_guide.markdown(
87
  '### You selected statistics. Now set your parameters and click the `Analyse` button.')
88
- # elif mode == 'Play Storytelling':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- # # # , placeholder="Start writing your story...")
91
- # # story_till_now = st.text_input(
92
- # # label='First Sentence', value='Hello, I\'m a language model,')
93
 
94
- # # num_generation = st.sidebar.slider(
95
- # # label='Number of generation', min_value=1, max_value=100, value=10, step=1)
96
- # # length = st.sidebar.slider(label='Length of the generated sentence',
97
- # # min_value=1, max_value=100, value=20, step=1)
98
- # if container_button.button('Run'):
99
- # story_till_now, emotion = gen.story(
100
- # story_till_now, num_generation, length)
101
- # st.markdown(f'### Story')
102
- # st.text(story_till_now)
103
- # st.markdown(f'The last sentence has the "{emotion["label"]}" **Emotion** with a confidence score of {emotion["score"]}.')
104
- # else:
105
- # container_guide.markdown('### Write the first sentence and then hit the `Run` button')
106
  # elif mode == 'Analyse Emotions':
107
  # container_mode.write('Let\'s play storytelling.')
 
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.sidebar.container()
13
  container_param = st.sidebar.container()
 
33
  else:
34
  st.session_state.first_sentence = cfs
35
  story_till_now = cfs
36
+ first_sentence = story_till_now
37
+ first_emotion = gen.get_emotion(first_sentence)
38
 
 
 
 
39
  length = container_param.slider(label='Length of the generated sentence',
40
  min_value=1, max_value=100, value=10, step=1)
41
  if mode == 'Create Statistics':
42
 
43
+ num_generation = container_param.slider(
44
+ label='Number of generation', min_value=1, max_value=100, value=5, step=1)
45
  num_tests = container_param.slider(
46
  label='Number of tests', min_value=1, max_value=1000, value=3, step=1)
47
  reaction_weight_mode = container_param.select_slider(
 
85
  else:
86
  container_guide.markdown(
87
  '### You selected statistics. Now set your parameters and click the `Analyse` button.')
88
+ elif mode == 'Play Storytelling':
89
+
90
+ if 'sentence_list' not in st.session_state:
91
+ st.session_state.sentence_list = [{'sentence': first_sentence,
92
+ 'emotion': first_emotion['label'],
93
+ 'score': first_emotion['score']}]
94
+ if 'full_story' not in st.session_state:
95
+ st.session_state.full_story = story_till_now
96
+ # # , placeholder="Start writing your story...")
97
+ # story_till_now = st.text_input(
98
+ # label='First Sentence', value='Hello, I\'m a language model,')
99
+
100
+ # num_generation = st.sidebar.slider(
101
+ # label='Number of generation', min_value=1, max_value=100, value=10, step=1)
102
+ # length = st.sidebar.slider(label='Length of the generated sentence',
103
+ # min_value=1, max_value=100, value=20, step=1)
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 = story_till_now
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_sentence.markdown(step['sentence'])
121
+ col_emo.markdown(
122
+ f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
123
+ # i=0
124
+ # while True:
125
+ # story_till_now, emotion, new_sentence = gen.next_sentence(
126
+ # story_till_now, length)
127
+ # col_sentence.text(new_sentence)
128
+ # col_emo.markdown(f'{emotion["label"]} {np.round(emotion["score"], 3)}', unsafe_allow_html=False)
129
+ # # col_emo.markdown(f'The last sentence has the "{emotion["label"]}" **Emotion** with a confidence score of {emotion["score"]}.')
130
+ # new_input_sentence = st.text_input(label='Next Sentence', key=f'next_sentence_{i}')
131
+ # story_till_now += ' ' + new_input_sentence
132
+
133
+ # i+=1
134
+
135
+ else:
136
+ step = st.session_state.sentence_list[0]
137
+ col_sentence.markdown(step['sentence'])
138
+ col_emo.markdown(
139
+ f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
140
+ st.markdown(
141
+ '### Write the first sentence and then hit the `Run` button')
142
 
143
+ if container_button[2].button('Clear'):
 
 
144
 
145
+ st.session_state.full_story = first_sentence
146
+ st.session_state.sentence_list = [{'sentence': first_sentence,
147
+ 'emotion': first_emotion['label'],
148
+ 'score': first_emotion['score']}]
 
 
 
 
 
 
 
 
149
  # elif mode == 'Analyse Emotions':
150
  # container_mode.write('Let\'s play storytelling.')
story_gen.py CHANGED
@@ -83,6 +83,18 @@ class StoryGenerator:
83
  # print(story_to_print)
84
  # printj.purple(f'Emotion: {emotion}')
85
  return story_till_now, emotion
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  def auto_ist(self,
88
  story_till_now="Hello, I'm a language model,",
 
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,",