File size: 2,838 Bytes
20684dc
 
 
 
 
 
1115bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20684dc
bfa0c67
1115bb5
 
20684dc
bfa0c67
20684dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfa0c67
1115bb5
 
 
20684dc
9372e1c
 
bfa0c67
9372e1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1115bb5
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
import random

import streamlit as st

from src import StoryGenerator

import xlsxwriter
import pandas as pd
import io


def create_dowload_button(data, sheet_name='AllData', label="Download data", file_name='data.xlsx'):

    buffer = io.BytesIO()
    with pd.ExcelWriter(buffer, engine='xlsxwriter') as writer:
        # Write each dataframe to a different worksheet.
        data.to_excel(writer, sheet_name=sheet_name)

        # Close the Pandas Excel writer and output the Excel file to the buffer
        writer.save()
        st.download_button(
            label=label,
            data=buffer,
            file_name=file_name,
            mime='application/vnd.ms-excel',
        )

# @st.cache(allow_output_mutation=True)


def initialise_storytelling(gen, container_guide, container_param, container_button):
    gen.initialise_models()
    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 = set_input(container_param,
                       label='Length of the sentence',
                       min_value=1, max_value=100, value=10, step=1,
                       key_slider='length_slider', key_input='length_input',)
    return first_sentence, first_emotion, length


# @st.cache(allow_output_mutation=True)
def set_input(container_param,
              label, key_slider, key_input,
              min_value=0.,
              max_value=1.,
              value=.5,
              step=.01,):
    def slider2input():
        st.session_state[key_input] = st.session_state[key_slider]

    def input2slider():
        st.session_state[key_slider] = st.session_state[key_input]
    container_param = container_param.columns([1.1, 1])
    number_input = container_param[0].number_input(
        label=label,
        min_value=min_value,
        max_value=max_value,
        value=value,
        step=step,
        key=key_input,
        on_change=input2slider)
    slider_input = container_param[1].slider(
        label='',
        min_value=min_value,
        max_value=max_value,
        value=value,
        step=step,
        key=key_slider,
        on_change=slider2input)
    return number_input