Added variables to scope
Browse files- app.py +59 -68
- utils/__init__.py +0 -0
- utils/io.py +0 -12
app.py
CHANGED
|
@@ -1,72 +1,58 @@
|
|
| 1 |
-
from typing import
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
import ai
|
| 6 |
-
from utils.io import print_system
|
| 7 |
|
| 8 |
|
| 9 |
MAX_INPUTS = 10
|
| 10 |
-
MAX_TASKS =
|
| 11 |
|
| 12 |
|
| 13 |
class Input:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
placeholder="Variable value",
|
| 25 |
-
)
|
| 26 |
-
return gr_component
|
| 27 |
|
| 28 |
def execute(self) -> None:
|
| 29 |
pass
|
| 30 |
|
| 31 |
|
| 32 |
class AITask:
|
| 33 |
-
|
| 34 |
-
def vars(self):
|
| 35 |
-
return [self.prompt]
|
| 36 |
|
| 37 |
-
def render(self, visible: bool) -> gr.Box:
|
| 38 |
with gr.Box(visible=visible) as gr_component:
|
| 39 |
gr.Markdown(f"AI task")
|
| 40 |
with gr.Row():
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
placeholder="Variable name",
|
| 53 |
-
)
|
| 54 |
-
self.output = gr.Textbox(
|
| 55 |
-
show_label=False,
|
| 56 |
-
lines=10,
|
| 57 |
-
interactive=False,
|
| 58 |
-
)
|
| 59 |
return gr_component
|
| 60 |
|
| 61 |
-
def execute(self, prompt: str) -> Optional[str]:
|
| 62 |
if prompt:
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
class Component:
|
| 67 |
-
def __init__(
|
| 68 |
-
self, id_: float, internal: Union[Input, AITask], visible: bool = False
|
| 69 |
-
):
|
| 70 |
# Internal state
|
| 71 |
self._id = id_
|
| 72 |
self.internal = internal
|
|
@@ -75,45 +61,40 @@ class Component:
|
|
| 75 |
|
| 76 |
# Gradio state
|
| 77 |
self.component_id: gr.Number
|
| 78 |
-
self.source: gr.Textbox
|
| 79 |
self.visible: gr.Number
|
| 80 |
self.gr_component = gr.Box
|
| 81 |
-
self.output_name: gr.Textbox
|
| 82 |
self.output: gr.Textbox
|
| 83 |
|
| 84 |
def render(self) -> None:
|
| 85 |
self.component_id = gr.Number(value=self._id, visible=False)
|
| 86 |
-
self.source = gr.Textbox(value=self._source, visible=False)
|
| 87 |
self.visible = gr.Number(int(self._initial_visibility), visible=False)
|
| 88 |
-
self.gr_component = self.internal.render(self._initial_visibility)
|
| 89 |
-
self.output_name = self.internal.output_name
|
| 90 |
self.output = self.internal.output
|
| 91 |
|
| 92 |
def execute(self, *args):
|
| 93 |
-
|
| 94 |
return self.internal.execute(*args)
|
| 95 |
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
id_: int
|
| 100 |
-
name: str
|
| 101 |
-
value: str
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
all_inputs = {float(i): Component(i, Input()) for i in range(MAX_INPUTS)}
|
| 105 |
-
all_tasks = {float(i): Component(i, AITask()) for i in range(MAX_TASKS)}
|
| 106 |
|
| 107 |
all_inputs[0]._initial_visibility = True
|
| 108 |
all_tasks[0]._initial_visibility = True
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def add_input(*visibility):
|
| 112 |
for i, visible in enumerate(visibility, 1):
|
| 113 |
if not bool(visible):
|
| 114 |
return (
|
| 115 |
-
[gr.
|
| 116 |
-
+ [gr.
|
| 117 |
+ [1] * i
|
| 118 |
+ [0] * (MAX_INPUTS - i)
|
| 119 |
)
|
|
@@ -123,8 +104,8 @@ def remove_input(*visibility):
|
|
| 123 |
for i, visible in reversed(list(enumerate(visibility, 1))):
|
| 124 |
if bool(visible):
|
| 125 |
return (
|
| 126 |
-
[gr.
|
| 127 |
-
+ [gr.
|
| 128 |
+ [1] * (i - 1)
|
| 129 |
+ [0] * (MAX_INPUTS - i + 1)
|
| 130 |
)
|
|
@@ -152,9 +133,19 @@ def remove_task(*visibility):
|
|
| 152 |
)
|
| 153 |
|
| 154 |
|
| 155 |
-
def execute_task(id_:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
if prompt:
|
| 157 |
-
return all_tasks[id_].execute(prompt)
|
| 158 |
|
| 159 |
|
| 160 |
with gr.Blocks() as demo:
|
|
@@ -181,7 +172,7 @@ with gr.Blocks() as demo:
|
|
| 181 |
add_task_btn = gr.Button("Add task")
|
| 182 |
remove_task_btn = gr.Button("Remove task")
|
| 183 |
|
| 184 |
-
#
|
| 185 |
add_input_btn.click(
|
| 186 |
add_input,
|
| 187 |
inputs=[i.visible for i in all_inputs.values()],
|
|
@@ -210,13 +201,13 @@ with gr.Blocks() as demo:
|
|
| 210 |
# Sequential execution
|
| 211 |
execution_event = execute_btn.click(
|
| 212 |
execute_task,
|
| 213 |
-
inputs=[all_tasks[0].component_id, all_tasks[0].internal.prompt], # type: ignore
|
| 214 |
outputs=[all_tasks[0].output],
|
| 215 |
)
|
| 216 |
-
for task in list(all_tasks.
|
| 217 |
execution_event = execution_event.then(
|
| 218 |
execute_task,
|
| 219 |
-
inputs=[task.component_id, task.internal.prompt], # type: ignore
|
| 220 |
outputs=[task.output],
|
| 221 |
)
|
| 222 |
|
|
|
|
| 1 |
+
from typing import Dict, Optional, Union
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
import ai
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
MAX_INPUTS = 10
|
| 9 |
+
MAX_TASKS = 10
|
| 10 |
|
| 11 |
|
| 12 |
class Input:
|
| 13 |
+
vname = "v"
|
| 14 |
+
|
| 15 |
+
def render(self, id_: int, visible: bool) -> gr.Textbox:
|
| 16 |
+
self.output = gr.Textbox(
|
| 17 |
+
label=f"Input: {{{self.vname}{id_}}}",
|
| 18 |
+
interactive=True,
|
| 19 |
+
placeholder="Variable value",
|
| 20 |
+
visible=visible,
|
| 21 |
+
)
|
| 22 |
+
return self.output
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def execute(self) -> None:
|
| 25 |
pass
|
| 26 |
|
| 27 |
|
| 28 |
class AITask:
|
| 29 |
+
vname = "t"
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def render(self, id_: int, visible: bool) -> gr.Box:
|
| 32 |
with gr.Box(visible=visible) as gr_component:
|
| 33 |
gr.Markdown(f"AI task")
|
| 34 |
with gr.Row():
|
| 35 |
+
self.prompt = gr.Textbox(
|
| 36 |
+
label="Instructions",
|
| 37 |
+
lines=10,
|
| 38 |
+
interactive=True,
|
| 39 |
+
placeholder="What is the AI assistant meant to do?",
|
| 40 |
+
)
|
| 41 |
+
self.output = gr.Textbox(
|
| 42 |
+
label=f"Output: {{{self.vname}{id_}}}",
|
| 43 |
+
lines=10,
|
| 44 |
+
interactive=False,
|
| 45 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return gr_component
|
| 47 |
|
| 48 |
+
def execute(self, prompt: str, prompt_vars: Dict[str, str]) -> Optional[str]:
|
| 49 |
if prompt:
|
| 50 |
+
formatted_prompt = prompt.format(**prompt_vars)
|
| 51 |
+
return ai.llm.next([{"role": "user", "content": formatted_prompt}])
|
| 52 |
|
| 53 |
|
| 54 |
class Component:
|
| 55 |
+
def __init__(self, id_: int, internal: Union[Input, AITask], visible: bool = False):
|
|
|
|
|
|
|
| 56 |
# Internal state
|
| 57 |
self._id = id_
|
| 58 |
self.internal = internal
|
|
|
|
| 61 |
|
| 62 |
# Gradio state
|
| 63 |
self.component_id: gr.Number
|
|
|
|
| 64 |
self.visible: gr.Number
|
| 65 |
self.gr_component = gr.Box
|
|
|
|
| 66 |
self.output: gr.Textbox
|
| 67 |
|
| 68 |
def render(self) -> None:
|
| 69 |
self.component_id = gr.Number(value=self._id, visible=False)
|
|
|
|
| 70 |
self.visible = gr.Number(int(self._initial_visibility), visible=False)
|
| 71 |
+
self.gr_component = self.internal.render(self._id, self._initial_visibility)
|
|
|
|
| 72 |
self.output = self.internal.output
|
| 73 |
|
| 74 |
def execute(self, *args):
|
| 75 |
+
print(f"Executing component :: {self._source}.{self._id}")
|
| 76 |
return self.internal.execute(*args)
|
| 77 |
|
| 78 |
|
| 79 |
+
all_inputs = {i: Component(i, Input()) for i in range(MAX_INPUTS)}
|
| 80 |
+
all_tasks = {i: Component(i, AITask()) for i in range(MAX_TASKS)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
all_inputs[0]._initial_visibility = True
|
| 83 |
all_tasks[0]._initial_visibility = True
|
| 84 |
|
| 85 |
|
| 86 |
+
def _get_all_vars_up_to(to: int):
|
| 87 |
+
return [in_.output for in_ in all_inputs.values()] + [
|
| 88 |
+
t.output for i, t in all_tasks.items() if i < to
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
def add_input(*visibility):
|
| 93 |
for i, visible in enumerate(visibility, 1):
|
| 94 |
if not bool(visible):
|
| 95 |
return (
|
| 96 |
+
[gr.Textbox.update(visible=True)] * i
|
| 97 |
+
+ [gr.Textbox.update(visible=False, value="")] * (MAX_INPUTS - i)
|
| 98 |
+ [1] * i
|
| 99 |
+ [0] * (MAX_INPUTS - i)
|
| 100 |
)
|
|
|
|
| 104 |
for i, visible in reversed(list(enumerate(visibility, 1))):
|
| 105 |
if bool(visible):
|
| 106 |
return (
|
| 107 |
+
[gr.Textbox.update(visible=True)] * (i - 1)
|
| 108 |
+
+ [gr.Textbox.update(visible=False, value="")] * (MAX_INPUTS - i + 1)
|
| 109 |
+ [1] * (i - 1)
|
| 110 |
+ [0] * (MAX_INPUTS - i + 1)
|
| 111 |
)
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
|
| 136 |
+
def execute_task(id_: int, prompt: str, *vars):
|
| 137 |
+
inputs = vars[:MAX_INPUTS]
|
| 138 |
+
task_outputs = vars[MAX_INPUTS:]
|
| 139 |
+
|
| 140 |
+
prompt_vars = {
|
| 141 |
+
f"{Input.vname}{i}": input_ for i, input_ in enumerate(inputs) if input_
|
| 142 |
+
}
|
| 143 |
+
prompt_vars.update(
|
| 144 |
+
{f"{AITask.vname}{i}": task for i, task in enumerate(task_outputs)}
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
if prompt:
|
| 148 |
+
return all_tasks[id_].execute(prompt, prompt_vars)
|
| 149 |
|
| 150 |
|
| 151 |
with gr.Blocks() as demo:
|
|
|
|
| 172 |
add_task_btn = gr.Button("Add task")
|
| 173 |
remove_task_btn = gr.Button("Remove task")
|
| 174 |
|
| 175 |
+
# Edit layout
|
| 176 |
add_input_btn.click(
|
| 177 |
add_input,
|
| 178 |
inputs=[i.visible for i in all_inputs.values()],
|
|
|
|
| 201 |
# Sequential execution
|
| 202 |
execution_event = execute_btn.click(
|
| 203 |
execute_task,
|
| 204 |
+
inputs=[all_tasks[0].component_id, all_tasks[0].internal.prompt] + _get_all_vars_up_to(0), # type: ignore
|
| 205 |
outputs=[all_tasks[0].output],
|
| 206 |
)
|
| 207 |
+
for i, task in list(all_tasks.items())[1:]:
|
| 208 |
execution_event = execution_event.then(
|
| 209 |
execute_task,
|
| 210 |
+
inputs=[task.component_id, task.internal.prompt] + _get_all_vars_up_to(i), # type: ignore
|
| 211 |
outputs=[task.output],
|
| 212 |
)
|
| 213 |
|
utils/__init__.py
DELETED
|
File without changes
|
utils/io.py
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
def print_system(message) -> str:
|
| 2 |
-
print(f"\033[0;0m{message}")
|
| 3 |
-
return message
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
def print_assistant(message) -> str:
|
| 7 |
-
print(f"\033[92m{message}")
|
| 8 |
-
return message
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def user_input(message: str = "") -> str:
|
| 12 |
-
return input(f"\033[1;34m{message}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|