File size: 7,081 Bytes
5c20801 846ae23 d5b1bca 5c20801 d5b1bca 846ae23 d5b1bca 759ceaa bc38777 b92b58b 846ae23 5c20801 846ae23 759ceaa 418800c 759ceaa 846ae23 759ceaa 846ae23 759ceaa 5c20801 759ceaa 5c20801 bc38777 759ceaa 418800c 5c20801 b92b58b bc38777 5c20801 bc38777 b92b58b 759ceaa 846ae23 759ceaa 5c20801 b92b58b 418800c 759ceaa 5c20801 759ceaa 846ae23 759ceaa 5c20801 846ae23 b92b58b 846ae23 b92b58b 846ae23 b92b58b 846ae23 b92b58b 846ae23 d5b1bca b92b58b 759ceaa 5c20801 d5b1bca 759ceaa bc38777 846ae23 759ceaa 846ae23 bc38777 846ae23 759ceaa 846ae23 759ceaa 846ae23 b92b58b 5c20801 b92b58b 846ae23 b92b58b 5c20801 b92b58b 846ae23 b92b58b 5c20801 b92b58b 846ae23 b92b58b 5c20801 b92b58b 846ae23 5c20801 846ae23 |
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 212 213 214 215 216 217 218 219 220 221 222 223 224 |
from typing import NamedTuple, Optional, Type, Union
import gradio as gr
import ai
from utils.io import print_system
MAX_INPUTS = 10
MAX_TASKS = 50
class Input:
def render(self, visible: bool) -> gr.Row:
with gr.Row(visible=visible) as gr_component:
self.output_name = gr.Textbox(
label="Input name (can be referenced with {})",
interactive=True,
placeholder="Variable name",
)
self.output = gr.Textbox(
label="Input value",
interactive=True,
placeholder="Variable value",
)
return gr_component
def execute(self) -> None:
pass
class AITask:
@property
def vars(self):
return [self.prompt]
def render(self, visible: bool) -> gr.Box:
with gr.Box(visible=visible) as gr_component:
gr.Markdown(f"AI task")
with gr.Row():
with gr.Column():
self.prompt = gr.Textbox(
label="Instructions",
lines=13,
interactive=True,
placeholder="What is the AI assistant meant to do?",
)
with gr.Column():
self.output_name = gr.Textbox(
label="Output name (can be referenced with {})",
interactive=True,
placeholder="Variable name",
)
self.output = gr.Textbox(
show_label=False,
lines=10,
interactive=False,
)
return gr_component
def execute(self, prompt: str) -> Optional[str]:
if prompt:
return ai.llm.next([{"role": "user", "content": prompt}])
class Component:
def __init__(
self, id_: float, internal: Union[Input, AITask], visible: bool = False
):
# Internal state
self._id = id_
self.internal = internal
self._source = self.internal.__class__.__name__
self._initial_visibility = visible
# Gradio state
self.component_id: gr.Number
self.source: gr.Textbox
self.visible: gr.Number
self.gr_component = gr.Box
self.output_name: gr.Textbox
self.output: gr.Textbox
def render(self) -> None:
self.component_id = gr.Number(value=self._id, visible=False)
self.source = gr.Textbox(value=self._source, visible=False)
self.visible = gr.Number(int(self._initial_visibility), visible=False)
self.gr_component = self.internal.render(self._initial_visibility)
self.output_name = self.internal.output_name
self.output = self.internal.output
def execute(self, *args):
print_system(f"Executing component :: {self._source}.{self._id}")
return self.internal.execute(*args)
class Variable(NamedTuple):
source: Type[Union[Input, AITask]]
id_: int
name: str
value: str
all_inputs = {float(i): Component(i, Input()) for i in range(MAX_INPUTS)}
all_tasks = {float(i): Component(i, AITask()) for i in range(MAX_TASKS)}
all_inputs[0]._initial_visibility = True
all_tasks[0]._initial_visibility = True
def add_input(*visibility):
for i, visible in enumerate(visibility, 1):
if not bool(visible):
return (
[gr.Row.update(visible=True)] * i
+ [gr.Row.update(visible=False)] * (MAX_INPUTS - i)
+ [1] * i
+ [0] * (MAX_INPUTS - i)
)
def remove_input(*visibility):
for i, visible in reversed(list(enumerate(visibility, 1))):
if bool(visible):
return (
[gr.Row.update(visible=True)] * (i - 1)
+ [gr.Row.update(visible=False)] * (MAX_INPUTS - i + 1)
+ [1] * (i - 1)
+ [0] * (MAX_INPUTS - i + 1)
)
def add_task(*visibility):
for i, visible in enumerate(visibility, 1):
if not bool(visible):
return (
[gr.Box.update(visible=True)] * i
+ [gr.Box.update(visible=False)] * (MAX_TASKS - i)
+ [1] * i
+ [0] * (MAX_TASKS - i)
)
def remove_task(*visibility):
for i, visible in reversed(list(enumerate(visibility, 1))):
if bool(visible):
return (
[gr.Box.update(visible=True)] * (i - 1)
+ [gr.Box.update(visible=False)] * (MAX_TASKS - i + 1)
+ [1] * (i - 1)
+ [0] * (MAX_TASKS - i + 1)
)
def execute_task(id_: float, prompt: str):
if prompt:
return all_tasks[id_].execute(prompt)
with gr.Blocks() as demo:
# Initial layout
for i in all_inputs.values():
i.render()
input_error = gr.HighlightedText(
[("Repeated variable names in inputs. Please pick different names.", "Error")],
show_label=False,
visible=False,
)
with gr.Row():
add_input_btn = gr.Button("Add input variable")
remove_input_btn = gr.Button("Remove input variable")
execute_btn = gr.Button("Execute")
for t in all_tasks.values():
t.render()
task_error = gr.HighlightedText(
[("Repeated variable names in tasks. Please pick different names.", "Error")],
show_label=False,
visible=False,
)
with gr.Row():
add_task_btn = gr.Button("Add task")
remove_task_btn = gr.Button("Remove task")
# Layout editing
add_input_btn.click(
add_input,
inputs=[i.visible for i in all_inputs.values()],
outputs=[i.gr_component for i in all_inputs.values()] # type: ignore
+ [i.visible for i in all_inputs.values()],
)
remove_input_btn.click(
remove_input,
inputs=[i.visible for i in all_inputs.values()],
outputs=[i.gr_component for i in all_inputs.values()] # type: ignore
+ [i.visible for i in all_inputs.values()],
)
add_task_btn.click(
add_task,
inputs=[i.visible for i in all_tasks.values()],
outputs=[i.gr_component for i in all_tasks.values()] # type: ignore
+ [i.visible for i in all_tasks.values()],
)
remove_task_btn.click(
remove_task,
inputs=[i.visible for i in all_tasks.values()],
outputs=[i.gr_component for i in all_tasks.values()] # type: ignore
+ [i.visible for i in all_tasks.values()],
)
# Sequential execution
execution_event = execute_btn.click(
execute_task,
inputs=[all_tasks[0].component_id, all_tasks[0].internal.prompt], # type: ignore
outputs=[all_tasks[0].output],
)
for task in list(all_tasks.values())[1:]:
execution_event = execution_event.then(
execute_task,
inputs=[task.component_id, task.internal.prompt], # type: ignore
outputs=[task.output],
)
demo.launch()
|