Add hello world example
Browse files- actions.py +0 -2
- app.py +39 -35
- components.py +20 -25
- examples/__init__.py +0 -0
- examples/actions.py +45 -0
- examples/hello.py +38 -0
actions.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import re
|
2 |
-
|
3 |
import gradio as gr
|
4 |
|
5 |
from components import MAX_TASKS, all_tasks, Task
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
from components import MAX_TASKS, all_tasks, Task
|
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
import actions as a
|
|
|
4 |
from components import all_tasks, Tasks
|
5 |
|
6 |
|
@@ -23,43 +24,46 @@ with gr.Blocks() as demo:
|
|
23 |
<br>2. AI Task: Summarize {t0}.
|
24 |
"""
|
25 |
)
|
26 |
-
|
27 |
-
t.
|
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 |
-
prev_tasks = []
|
54 |
-
for i, task in all_tasks.items():
|
55 |
-
execution_event = execution_event.then(
|
56 |
-
a.execute_task,
|
57 |
-
inputs=[task.component_id, task.active_index, error_message]
|
58 |
-
+ task.inputs
|
59 |
-
+ [t.active_index for t in prev_tasks]
|
60 |
-
+ [o for t in prev_tasks for o in t.outputs],
|
61 |
-
outputs=task.outputs + [error_message],
|
62 |
)
|
63 |
-
prev_tasks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
import actions as a
|
4 |
+
from examples import hello
|
5 |
from components import all_tasks, Tasks
|
6 |
|
7 |
|
|
|
24 |
<br>2. AI Task: Summarize {t0}.
|
25 |
"""
|
26 |
)
|
27 |
+
with gr.Tab("Toolkit"):
|
28 |
+
for t in all_tasks.values():
|
29 |
+
t.render()
|
30 |
+
with gr.Row():
|
31 |
+
add_task_btn = gr.Button("Add task")
|
32 |
+
remove_task_btn = gr.Button("Remove task")
|
33 |
+
error_message = gr.HighlightedText(value=None, visible=False)
|
34 |
+
execute_btn = gr.Button("Execute")
|
35 |
|
36 |
+
# Edit layout
|
37 |
+
add_task_btn.click(
|
38 |
+
a.add_task,
|
39 |
+
inputs=Tasks.visibilities(),
|
40 |
+
outputs=Tasks.gr_components() + Tasks.visibilities(),
|
41 |
+
)
|
42 |
+
remove_task_btn.click(
|
43 |
+
a.remove_task,
|
44 |
+
inputs=Tasks.visibilities(),
|
45 |
+
outputs=Tasks.gr_components() + Tasks.visibilities(),
|
46 |
+
)
|
47 |
|
48 |
+
# Sequential execution
|
49 |
+
execution_event = execute_btn.click(
|
50 |
+
# Clear error message
|
51 |
+
lambda: gr.HighlightedText.update(value=None, visible=False),
|
52 |
+
inputs=[],
|
53 |
+
outputs=[error_message],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
)
|
55 |
+
prev_tasks = []
|
56 |
+
for i, task in all_tasks.items():
|
57 |
+
execution_event = execution_event.then(
|
58 |
+
a.execute_task,
|
59 |
+
inputs=[task.component_id, task.active_index, error_message]
|
60 |
+
+ task.inputs
|
61 |
+
+ [t.active_index for t in prev_tasks]
|
62 |
+
+ [o for t in prev_tasks for o in t.outputs],
|
63 |
+
outputs=task.outputs + [error_message],
|
64 |
+
)
|
65 |
+
prev_tasks.append(task)
|
66 |
+
with gr.Tab("Example: Hello world"):
|
67 |
+
hello.render()
|
68 |
|
69 |
demo.launch()
|
components.py
CHANGED
@@ -24,19 +24,19 @@ class Component(ABC):
|
|
24 |
def render(self) -> None:
|
25 |
self.component_id = gr.Number(value=self._id, visible=False)
|
26 |
self.visible = gr.Number(0, visible=False)
|
27 |
-
self.gr_component = self._render(
|
28 |
|
29 |
@abstractmethod
|
30 |
-
def _render(self
|
31 |
...
|
32 |
|
33 |
|
34 |
class Input(Component):
|
35 |
vname = "v"
|
36 |
|
37 |
-
def _render(self
|
38 |
self.output = gr.Textbox(
|
39 |
-
label=f"Input: {{{self.vname}{
|
40 |
interactive=True,
|
41 |
placeholder="Variable value",
|
42 |
visible=False,
|
@@ -44,15 +44,15 @@ class Input(Component):
|
|
44 |
return self.output
|
45 |
|
46 |
|
47 |
-
class TaskComponent(ABC):
|
48 |
vname = "t"
|
49 |
|
50 |
-
def __init__(self):
|
|
|
|
|
|
|
51 |
self.name: str
|
52 |
-
self.gr_component: gr.Box
|
53 |
self.input: gr.Textbox
|
54 |
-
self.output: gr.Textbox
|
55 |
-
self._source = self.__class__.__name__
|
56 |
|
57 |
def format_input(self, input: str, vars_in_scope: Dict[str, Any]) -> str:
|
58 |
input = input.strip()
|
@@ -64,13 +64,6 @@ class TaskComponent(ABC):
|
|
64 |
)
|
65 |
return input.format(**vars_in_scope)
|
66 |
|
67 |
-
def render(self, id_: int) -> None:
|
68 |
-
self.gr_component = self._render(id_)
|
69 |
-
|
70 |
-
@abstractmethod
|
71 |
-
def _render(self, id_) -> gr.Box:
|
72 |
-
...
|
73 |
-
|
74 |
@property
|
75 |
def n_inputs(self) -> int:
|
76 |
return len(self.inputs)
|
@@ -88,17 +81,18 @@ class TaskComponent(ABC):
|
|
88 |
class AITask(TaskComponent):
|
89 |
name = "AI Task"
|
90 |
|
91 |
-
def _render(self
|
92 |
-
with gr.Box(visible=
|
93 |
with gr.Row():
|
94 |
self.input = gr.Textbox(
|
95 |
label="Instructions",
|
96 |
lines=10,
|
97 |
interactive=True,
|
98 |
placeholder="What would you like ChatGPT to do?",
|
|
|
99 |
)
|
100 |
self.output = gr.Textbox(
|
101 |
-
label=f"Output: {{{self.vname}{
|
102 |
lines=10,
|
103 |
interactive=True,
|
104 |
)
|
@@ -116,11 +110,12 @@ class AITask(TaskComponent):
|
|
116 |
class CodeTask(TaskComponent):
|
117 |
name = "Code Task"
|
118 |
|
119 |
-
def _render(self
|
120 |
-
with gr.Column(visible=
|
121 |
code_prompt = gr.Textbox(
|
122 |
label="What would you like to do?",
|
123 |
interactive=True,
|
|
|
124 |
)
|
125 |
generate_code = gr.Button("Generate code")
|
126 |
with gr.Row():
|
@@ -148,7 +143,7 @@ class CodeTask(TaskComponent):
|
|
148 |
)
|
149 |
with gr.Column():
|
150 |
self.output = gr.Textbox(
|
151 |
-
label=f"Output: {{{self.vname}{
|
152 |
lines=10,
|
153 |
interactive=True,
|
154 |
)
|
@@ -287,10 +282,10 @@ class Task(Component):
|
|
287 |
|
288 |
def __init__(self, id_: int):
|
289 |
super().__init__(id_)
|
290 |
-
self._inner_tasks = [t() for t in self.available_tasks]
|
291 |
self.gr_component: gr.Box
|
292 |
|
293 |
-
def _render(self
|
294 |
with gr.Box(visible=False) as gr_component:
|
295 |
self.active_index = gr.Dropdown(
|
296 |
[AITask.name, CodeTask.name],
|
@@ -298,7 +293,7 @@ class Task(Component):
|
|
298 |
type="index",
|
299 |
)
|
300 |
for t in self._inner_tasks:
|
301 |
-
t.render(
|
302 |
|
303 |
self.active_index.select(
|
304 |
self.pick_task,
|
|
|
24 |
def render(self) -> None:
|
25 |
self.component_id = gr.Number(value=self._id, visible=False)
|
26 |
self.visible = gr.Number(0, visible=False)
|
27 |
+
self.gr_component = self._render()
|
28 |
|
29 |
@abstractmethod
|
30 |
+
def _render(self) -> Union[gr.Box, gr.Textbox]:
|
31 |
...
|
32 |
|
33 |
|
34 |
class Input(Component):
|
35 |
vname = "v"
|
36 |
|
37 |
+
def _render(self) -> gr.Textbox:
|
38 |
self.output = gr.Textbox(
|
39 |
+
label=f"Input: {{{self.vname}{self._id}}}",
|
40 |
interactive=True,
|
41 |
placeholder="Variable value",
|
42 |
visible=False,
|
|
|
44 |
return self.output
|
45 |
|
46 |
|
47 |
+
class TaskComponent(Component, ABC):
|
48 |
vname = "t"
|
49 |
|
50 |
+
def __init__(self, id_: int, value: str = "", visible: bool = False):
|
51 |
+
super().__init__(id_)
|
52 |
+
self._initial_value = value
|
53 |
+
self._initial_visbility = visible
|
54 |
self.name: str
|
|
|
55 |
self.input: gr.Textbox
|
|
|
|
|
56 |
|
57 |
def format_input(self, input: str, vars_in_scope: Dict[str, Any]) -> str:
|
58 |
input = input.strip()
|
|
|
64 |
)
|
65 |
return input.format(**vars_in_scope)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
@property
|
68 |
def n_inputs(self) -> int:
|
69 |
return len(self.inputs)
|
|
|
81 |
class AITask(TaskComponent):
|
82 |
name = "AI Task"
|
83 |
|
84 |
+
def _render(self) -> gr.Box:
|
85 |
+
with gr.Box(visible=self._initial_visbility) as gr_component:
|
86 |
with gr.Row():
|
87 |
self.input = gr.Textbox(
|
88 |
label="Instructions",
|
89 |
lines=10,
|
90 |
interactive=True,
|
91 |
placeholder="What would you like ChatGPT to do?",
|
92 |
+
value=self._initial_value,
|
93 |
)
|
94 |
self.output = gr.Textbox(
|
95 |
+
label=f"Output: {{{self.vname}{self._id}}}",
|
96 |
lines=10,
|
97 |
interactive=True,
|
98 |
)
|
|
|
110 |
class CodeTask(TaskComponent):
|
111 |
name = "Code Task"
|
112 |
|
113 |
+
def _render(self) -> gr.Column:
|
114 |
+
with gr.Column(visible=self._initial_visbility) as gr_component:
|
115 |
code_prompt = gr.Textbox(
|
116 |
label="What would you like to do?",
|
117 |
interactive=True,
|
118 |
+
value=self._initial_value,
|
119 |
)
|
120 |
generate_code = gr.Button("Generate code")
|
121 |
with gr.Row():
|
|
|
143 |
)
|
144 |
with gr.Column():
|
145 |
self.output = gr.Textbox(
|
146 |
+
label=f"Output: {{{self.vname}{self._id}}}",
|
147 |
lines=10,
|
148 |
interactive=True,
|
149 |
)
|
|
|
282 |
|
283 |
def __init__(self, id_: int):
|
284 |
super().__init__(id_)
|
285 |
+
self._inner_tasks = [t(id_) for t in self.available_tasks]
|
286 |
self.gr_component: gr.Box
|
287 |
|
288 |
+
def _render(self) -> gr.Box:
|
289 |
with gr.Box(visible=False) as gr_component:
|
290 |
self.active_index = gr.Dropdown(
|
291 |
[AITask.name, CodeTask.name],
|
|
|
293 |
type="index",
|
294 |
)
|
295 |
for t in self._inner_tasks:
|
296 |
+
t.render()
|
297 |
|
298 |
self.active_index.select(
|
299 |
self.pick_task,
|
examples/__init__.py
ADDED
File without changes
|
examples/actions.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from components import Task
|
4 |
+
|
5 |
+
demo_tasks = {}
|
6 |
+
|
7 |
+
|
8 |
+
def execute_task(demo_id: str, task_id: int, error_value, *args):
|
9 |
+
error_update = gr.HighlightedText.update(
|
10 |
+
value=error_value, visible=error_value is not None
|
11 |
+
)
|
12 |
+
|
13 |
+
if error_value:
|
14 |
+
return ["", error_update]
|
15 |
+
|
16 |
+
task_id = int(task_id)
|
17 |
+
n_inputs = demo_tasks[demo_id][task_id].n_inputs
|
18 |
+
|
19 |
+
task_inputs = args[:n_inputs]
|
20 |
+
prev_task_outputs = args[n_inputs:]
|
21 |
+
|
22 |
+
non_empty_inputs = [i for i in task_inputs if i]
|
23 |
+
if not non_empty_inputs:
|
24 |
+
return ["", error_update]
|
25 |
+
|
26 |
+
# Put task outputs in a dictionary with names.
|
27 |
+
vars_in_scope = {f"{Task.vname}{i}": o for i, o in enumerate(prev_task_outputs)}
|
28 |
+
|
29 |
+
try:
|
30 |
+
# Task logic gets inserted into the right index
|
31 |
+
output = demo_tasks[demo_id][task_id].execute(
|
32 |
+
*task_inputs, vars_in_scope=vars_in_scope
|
33 |
+
)
|
34 |
+
return [output, error_update]
|
35 |
+
except Exception as e:
|
36 |
+
import traceback
|
37 |
+
|
38 |
+
print(traceback.format_tb(e.__traceback__))
|
39 |
+
return [
|
40 |
+
"ERROR",
|
41 |
+
gr.HighlightedText.update(
|
42 |
+
value=[(f"Error in Task {task_id} :: {e}", "ERROR")],
|
43 |
+
visible=True,
|
44 |
+
),
|
45 |
+
]
|
examples/hello.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from components import AITask, CodeTask
|
3 |
+
|
4 |
+
import examples.actions as ea
|
5 |
+
|
6 |
+
|
7 |
+
DEMO_ID = "hello"
|
8 |
+
tasks = [
|
9 |
+
CodeTask(0, "Say hello world", visible=True),
|
10 |
+
AITask(1, "Explain this to me: {t0}", visible=True),
|
11 |
+
]
|
12 |
+
ea.demo_tasks[DEMO_ID] = tasks
|
13 |
+
|
14 |
+
|
15 |
+
def render():
|
16 |
+
demo_id = gr.Textbox(DEMO_ID, visible=False)
|
17 |
+
tasks[0].render()
|
18 |
+
tasks[1].render()
|
19 |
+
error_message = gr.HighlightedText(value=None, visible=False)
|
20 |
+
execute_btn = gr.Button("Execute")
|
21 |
+
|
22 |
+
# Sequential execution
|
23 |
+
execution_event = execute_btn.click(
|
24 |
+
# Clear error message
|
25 |
+
lambda: gr.HighlightedText.update(value=None, visible=False),
|
26 |
+
inputs=[],
|
27 |
+
outputs=[error_message],
|
28 |
+
)
|
29 |
+
prev_tasks = []
|
30 |
+
for task in tasks:
|
31 |
+
execution_event = execution_event.then(
|
32 |
+
ea.execute_task,
|
33 |
+
inputs=[demo_id, task.component_id, error_message]
|
34 |
+
+ task.inputs
|
35 |
+
+ [t.output for t in prev_tasks],
|
36 |
+
outputs=[task.output, error_message],
|
37 |
+
)
|
38 |
+
prev_tasks.append(task)
|