File size: 2,545 Bytes
5e9e416 a35fa4d 5e9e416 a35fa4d 5e9e416 a35fa4d 5e9e416 a35fa4d 5e9e416 a35fa4d 5e9e416 a35fa4d 5e9e416 a35fa4d 5e9e416 a35fa4d |
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 |
from abc import ABC, abstractmethod
from typing import Dict, Optional
import gradio as gr
import ai
class Component(ABC):
vname = None
def __init__(self, id_: int, visible: bool = False):
# Internal state
self._id = id_
self._source = self.__class__.__name__
self._initial_visibility = visible
# Gradio state
self.component_id: gr.Number
self.visible: gr.Number
self.gr_component = gr.Box
self.output: gr.Textbox
@abstractmethod
def _render(self, id_: int, visible: bool):
pass
@abstractmethod
def _execute(self):
pass
def render(self) -> None:
self.component_id = gr.Number(value=self._id, visible=False)
self.visible = gr.Number(int(self._initial_visibility), visible=False)
self.gr_component = self._render(self._id, self._initial_visibility)
def execute(self, *args):
print(f"Executing component :: {self._source}.{self._id}")
return self._execute(*args)
class Input(Component):
vname = "v"
def _render(self, id_: int, visible: bool) -> gr.Textbox:
self.output = gr.Textbox(
label=f"Input: {{{self.vname}{id_}}}",
interactive=True,
placeholder="Variable value",
visible=visible,
)
return self.output
def _execute(self):
pass
class AITask(Component):
vname = "t"
def _render(self, id_: int, visible: bool) -> gr.Box:
with gr.Box(visible=visible) as gr_component:
gr.Markdown(f"AI task")
with gr.Row():
self.prompt = gr.Textbox(
label="Instructions",
lines=10,
interactive=True,
placeholder="What is the AI assistant meant to do?",
)
self.output = gr.Textbox(
label=f"Output: {{{self.vname}{id_}}}",
lines=10,
interactive=False,
)
return gr_component
def _execute(self, prompt: str, prompt_vars: Dict[str, str]) -> Optional[str]:
if prompt:
formatted_prompt = prompt.format(**prompt_vars)
return ai.llm.next([{"role": "user", "content": formatted_prompt}])
MAX_INPUTS = 10
MAX_TASKS = 10
all_inputs = {i: Input(i) for i in range(MAX_INPUTS)}
all_tasks = {i: AITask(i) for i in range(MAX_TASKS)}
all_inputs[0]._initial_visibility = True
all_tasks[0]._initial_visibility = True
|