File size: 7,339 Bytes
644c96b
86f2d3a
0c756f9
be5bcd1
5c60a64
877913f
644c96b
86f2d3a
 
1061dba
86f2d3a
877913f
86f2d3a
 
 
 
 
bf1d936
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c73fdd
 
1061dba
86f2d3a
62a9f8b
86f2d3a
1061dba
0c73fdd
 
86f2d3a
 
 
 
 
 
c1fa828
86f2d3a
 
 
be5bcd1
86f2d3a
877913f
 
 
 
 
 
 
 
 
 
 
bf1d936
877913f
1061dba
 
 
 
 
 
 
877913f
1061dba
 
 
0c756f9
1061dba
 
be5bcd1
c1fa828
1061dba
 
0c73fdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644c96b
0c73fdd
644c96b
0c73fdd
 
 
 
877913f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import streamlit as st
from story_gen import StoryGenerator
import plotly.figure_factory as ff
import plotly.express as px
import random
import numpy as np

st.set_page_config(page_title='Storytelling ' +
                   u'\U0001F5BC', page_icon=u'\U0001F5BC', layout="wide")
gen = StoryGenerator()
container_mode = st.sidebar.container()
container_guide = st.sidebar.container()
container_param = st.sidebar.container()
container_button = st.sidebar.container()
mode = container_mode.radio(
    "Select your mode",
    ('Create Statistics', 'Play Storytelling'), index=0)
choices_first_sentence = [
    'Custom',
    'Hello, I\'m a language model,',
    'So I suppose you want to ask me how I did it.',
    'I always wanted to be a giraffe - until that night.',
    'My first tutor was a dragon with a terrible sense of humor.',
    'Doctors told her she could never diet again.',
    'Memory is all around us, as well as within.',


]
cfs = st.selectbox('Choose First Sentence', choices_first_sentence)
if cfs == 'Custom':
    story_till_now = st.text_input(
        label='First Sentence', key='first_sentence')
else:
    st.session_state.first_sentence = cfs
    story_till_now = cfs
first_sentence = story_till_now
first_emotion = gen.get_emotion(first_sentence)

length = container_param.slider(label='Length of the generated sentence',
                                min_value=1, max_value=100, value=10, step=1)
if mode == 'Create Statistics':

    num_generation = container_param.slider(
        label='Number of generation', min_value=1, max_value=100, value=5, step=1)
    num_tests = container_param.slider(
        label='Number of tests', min_value=1, max_value=1000, value=3, step=1)
    reaction_weight_mode = container_param.select_slider(
        "Reaction Weight w:", ["Random", "Fixed"])
    if reaction_weight_mode == "Fixed":
        reaction_weight = container_param.slider(
            label='Reaction Weight w', min_value=0.0, max_value=1.0, value=0.5, step=0.01)
    elif reaction_weight_mode == "Random":
        reaction_weight = -1
    if container_button.button('Analyse'):
        gen.get_stats(story_till_now=story_till_now,
                      num_generation=num_generation, length=length, reaction_weight=reaction_weight, num_tests=num_tests)
        # if len(gen.stories) > 0:
        #     for si, story in enumerate(gen.stories):
        #         st.markdown(f'### Story no. {si}:', unsafe_allow_html=False)
        #         st.markdown(story, unsafe_allow_html=False)
        #     data=gen.stats_df[gen.stats_df.sentence_no==3]
        #     fig = px.violin(data_frame=data, x="reaction_weight", y="num_reactions", hover_data=data.columns)
        #     st.plotly_chart(fig, use_container_width=True)
        #     fig2 = px.box(data_frame=data, x="reaction_weight", y="num_reactions", hover_data=data.columns)
        #     st.plotly_chart(fig2, use_container_width=True)
        if len(gen.data) > 0:
            for si, story in enumerate(gen.data):
                st.markdown(f'### Story {si}:', unsafe_allow_html=False)
                for i, sentence in enumerate(story):
                    col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
                    col_turn.markdown(
                        sentence['turn'], unsafe_allow_html=False)
                    col_sentence.markdown(
                        sentence['sentence'], unsafe_allow_html=False)
                    col_emo.markdown(
                        f'{sentence["emotion"]} {np.round(sentence["confidence_score"], 3)}', unsafe_allow_html=False)
            st.table(data=gen.stats_df, )
            data = gen.stats_df[gen.stats_df.sentence_no == 3]
            fig = px.violin(data_frame=data, x="reaction_weight",
                            y="num_reactions", hover_data=data.columns)
            st.plotly_chart(fig, use_container_width=True)
            fig2 = px.box(data_frame=data, x="reaction_weight",
                          y="num_reactions", hover_data=data.columns)
            st.plotly_chart(fig2, use_container_width=True)
    else:
        container_guide.markdown(
            '### You selected statistics. Now set your parameters and click the `Analyse` button.')
elif mode == 'Play Storytelling':

    if 'sentence_list' not in st.session_state:
        st.session_state.sentence_list = [{'sentence': first_sentence,
                                           'emotion': first_emotion['label'],
                                           'score': first_emotion['score']}]
    if 'full_story' not in st.session_state:
        st.session_state.full_story = story_till_now
    # # , placeholder="Start writing your story...")
    # story_till_now = st.text_input(
    #     label='First Sentence', value='Hello, I\'m a language model,')

    # num_generation = st.sidebar.slider(
    #     label='Number of generation', min_value=1, max_value=100, value=10, step=1)
    # length = st.sidebar.slider(label='Length of the generated sentence',
    #                            min_value=1, max_value=100, value=20, step=1)
    container_button = container_button.columns([1, 1, 1])
    heading_container = st.container()
    col_turn, col_sentence, col_emo = st.columns([1, 8, 2])
    if container_button[0].button('Run'):
        heading_container.markdown(f'### Story')

        # st.text(story_till_now)
        full_story, emotion, new_sentence = gen.next_sentence(
            st.session_state.full_story, length)
        st.session_state.full_story = story_till_now
        st.session_state.sentence_list.append({
            'sentence': new_sentence,
            'emotion': emotion["label"],
            'score': emotion["score"]})
        # col_sentence.markdown(st.session_state.sentence_list)
        for step in st.session_state.sentence_list:
            col_sentence.markdown(step['sentence'])
            col_emo.markdown(
                f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
        # i=0
        # while True:
        #     story_till_now, emotion, new_sentence = gen.next_sentence(
        #         story_till_now, length)
        #     col_sentence.text(new_sentence)
        #     col_emo.markdown(f'{emotion["label"]} {np.round(emotion["score"], 3)}', unsafe_allow_html=False)
        #     # col_emo.markdown(f'The last sentence has the "{emotion["label"]}" **Emotion** with a confidence score of {emotion["score"]}.')
        #     new_input_sentence = st.text_input(label='Next Sentence', key=f'next_sentence_{i}')
        #     story_till_now += ' ' + new_input_sentence

        #     i+=1

    else:
        step = st.session_state.sentence_list[0]
        col_sentence.markdown(step['sentence'])
        col_emo.markdown(
            f'{step["emotion"]} {np.round(step["score"], 3)}', unsafe_allow_html=False)
        st.markdown(
            '### Write the first sentence and then hit the `Run` button')

    if container_button[2].button('Clear'):

        st.session_state.full_story = first_sentence
        st.session_state.sentence_list = [{'sentence': first_sentence,
                                           'emotion': first_emotion['label'],
                                           'score': first_emotion['score']}]
# elif mode == 'Analyse Emotions':
#     container_mode.write('Let\'s play storytelling.')