Capturing state of variables
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from
|
2 |
|
3 |
|
4 |
import gradio as gr
|
@@ -8,19 +8,9 @@ MAX_INPUTS = 10
|
|
8 |
MAX_TASKS = 50
|
9 |
|
10 |
|
11 |
-
class
|
12 |
-
def
|
13 |
-
|
14 |
-
self.visible = visible
|
15 |
-
|
16 |
-
@abstractmethod
|
17 |
-
def render(self) -> gr.Box:
|
18 |
-
...
|
19 |
-
|
20 |
-
|
21 |
-
class Input(Component):
|
22 |
-
def render(self) -> None:
|
23 |
-
with gr.Box(visible=self.visible) as component:
|
24 |
with gr.Row():
|
25 |
self.output_name = gr.Textbox(
|
26 |
label="Input name (can be referenced with {})",
|
@@ -32,36 +22,65 @@ class Input(Component):
|
|
32 |
interactive=True,
|
33 |
placeholder="Variable value",
|
34 |
)
|
35 |
-
|
36 |
|
37 |
|
38 |
-
class AITask
|
39 |
-
def render(self) ->
|
40 |
-
with gr.Box(visible=
|
41 |
gr.Markdown(f"AI task")
|
42 |
with gr.Row():
|
43 |
with gr.Column():
|
44 |
self.prompt = gr.Textbox(
|
45 |
label="Instructions",
|
46 |
-
lines=
|
47 |
interactive=True,
|
48 |
placeholder="What is the AI assistant meant to do?",
|
49 |
)
|
50 |
with gr.Column():
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
65 |
|
66 |
all_inputs[0].visible = True
|
67 |
all_tasks[0].visible = True
|
@@ -103,40 +122,70 @@ def remove_task():
|
|
103 |
return _update_components(next_task, MAX_TASKS)
|
104 |
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
with gr.Blocks() as demo:
|
107 |
-
#
|
108 |
for i in all_inputs:
|
109 |
i.render()
|
|
|
|
|
|
|
|
|
|
|
110 |
with gr.Row():
|
111 |
add_input_btn = gr.Button("Add input variable")
|
112 |
remove_input_btn = gr.Button("Remove input variable")
|
113 |
execute_btn = gr.Button("Execute")
|
114 |
for t in all_tasks:
|
115 |
t.render()
|
|
|
|
|
|
|
|
|
|
|
116 |
with gr.Row():
|
117 |
add_task_btn = gr.Button("Add task")
|
118 |
remove_task_btn = gr.Button("Remove task")
|
119 |
|
120 |
-
#
|
121 |
add_input_btn.click(
|
122 |
add_input,
|
123 |
inputs=[],
|
124 |
-
outputs=[i.
|
125 |
)
|
126 |
remove_input_btn.click(
|
127 |
remove_input,
|
128 |
inputs=[],
|
129 |
-
outputs=[i.
|
130 |
)
|
131 |
add_task_btn.click(
|
132 |
add_task,
|
133 |
inputs=[],
|
134 |
-
outputs=[t.
|
135 |
)
|
136 |
remove_task_btn.click(
|
137 |
remove_task,
|
138 |
inputs=[],
|
139 |
-
outputs=[t.
|
140 |
)
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
demo.launch()
|
|
|
1 |
+
from typing import NamedTuple, Type, Union
|
2 |
|
3 |
|
4 |
import gradio as gr
|
|
|
8 |
MAX_TASKS = 50
|
9 |
|
10 |
|
11 |
+
class Input:
|
12 |
+
def render(self, visible: bool) -> gr.Box:
|
13 |
+
with gr.Box(visible=visible) as gr_component: # TODO: Remove this
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
with gr.Row():
|
15 |
self.output_name = gr.Textbox(
|
16 |
label="Input name (can be referenced with {})",
|
|
|
22 |
interactive=True,
|
23 |
placeholder="Variable value",
|
24 |
)
|
25 |
+
return gr_component
|
26 |
|
27 |
|
28 |
+
class AITask:
|
29 |
+
def render(self, visible: bool) -> gr.Box:
|
30 |
+
with gr.Box(visible=visible) as gr_component:
|
31 |
gr.Markdown(f"AI task")
|
32 |
with gr.Row():
|
33 |
with gr.Column():
|
34 |
self.prompt = gr.Textbox(
|
35 |
label="Instructions",
|
36 |
+
lines=13,
|
37 |
interactive=True,
|
38 |
placeholder="What is the AI assistant meant to do?",
|
39 |
)
|
40 |
with gr.Column():
|
41 |
+
self.output_name = gr.Textbox(
|
42 |
+
label="Output name (can be referenced with {})",
|
43 |
+
interactive=True,
|
44 |
+
placeholder="Variable name",
|
45 |
+
)
|
46 |
+
self.output = gr.Textbox(
|
47 |
+
show_label=False,
|
48 |
+
lines=10,
|
49 |
+
interactive=False,
|
50 |
+
)
|
51 |
+
return gr_component
|
52 |
+
|
53 |
+
|
54 |
+
class Component:
|
55 |
+
def __init__(self, id_: int, internal: Union[Input, AITask], visible: bool = False):
|
56 |
+
self._id = id_
|
57 |
+
self.component_id: gr.Textbox
|
58 |
+
self._internal = internal
|
59 |
+
self.gr_component = gr.Box
|
60 |
+
self.visible = visible
|
61 |
+
self.output_name: gr.Textbox
|
62 |
+
self.output: gr.Textbox
|
63 |
+
self.source: gr.Textbox
|
64 |
|
65 |
+
def render(self) -> None:
|
66 |
+
self.component_id = gr.Textbox(value=str(self._id), visible=False)
|
67 |
+
self.source = gr.Textbox(value=self._internal.__class__.__name__, visible=False)
|
68 |
+
self.gr_component = self._internal.render(self.visible)
|
69 |
+
self.output_name = self._internal.output_name
|
70 |
+
self.output = self._internal.output
|
71 |
+
|
72 |
+
|
73 |
+
class Variable(NamedTuple):
|
74 |
+
source: Type[Union[Input, AITask]]
|
75 |
+
id_: int
|
76 |
+
name: str
|
77 |
+
value: str
|
78 |
|
79 |
+
|
80 |
+
all_inputs = [Component(i, Input()) for i in range(MAX_INPUTS)]
|
81 |
+
all_tasks = [Component(i, AITask()) for i in range(MAX_TASKS)]
|
82 |
+
all_components = all_inputs + all_tasks
|
83 |
+
all_variables = {} # Will be updated once rendered
|
84 |
|
85 |
all_inputs[0].visible = True
|
86 |
all_tasks[0].visible = True
|
|
|
122 |
return _update_components(next_task, MAX_TASKS)
|
123 |
|
124 |
|
125 |
+
def execute(output_names, outputs):
|
126 |
+
for output_name in output_names:
|
127 |
+
print(output_name)
|
128 |
+
|
129 |
+
|
130 |
+
def update_scope_variables(component_id, source, output_name, output):
|
131 |
+
all_variables[f"{source}.{component_id}"] = Variable(
|
132 |
+
component_id, source, output_name, output
|
133 |
+
)
|
134 |
+
print(all_variables)
|
135 |
+
|
136 |
+
|
137 |
with gr.Blocks() as demo:
|
138 |
+
# Initial layout
|
139 |
for i in all_inputs:
|
140 |
i.render()
|
141 |
+
input_error = gr.HighlightedText(
|
142 |
+
[("Repeated variable names in inputs. Please pick different names.", "Error")],
|
143 |
+
show_label=False,
|
144 |
+
visible=False,
|
145 |
+
)
|
146 |
with gr.Row():
|
147 |
add_input_btn = gr.Button("Add input variable")
|
148 |
remove_input_btn = gr.Button("Remove input variable")
|
149 |
execute_btn = gr.Button("Execute")
|
150 |
for t in all_tasks:
|
151 |
t.render()
|
152 |
+
task_error = gr.HighlightedText(
|
153 |
+
[("Repeated variable names in tasks. Please pick different names.", "Error")],
|
154 |
+
show_label=False,
|
155 |
+
visible=False,
|
156 |
+
)
|
157 |
with gr.Row():
|
158 |
add_task_btn = gr.Button("Add task")
|
159 |
remove_task_btn = gr.Button("Remove task")
|
160 |
|
161 |
+
# Layout editing
|
162 |
add_input_btn.click(
|
163 |
add_input,
|
164 |
inputs=[],
|
165 |
+
outputs=[i.gr_component for i in all_inputs],
|
166 |
)
|
167 |
remove_input_btn.click(
|
168 |
remove_input,
|
169 |
inputs=[],
|
170 |
+
outputs=[i.gr_component for i in all_inputs],
|
171 |
)
|
172 |
add_task_btn.click(
|
173 |
add_task,
|
174 |
inputs=[],
|
175 |
+
outputs=[t.gr_component for t in all_tasks],
|
176 |
)
|
177 |
remove_task_btn.click(
|
178 |
remove_task,
|
179 |
inputs=[],
|
180 |
+
outputs=[t.gr_component for t in all_tasks],
|
181 |
)
|
182 |
|
183 |
+
# Execution
|
184 |
+
for c in all_components:
|
185 |
+
c.output_name.change(
|
186 |
+
update_scope_variables,
|
187 |
+
inputs=[c.component_id, c.source, c.output_name, c.output],
|
188 |
+
outputs=[],
|
189 |
+
)
|
190 |
+
|
191 |
demo.launch()
|