lgaleana commited on
Commit
169710f
1 Parent(s): db9c02a

Handle errors

Browse files
Files changed (1) hide show
  1. app.py +36 -12
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from typing import Dict, Optional, Union
2
 
3
  import gradio as gr
@@ -133,24 +134,47 @@ def remove_task(*visibility):
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:
152
  # Initial layout
153
- gr.Markdown("""
 
154
  # Toolkit
155
  Define input variables to be used in your tasks.
156
  <br>Task outputs can be used in subsequent tasks.
@@ -159,7 +183,8 @@ with gr.Blocks() as demo:
159
  <br>Chain inputs and tasks to build an E2E application.
160
  <br>
161
  <br>Example prompt: "Translate the following text into spanish and add {v0} more sentences: {t0}".
162
- """)
 
163
  for i in all_inputs.values():
164
  i.render()
165
  with gr.Row():
@@ -170,6 +195,7 @@ with gr.Blocks() as demo:
170
  with gr.Row():
171
  add_task_btn = gr.Button("Add task")
172
  remove_task_btn = gr.Button("Remove task")
 
173
  execute_btn = gr.Button("Execute")
174
 
175
  # Edit layout
@@ -200,15 +226,13 @@ with gr.Blocks() as demo:
200
 
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
 
214
  demo.launch()
 
1
+ import re
2
  from typing import Dict, Optional, Union
3
 
4
  import gradio as gr
 
134
  )
135
 
136
 
137
+ def _clear_error():
138
+ return gr.HighlightedText.update(value=None, visible=False)
139
+
140
+
141
+ def execute_task(id_: int, prompt: str, prev_error_value, *vars):
142
  inputs = vars[:MAX_INPUTS]
143
  task_outputs = vars[MAX_INPUTS:]
144
 
145
+ prompt_vars = set(re.findall("{(.*?)}", prompt))
146
+ vars_in_scope = {
147
  f"{Input.vname}{i}": input_ for i, input_ in enumerate(inputs) if input_
148
  }
149
+ vars_in_scope.update(
150
  {f"{AITask.vname}{i}": task for i, task in enumerate(task_outputs)}
151
  )
152
+ undefined_vars = prompt_vars - vars_in_scope.keys()
153
+
154
+ if len(undefined_vars) > 0:
155
+ return None, gr.HighlightedText.update(
156
+ value=[
157
+ (
158
+ f"The following variables are being used before being defined :: {undefined_vars}. Please check your tasks.",
159
+ "ERROR",
160
+ )
161
+ ],
162
+ visible=True,
163
+ )
164
+ error_update = gr.HighlightedText.update(
165
+ value=prev_error_value, visible=prev_error_value is not None
166
+ )
167
 
168
  if prompt:
169
+ return all_tasks[id_].execute(prompt, vars_in_scope), error_update
170
+
171
+ return None, error_update
172
 
173
 
174
  with gr.Blocks() as demo:
175
  # Initial layout
176
+ gr.Markdown(
177
+ """
178
  # Toolkit
179
  Define input variables to be used in your tasks.
180
  <br>Task outputs can be used in subsequent tasks.
 
183
  <br>Chain inputs and tasks to build an E2E application.
184
  <br>
185
  <br>Example prompt: "Translate the following text into spanish and add {v0} more sentences: {t0}".
186
+ """
187
+ )
188
  for i in all_inputs.values():
189
  i.render()
190
  with gr.Row():
 
195
  with gr.Row():
196
  add_task_btn = gr.Button("Add task")
197
  remove_task_btn = gr.Button("Remove task")
198
+ error_message = gr.HighlightedText(value=None, visible=False)
199
  execute_btn = gr.Button("Execute")
200
 
201
  # Edit layout
 
226
 
227
  # Sequential execution
228
  execution_event = execute_btn.click(
229
+ _clear_error, inputs=[], outputs=[error_message]
 
 
230
  )
231
+ for i, task in all_tasks.items():
232
  execution_event = execution_event.then(
233
  execute_task,
234
+ inputs=[task.component_id, task.internal.prompt, error_message] + _get_all_vars_up_to(i), # type: ignore
235
+ outputs=[task.output, error_message],
236
  )
237
 
238
  demo.launch()