lgaleana commited on
Commit
b3d1811
1 Parent(s): 4bd8f86

Pass execute exclusively to tasks

Browse files
Files changed (2) hide show
  1. ai/llm.py +0 -1
  2. components.py +20 -25
ai/llm.py CHANGED
@@ -23,7 +23,6 @@ def call(
23
  if not temperature:
24
  temperature = TEMPERATURE
25
 
26
- print(f"Running OpenAI with :: {locals()}")
27
  return openai.ChatCompletion.create( # type: ignore
28
  model=model,
29
  messages=messages,
 
23
  if not temperature:
24
  temperature = TEMPERATURE
25
 
 
26
  return openai.ChatCompletion.create( # type: ignore
27
  model=model,
28
  messages=messages,
components.py CHANGED
@@ -26,18 +26,11 @@ class Component(ABC):
26
  def _render(self, id_: int, visible: bool):
27
  ...
28
 
29
- @abstractmethod
30
- def _execute(self):
31
- ...
32
-
33
  def render(self) -> None:
34
  self.component_id = gr.Number(value=self._id, visible=False)
35
  self.visible = gr.Number(int(self._initial_visibility), visible=False)
36
  self.gr_component = self._render(self._id, self._initial_visibility)
37
 
38
- def execute(self, *args):
39
- return self._execute(*args)
40
-
41
 
42
  class Input(Component):
43
  VNAME = "v"
@@ -51,9 +44,6 @@ class Input(Component):
51
  )
52
  return self.output
53
 
54
- def _execute(self) -> None:
55
- pass
56
-
57
 
58
  class TaskComponent(Component, ABC):
59
  VNAME = "t"
@@ -70,6 +60,10 @@ class TaskComponent(Component, ABC):
70
  super().render()
71
  self.n_inputs = gr.Number(value=self._n_inputs, visible=False)
72
 
 
 
 
 
73
 
74
  class AITask(TaskComponent):
75
  NAME = "AI Task"
@@ -96,9 +90,10 @@ class AITask(TaskComponent):
96
  )
97
  return gr_component
98
 
99
- def _execute(self, prompt: str, prompt_vars: Dict[str, str]) -> Optional[str]:
100
  if prompt:
101
- formatted_prompt = prompt.format(**prompt_vars)
 
102
  return ai.llm.next([{"role": "user", "content": formatted_prompt}])
103
 
104
  def inputs(self) -> List[gr.Textbox]:
@@ -129,9 +124,10 @@ class VisitURL(TaskComponent):
129
  )
130
  return gr_component
131
 
132
- def _execute(self, url: str, prompt_vars: Dict[str, str]) -> Optional[str]:
133
  if url:
134
- formatted_url = url.format(**prompt_vars)
 
135
  return requests.get(formatted_url).text
136
 
137
  def inputs(self) -> List[gr.Textbox]:
@@ -144,39 +140,39 @@ class Task:
144
 
145
  def __init__(self, id_: int):
146
  self._id = id_
147
- self.active_task = AITask.NAME # Default
148
- self.inner_tasks = {t.NAME: t(self._id, False) for t in self.AVAILABLE_TASKS}
149
 
150
  def render(self) -> None:
151
- for t in self.inner_tasks.values():
152
  t.render()
153
 
154
  @property
155
  def component_id(self) -> gr.Textbox:
156
- return self.inner_tasks[self.active_task].component_id
157
 
158
  @property
159
  def visibilities(self) -> List[gr.Number]:
160
- return [t.visible for t in self.inner_tasks.values()]
161
 
162
  @property
163
  def gr_components(self) -> List[gr.Box]:
164
- return [t.gr_component for t in self.inner_tasks.values()]
165
 
166
  @property
167
  def output(self) -> gr.Textbox:
168
- return self.inner_tasks[self.active_task].output
169
 
170
  @property
171
  def inputs(self) -> List[gr.Textbox]:
172
- return self.inner_tasks[self.active_task].inputs()
173
 
174
  @property
175
  def n_inputs(self) -> int:
176
- return self.inner_tasks[self.active_task].n_inputs
177
 
178
  def execute(self, *args):
179
- inner_task = self.inner_tasks[self.active_task]
180
  print(f"Executing {inner_task._source}: {inner_task._id}")
181
  return inner_task.execute(*args)
182
 
@@ -189,4 +185,3 @@ all_inputs = {i: Input(i) for i in range(MAX_INPUTS)}
189
  all_tasks = {i: Task(i) for i in range(MAX_TASKS)}
190
 
191
  all_inputs[0]._initial_visibility = True
192
- all_tasks[0].inner_tasks[all_tasks[0].active_task]._initial_visibility = True
 
26
  def _render(self, id_: int, visible: bool):
27
  ...
28
 
 
 
 
 
29
  def render(self) -> None:
30
  self.component_id = gr.Number(value=self._id, visible=False)
31
  self.visible = gr.Number(int(self._initial_visibility), visible=False)
32
  self.gr_component = self._render(self._id, self._initial_visibility)
33
 
 
 
 
34
 
35
  class Input(Component):
36
  VNAME = "v"
 
44
  )
45
  return self.output
46
 
 
 
 
47
 
48
  class TaskComponent(Component, ABC):
49
  VNAME = "t"
 
60
  super().render()
61
  self.n_inputs = gr.Number(value=self._n_inputs, visible=False)
62
 
63
+ @abstractmethod
64
+ def execute(self, *vars, vars_in_scope: Dict[str, str]):
65
+ ...
66
+
67
 
68
  class AITask(TaskComponent):
69
  NAME = "AI Task"
 
90
  )
91
  return gr_component
92
 
93
+ def execute(self, prompt: str, vars_in_scope: Dict[str, str]) -> Optional[str]:
94
  if prompt:
95
+ formatted_prompt = prompt.format(**vars_in_scope)
96
+ print(f"Executing {self.NAME} with prompt :: {formatted_prompt}")
97
  return ai.llm.next([{"role": "user", "content": formatted_prompt}])
98
 
99
  def inputs(self) -> List[gr.Textbox]:
 
124
  )
125
  return gr_component
126
 
127
+ def execute(self, url: str, vars_in_scope: Dict[str, str]) -> Optional[str]:
128
  if url:
129
+ formatted_url = url.format(**vars_in_scope)
130
+ print(f"Executing {self.NAME} with url :: {formatted_url}")
131
  return requests.get(formatted_url).text
132
 
133
  def inputs(self) -> List[gr.Textbox]:
 
140
 
141
  def __init__(self, id_: int):
142
  self._id = id_
143
+ self._active_task = AITask.NAME # Default
144
+ self._inner_tasks = {t.NAME: t(self._id, False) for t in self.AVAILABLE_TASKS}
145
 
146
  def render(self) -> None:
147
+ for t in self._inner_tasks.values():
148
  t.render()
149
 
150
  @property
151
  def component_id(self) -> gr.Textbox:
152
+ return self._inner_tasks[self._active_task].component_id
153
 
154
  @property
155
  def visibilities(self) -> List[gr.Number]:
156
+ return [t.visible for t in self._inner_tasks.values()]
157
 
158
  @property
159
  def gr_components(self) -> List[gr.Box]:
160
+ return [t.gr_component for t in self._inner_tasks.values()]
161
 
162
  @property
163
  def output(self) -> gr.Textbox:
164
+ return self._inner_tasks[self._active_task].output
165
 
166
  @property
167
  def inputs(self) -> List[gr.Textbox]:
168
+ return self._inner_tasks[self._active_task].inputs()
169
 
170
  @property
171
  def n_inputs(self) -> int:
172
+ return self._inner_tasks[self._active_task].n_inputs
173
 
174
  def execute(self, *args):
175
+ inner_task = self._inner_tasks[self._active_task]
176
  print(f"Executing {inner_task._source}: {inner_task._id}")
177
  return inner_task.execute(*args)
178
 
 
185
  all_tasks = {i: Task(i) for i in range(MAX_TASKS)}
186
 
187
  all_inputs[0]._initial_visibility = True