lgaleana commited on
Commit
846ae23
β€’
1 Parent(s): fb46dd6

Added layout

Browse files
Files changed (3) hide show
  1. .gitignore +2 -1
  2. README.md +2 -2
  3. app.py +135 -13
.gitignore CHANGED
@@ -1 +1,2 @@
1
- venv
 
 
1
+ venv
2
+ .vscode
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Toolkit
3
  emoji: 😎
4
- colorFrom: blue
5
- colorTo: red
6
  sdk: gradio
7
  sdk_version: 3.32.0
8
  app_file: app.py
 
1
  ---
2
  title: Toolkit
3
  emoji: 😎
4
+ colorFrom: red
5
+ colorTo: blue
6
  sdk: gradio
7
  sdk_version: 3.32.0
8
  app_file: app.py
app.py CHANGED
@@ -1,20 +1,142 @@
 
 
 
1
  import gradio as gr
2
 
3
- demo = None
4
 
5
- def component():
6
- with gr.Row():
7
- gr.Textbox("Hello")
8
- gr.Textbox("World")
9
 
10
 
11
- with gr.Blocks() as demo:
12
- for c in [component, component]:
13
- c()
14
- gr.Button("Hey")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- print("heyhey")
17
  with gr.Blocks() as demo:
18
- for c in [component, component]:
19
- c()
20
- gr.Textbox("World")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from abc import ABC, abstractmethod
2
+
3
+
4
  import gradio as gr
5
 
 
6
 
7
+ MAX_INPUTS = 10
8
+ MAX_TASKS = 50
 
 
9
 
10
 
11
+ class Component(ABC):
12
+ def __init__(self, visible: bool = False):
13
+ self.component = None
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 {})",
27
+ interactive=True,
28
+ placeholder="Variable name",
29
+ )
30
+ self.output = gr.Textbox(
31
+ label="Input value",
32
+ interactive=True,
33
+ placeholder="Variable value",
34
+ )
35
+ self.component = component
36
+
37
+
38
+ class AITask(Component):
39
+ def render(self) -> None:
40
+ with gr.Box(visible=self.visible) as component:
41
+ gr.Markdown(f"AI task")
42
+ with gr.Row():
43
+ with gr.Column():
44
+ self.prompt = gr.Textbox(
45
+ label="Instructions",
46
+ lines=15,
47
+ interactive=True,
48
+ placeholder="What is the AI assistant meant to do?",
49
+ )
50
+ with gr.Column():
51
+ with gr.Box():
52
+ self.output_name = gr.Textbox(
53
+ label="Output name", interactive=True, placeholder="var"
54
+ )
55
+ self.output = gr.Textbox(
56
+ label="",
57
+ lines=10,
58
+ interactive=False,
59
+ )
60
+ self.component = component
61
+
62
+
63
+ all_inputs = [Input() for _ in range(MAX_INPUTS)]
64
+ all_tasks = [AITask() for _ in range(MAX_TASKS)]
65
+
66
+ all_inputs[0].visible = True
67
+ all_tasks[0].visible = True
68
+ next_input = 1
69
+ next_task = 1
70
+
71
+
72
+ def _update_components(i: int, max: int):
73
+ return [gr.Box.update(visible=True)] * i + [gr.Box.update(visible=False)] * (
74
+ max - i
75
+ )
76
+
77
+
78
+ def add_input():
79
+ global next_input
80
+ if next_input < MAX_INPUTS:
81
+ next_input += 1
82
+ return _update_components(next_input, MAX_INPUTS)
83
+
84
+
85
+ def remove_input():
86
+ global next_input
87
+ if next_input > 0:
88
+ next_input -= 1
89
+ return _update_components(next_input, MAX_INPUTS)
90
+
91
+
92
+ def add_task():
93
+ global next_task
94
+ if next_task < MAX_TASKS:
95
+ next_task += 1
96
+ return _update_components(next_task, MAX_TASKS)
97
+
98
+
99
+ def remove_task():
100
+ global next_task
101
+ if next_task > 0:
102
+ next_task -= 1
103
+ return _update_components(next_task, MAX_TASKS)
104
+
105
 
 
106
  with gr.Blocks() as demo:
107
+ # Layout
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
+ # Event handling
121
+ add_input_btn.click(
122
+ add_input,
123
+ inputs=[],
124
+ outputs=[i.component for i in all_inputs],
125
+ )
126
+ remove_input_btn.click(
127
+ remove_input,
128
+ inputs=[],
129
+ outputs=[i.component for i in all_inputs],
130
+ )
131
+ add_task_btn.click(
132
+ add_task,
133
+ inputs=[],
134
+ outputs=[t.component for t in all_tasks],
135
+ )
136
+ remove_task_btn.click(
137
+ remove_task,
138
+ inputs=[],
139
+ outputs=[t.component for t in all_tasks],
140
+ )
141
+
142
+ demo.launch()