import streamlit as st from story_gen import StoryGenerator import plotly.figure_factory as ff import plotly.express as px import random gen = StoryGenerator() st.set_page_config(page_title='Storytelling ' + u'\U0001F5BC', page_icon=u'\U0001F5BC', layout="wide") if 'count' not in st.session_state or st.session_state.count == 6: st.session_state.count = 0 st.session_state.chat_history_ids = None st.session_state.old_response = '' else: st.session_state.count += 1 container_mode = 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) story_till_now = container_param.text_input( label='First Sentence', value=random.choice([ '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.', ])) num_generation = container_param.slider( label='Number of generation', min_value=1, max_value=100, value=3, step=1) 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_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) else: st.markdown('### You selected statistics. Now set your parameters and click the `Analyse` button.') elif mode == 'Play Storytelling': container_mode.write('Let\'s play storytelling.') # # , 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) if container_button.button('Run'): story_till_now, emotion = gen.story( story_till_now, num_generation, length) st.markdown(f'### Story') st.text(story_till_now) st.markdown(f'The last sentence has the "{emotion["label"]}" **Emotion** with a confidence score of {emotion["score"]}.') else: st.markdown('### Write the first sentence and then hit the `Run` button')