lgaleana commited on
Commit
5e9e416
1 Parent(s): a35fa4d

Refactor into class hierarchy

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. components.py +43 -34
app.py CHANGED
@@ -64,7 +64,7 @@ with gr.Blocks() as demo:
64
  for i, task in all_tasks.items():
65
  execution_event = execution_event.then(
66
  a.execute_task,
67
- inputs=[task.component_id, task.internal.prompt, error_message] + a._get_all_vars_up_to(i), # type: ignore
68
  outputs=[task.output, error_message],
69
  )
70
 
 
64
  for i, task in all_tasks.items():
65
  execution_event = execution_event.then(
66
  a.execute_task,
67
+ inputs=[task.component_id, task.prompt, error_message] + a._get_all_vars_up_to(i), # type: ignore
68
  outputs=[task.output, error_message],
69
  )
70
 
components.py CHANGED
@@ -1,14 +1,48 @@
1
- from typing import Dict, Optional, Union
 
2
 
3
  import gradio as gr
4
 
5
  import ai
6
 
7
 
8
- class Input:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  vname = "v"
10
 
11
- def render(self, id_: int, visible: bool) -> gr.Textbox:
12
  self.output = gr.Textbox(
13
  label=f"Input: {{{self.vname}{id_}}}",
14
  interactive=True,
@@ -17,14 +51,14 @@ class Input:
17
  )
18
  return self.output
19
 
20
- def execute(self) -> None:
21
  pass
22
 
23
 
24
- class AITask:
25
  vname = "t"
26
 
27
- def render(self, id_: int, visible: bool) -> gr.Box:
28
  with gr.Box(visible=visible) as gr_component:
29
  gr.Markdown(f"AI task")
30
  with gr.Row():
@@ -41,43 +75,18 @@ class AITask:
41
  )
42
  return gr_component
43
 
44
- def execute(self, prompt: str, prompt_vars: Dict[str, str]) -> Optional[str]:
45
  if prompt:
46
  formatted_prompt = prompt.format(**prompt_vars)
47
  return ai.llm.next([{"role": "user", "content": formatted_prompt}])
48
 
49
 
50
- class Component:
51
- def __init__(self, id_: int, internal: Union[Input, AITask], visible: bool = False):
52
- # Internal state
53
- self._id = id_
54
- self.internal = internal
55
- self._source = self.internal.__class__.__name__
56
- self._initial_visibility = visible
57
-
58
- # Gradio state
59
- self.component_id: gr.Number
60
- self.visible: gr.Number
61
- self.gr_component = gr.Box
62
- self.output: gr.Textbox
63
-
64
- def render(self) -> None:
65
- self.component_id = gr.Number(value=self._id, visible=False)
66
- self.visible = gr.Number(int(self._initial_visibility), visible=False)
67
- self.gr_component = self.internal.render(self._id, self._initial_visibility)
68
- self.output = self.internal.output
69
-
70
- def execute(self, *args):
71
- print(f"Executing component :: {self._source}.{self._id}")
72
- return self.internal.execute(*args)
73
-
74
-
75
  MAX_INPUTS = 10
76
  MAX_TASKS = 10
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
 
1
+ from abc import ABC, abstractmethod
2
+ from typing import Dict, Optional
3
 
4
  import gradio as gr
5
 
6
  import ai
7
 
8
 
9
+ class Component(ABC):
10
+ vname = None
11
+
12
+ def __init__(self, id_: int, visible: bool = False):
13
+ # Internal state
14
+ self._id = id_
15
+ self._source = self.__class__.__name__
16
+ self._initial_visibility = visible
17
+
18
+ # Gradio state
19
+ self.component_id: gr.Number
20
+ self.visible: gr.Number
21
+ self.gr_component = gr.Box
22
+ self.output: gr.Textbox
23
+
24
+ @abstractmethod
25
+ def _render(self, id_: int, visible: bool):
26
+ pass
27
+
28
+ @abstractmethod
29
+ def _execute(self):
30
+ pass
31
+
32
+ def render(self) -> None:
33
+ self.component_id = gr.Number(value=self._id, visible=False)
34
+ self.visible = gr.Number(int(self._initial_visibility), visible=False)
35
+ self.gr_component = self._render(self._id, self._initial_visibility)
36
+
37
+ def execute(self, *args):
38
+ print(f"Executing component :: {self._source}.{self._id}")
39
+ return self._execute(*args)
40
+
41
+
42
+ class Input(Component):
43
  vname = "v"
44
 
45
+ def _render(self, id_: int, visible: bool) -> gr.Textbox:
46
  self.output = gr.Textbox(
47
  label=f"Input: {{{self.vname}{id_}}}",
48
  interactive=True,
 
51
  )
52
  return self.output
53
 
54
+ def _execute(self):
55
  pass
56
 
57
 
58
+ class AITask(Component):
59
  vname = "t"
60
 
61
+ def _render(self, id_: int, visible: bool) -> gr.Box:
62
  with gr.Box(visible=visible) as gr_component:
63
  gr.Markdown(f"AI task")
64
  with gr.Row():
 
75
  )
76
  return gr_component
77
 
78
+ def _execute(self, prompt: str, prompt_vars: Dict[str, str]) -> Optional[str]:
79
  if prompt:
80
  formatted_prompt = prompt.format(**prompt_vars)
81
  return ai.llm.next([{"role": "user", "content": formatted_prompt}])
82
 
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  MAX_INPUTS = 10
85
  MAX_TASKS = 10
86
 
87
 
88
+ all_inputs = {i: Input(i) for i in range(MAX_INPUTS)}
89
+ all_tasks = {i: AITask(i) for i in range(MAX_TASKS)}
90
 
91
  all_inputs[0]._initial_visibility = True
92
  all_tasks[0]._initial_visibility = True