File size: 3,785 Bytes
b151e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ac7eb0
b151e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ac7eb0
b151e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import gradio as gr
import os

from typing import Dict, List, Tuple, Union

import src.agents.coordinator as C

from src.frontend.chatbot import (
    chatbot,
    init_graph,
    on_chatbot_response,
    option_buttons,
    restart_button
)
from src.frontend import sidebar
from src.utils.utils import load_information, transform_story_name

information_text = gr.Text(
    value='Select which story to continue below!',
    interactive=False,
    key='continue_story_user_information',
    preserved_by_key='key',
    visible=False,
    label=''
)
session_dir = os.path.join(
    os.path.dirname(__file__),
    '..', '..', 'sessions'
)
story_widgets: List[Tuple[gr.Row, gr.Button, gr.Text]] = []
story_dir_mapper = dict()
for i, dirname in enumerate(sorted(os.listdir(session_dir))):
    if dirname.startswith('.'):
        continue
    dirpath = os.path.join(session_dir, dirname)
    if not os.listdir(dirpath):
        continue
    story_information_filepath = os.path.join(dirpath, 'story.json')
    story_information_dict = load_information(story_information_filepath)
    story_name = story_information_dict['story_name']
    story_context = story_information_dict['story_context']
    row = gr.Row(
        visible=False,
        key=f'continue_row_{i}',
        preserved_by_key='key'
    )
    button = gr.Button(
        value=story_name,
        visible=False,
        key=f'continue_button_{i}',
        preserved_by_key='key',
        scale=1
    )
    text = gr.Text(
        value=story_context,
        label='',
        interactive=False,
        visible=False,
        key=f'continue_story_{i}',
        preserved_by_key='key',
        scale=3
    )
    story_widgets.append((row, button, text))
    story_dir_mapper[story_name] = dirpath


def get_widgets() -> List[Union[gr.Text, gr.Button, gr.Row]]:
    widgets = [information_text]
    for row, button, text in story_widgets:
        widgets.append(row)
        widgets.append(button)
        widgets.append(text)
    return widgets


def get_widgets_updates(
    is_visible: bool
) -> Dict[Union[gr.Slider, gr.Text, gr.Button], gr.update]:
    updates = dict()
    for widget in get_widgets():
        if isinstance(widget, gr.Row):
            updates[widget] = gr.Row(visible=is_visible)
        else:
            updates[widget] = gr.update(visible=is_visible)
    return updates


def on_button_click(
    story_name: str
) -> Dict[Union[gr.Text, gr.Slider, gr.Button, gr.Chatbot], gr.update]:
    
    dirpath = story_dir_mapper[story_name]
    story_name_ = transform_story_name(story_name)

    story_information_filepath = os.path.join(dirpath, 'story.json')
    story_information_dict = load_information(story_information_filepath)
    story_context = story_information_dict['story_context']
    categories_context = story_information_dict['categories_context']
    num_questions = story_information_dict['num_questions']
    num_options = story_information_dict['num_options']
    num_categories = story_information_dict['num_categories']

    init_graph(
        story_context,
        categories_context,
        num_questions,
        num_options,
        num_categories
    )

    C.story_name = story_name_
    C.load_coordinator()

    chatbot_updates = on_chatbot_response([])

    return chatbot_updates | get_widgets_updates(False) | sidebar.view_screen()


def render():
    information_text.render()

    for row, button, text in story_widgets:
        row.render()
        with row:
            button.render()
            text.render()
        button.click(
            fn=on_button_click,
            inputs=[button],
            outputs=get_widgets() + \
                [chatbot, restart_button] + \
                option_buttons + \
                sidebar.get_widgets()
        )