File size: 5,032 Bytes
5e9e416
c121218
a35fa4d
 
9f68e0d
a35fa4d
 
 
 
5e9e416
 
 
 
 
c121218
5e9e416
 
 
c121218
5e9e416
c121218
5e9e416
 
 
c121218
 
 
 
 
 
5e9e416
 
 
0905f7c
a35fa4d
c121218
a35fa4d
0905f7c
a35fa4d
 
c121218
a35fa4d
 
 
 
c121218
0905f7c
78dfff8
c121218
c4df480
c121218
c4df480
c121218
 
 
 
 
 
 
 
 
78dfff8
b3d1811
c4df480
b3d1811
 
78dfff8
9f68e0d
0905f7c
9f68e0d
c121218
 
 
a35fa4d
c4df480
a35fa4d
 
 
c121218
a35fa4d
 
0905f7c
a35fa4d
 
 
 
 
c4df480
 
78dfff8
a35fa4d
9f68e0d
0905f7c
9f68e0d
c121218
 
 
9f68e0d
c4df480
9f68e0d
 
 
 
 
0905f7c
9f68e0d
 
 
c121218
9f68e0d
c4df480
 
9f68e0d
 
c121218
0905f7c
 
9f68e0d
c121218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f68e0d
c121218
 
 
 
 
9f68e0d
 
c4df480
9f68e0d
c4df480
 
9f68e0d
c4df480
 
4bd8f86
c4df480
9f68e0d
 
c4df480
a35fa4d
c4df480
a35fa4d
 
c4df480
0905f7c
c4df480
c121218
0905f7c
c4df480
 
 
0905f7c
c4df480
 
c121218
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from abc import ABC, abstractmethod
from typing import List, Union

import gradio as gr
import requests

import ai


class Component(ABC):
    def __init__(self, id_: int, visible: bool = False):
        # Internal state
        self._id = id_
        self._source = self.__class__.__name__
        self.vname: str

        # Gradio state
        self.component_id: gr.Number
        self.gr_component: Union[gr.Box, gr.Textbox]
        self.output: gr.Textbox
        self.visible: gr.Number

    def render(self) -> None:
        self.component_id = gr.Number(value=self._id, visible=False)
        self.visible = gr.Number(0, visible=False)
        self.gr_component = self._render(self._id)

    @abstractmethod
    def _render(self, id_: int) -> Union[gr.Box, gr.Textbox]:
        ...


class Input(Component):
    vname = "v"

    def _render(self, id_: int) -> gr.Textbox:
        self.output = gr.Textbox(
            label=f"Input: {{{self.vname}{id_}}}",
            interactive=True,
            placeholder="Variable value",
            visible=False,
        )
        return self.output


class TaskComponent(ABC):
    vname = "t"

    def __init__(self):
        self.name: str
        self.gr_component: gr.Box
        self.input: gr.Textbox
        self.output: gr.Textbox

    def render(self, id_: int) -> None:
        self.gr_component = self._render(id_)
        self.gr_component.visible = False

    @abstractmethod
    def _render(self, id_) -> gr.Box:
        ...

    @abstractmethod
    def execute(self, input):
        ...


class AITask(TaskComponent):
    name = "AI Task"

    def _render(self, id_: int) -> gr.Box:
        with gr.Box() as gr_component:
            gr.Markdown("Give instructions to ChatGPT to do something.")
            with gr.Row():
                self.input = gr.Textbox(
                    label="Instructions",
                    lines=10,
                    interactive=True,
                    placeholder="Example: summarize this text: {v0}",
                )
                self.output = gr.Textbox(
                    label=f"Output: {{{self.vname}{id_}}}",
                    lines=10,
                    interactive=False,
                )
            return gr_component

    def execute(self, prompt: str) -> str:
        return ai.llm.next([{"role": "user", "content": prompt}])


class VisitURL(TaskComponent):
    name = "Visit URL"

    def _render(self, id_: int) -> gr.Box:
        with gr.Box() as gr_component:
            gr.Markdown("Visit an URL and get its content.")
            with gr.Row():
                self.input = gr.Textbox(
                    interactive=True,
                    placeholder="URL",
                    show_label=False,
                )
                self.output = gr.Textbox(
                    label=f"Output: {{{self.vname}{id_}}}",
                    lines=10,
                    interactive=False,
                )
        return gr_component

    def execute(self, url: str) -> str:
        return requests.get(url).text


class Task(Component):
    available_tasks = [AITask, VisitURL]
    vname = "t"

    def __init__(self, id_: int, visible: bool = False):
        super().__init__(id_, visible)
        self._inner_tasks = [t() for t in self.available_tasks]
        self.gr_component: gr.Box

    def _render(self, id_: int) -> gr.Box:
        with gr.Box(visible=False) as gr_component:
            self.task_picker = gr.Dropdown(
                [AITask.name, VisitURL.name],
                value=AITask.name,
                label="Pick a new Task",
                type="index",
            )
            self.active_index = gr.Number(-1, visible=False)
            for t in self._inner_tasks:
                t.render(id_)

            self.task_picker.select(
                self.pick_task,
                inputs=[self.task_picker],
                outputs=[t.gr_component for t in self._inner_tasks],
            )
        return gr_component

    @staticmethod
    def pick_task(idx):
        update = [gr.Box.update(visible=False)] * len(Task.available_tasks)
        update[idx] = gr.Box.update(visible=True)
        return update

    def inputs(self) -> List[gr.Textbox]:
        return [t.input for t in self._inner_tasks]

    def outputs(self) -> List[gr.Textbox]:
        return [t.output for t in self._inner_tasks]

    def execute(self, active_index, input):
        inner_task = self._inner_tasks[active_index]
        print(f"Executing {inner_task._source}: {inner_task._id}")
        return inner_task.execute(input)


MAX_TASKS = 10

all_tasks = {i: Task(i) for i in range(MAX_TASKS)}


class Tasks:
    @classmethod
    def visibilities(cls) -> List[gr.Number]:
        return [t.visible for t in all_tasks.values()]

    @classmethod
    def active_indexes(cls) -> List[gr.Number]:
        return [t.active_index for t in all_tasks.values()]

    @classmethod
    def gr_components(cls) -> List[gr.Box]:
        return [t.gr_component for t in all_tasks.values()]