Spaces:
Sleeping
Sleeping
File size: 5,250 Bytes
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
import gradio as gr
import json
import os
from typing import Dict, List, Union
import src.agents.coordinator as C
from src.frontend.chatbot import (
MAX_OPTIONS,
chatbot,
init_graph,
on_chatbot_response,
option_buttons,
restart_button
)
from src.frontend import sidebar
from src.utils.utils import (
get_session_dir,
save_information,
transform_story_name
)
story_name_textbox = gr.Text(
placeholder='Give your adventure a unique name!',
label='Story Name',
interactive=True,
key='story_name_textbox',
preserved_by_key='key',
visible=False
)
story_context_textbox = gr.Text(
placeholder='What kind of story would you like to adventure on?',
label='Story Context',
interactive=True,
key='story_context_textbox',
preserved_by_key='key',
visible=False
)
categories_context_textbox = gr.Text(
placeholder='What is the theme of the categories you want to have?',
label='Categories Context',
interactive=True,
key='categories_context_textbox',
preserved_by_key='key',
visible=False
)
num_questions_slider = gr.Slider(
minimum=1,
maximum=10,
value=5,
step=1,
label='How many questions would you want your journey to have?',
interactive=True,
visible=False,
key='num_questions_slider',
preserved_by_key='key'
)
num_options_slider = gr.Slider(
minimum=2,
maximum=4,
value=2,
step=1,
label='How many options would you like at each turn?',
interactive=True,
visible=False,
key='num_options_slider',
preserved_by_key='key'
)
num_categories_slider = gr.Slider(
minimum=2,
maximum=MAX_OPTIONS,
value=2,
step=1,
label='How many categories / endings would you like to have?',
interactive=True,
visible=False,
key='num_categories_slider',
preserved_by_key='key'
)
story_information_submit_button = gr.Button(
visible=False,
interactive=False,
key='story_information_submit_button',
preserved_by_key='key'
)
def get_widgets() -> List[Union[gr.Text, gr.Slider, gr.Button]]:
return [
story_name_textbox,
story_context_textbox,
categories_context_textbox,
num_questions_slider,
num_options_slider,
num_categories_slider,
story_information_submit_button
]
def get_widgets_updates(
is_visible: bool
) -> Dict[Union[gr.Slider, gr.Text, gr.Button], gr.update]:
return {
widget: gr.update(visible=is_visible)
for widget in get_widgets()
}
def check_story_name(story_name: str) -> bool:
story_name_dir = transform_story_name(story_name)
return not story_name_dir in os.listdir(get_session_dir())
def on_text_change(
story_name: str,
story_context: str,
categories_context: str
) -> gr.update:
if all([story_name, story_context, categories_context]) and \
check_story_name(story_name):
return gr.update(interactive=True)
return gr.update(interactive=False)
def save_story_information(
story_name: str,
story_context: str,
categories_context: str,
num_questions: int,
num_options: int,
num_categories: int
):
story_name_dir = transform_story_name(story_name)
story_dirpath = os.path.join(get_session_dir(), story_name_dir)
os.mkdir(story_dirpath)
story_information_filepath = os.path.join(story_dirpath, 'story.json')
story_information = {
'story_name': story_name,
'story_context': story_context,
'categories_context': categories_context,
'num_questions': num_questions,
'num_options': num_options,
'num_categories': num_categories
}
save_information(story_information, story_information_filepath)
def on_submit(
story_name: str,
story_context: str,
categories_context: str,
num_questions: int,
num_options: int,
num_categories: int
) -> Dict[Union[gr.Text, gr.Slider, gr.Button, gr.Chatbot], gr.update]:
save_story_information(
story_name,
story_context,
categories_context,
num_questions,
num_options,
num_categories
)
C.story_name = transform_story_name(story_name)
init_graph(
story_context,
categories_context,
num_questions,
num_options,
num_categories
)
chatbot_updates = on_chatbot_response([])
return chatbot_updates | get_widgets_updates(False) | sidebar.view_screen()
def render():
for widget in get_widgets():
widget.render()
if not isinstance(widget, gr.Text):
continue
widget.change(
fn=on_text_change,
inputs=[
story_name_textbox,
story_context_textbox,
categories_context_textbox
],
outputs=[story_information_submit_button]
)
story_information_submit_button.click(
fn=on_submit,
inputs=[
widget
for widget in get_widgets()
if not isinstance(widget, gr.Button)
],
outputs=get_widgets() + \
[chatbot, restart_button] + \
option_buttons + \
sidebar.get_widgets()
)
|