Skip to content

Commit a8ab1c8

Browse files
aliabid94omerXfaruqabidlabsAli Abid
authored
Blocks dev (#853)
* Blocks-Components-v2 - default -> default_value refactoring * Blocks-Components-v2 - refactor output types into output_type and make them auto * Blocks-Backend-Events - all events are implemented * Blocks-Backend-Events - refactor fn: str -> Callable * Blocks-Backend-Events - add change event to TabItem * Blocks Backend Components - Remove KeyValues from components * Blocks-Backend-Components-v2 - Resolve Components imports in inputs and outputs * Blocks-Backend-Components-v2 - fix default parameter usage in demos * Blocks-Backend-Components-v2 - fix default parameter usage in demos * Blocks-Backend-Components-v2 - fix default parameter usage in demos * Blocks-Backend-Components-v2 - docstring updates * revert demos * Blocks-Backend-Components-v2 - tweaks * Blocks-Backend-Components-v2 - add change event to Tabs * Interfaces from Blocks (#849) Build interfaces from blocks Co-authored-by: Ali Abid <aliabid94@gmail.com> Co-authored-by: Ömer Faruk Özdemir <farukozderim@gmail.com> Co-authored-by: Abubakar Abid <abubakar@huggingface.co> Co-authored-by: Ali Abid <aliabid94@gmail.com>
1 parent 566c954 commit a8ab1c8

89 files changed

Lines changed: 7516 additions & 354 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

demo/xray_blocks/run.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,38 @@
88
xray_blocks = gr.Blocks()
99

1010
with xray_blocks:
11-
gr.Markdown(
11+
gr.components.Markdown(
1212
"""
1313
# Detect Disease From Scan
1414
With this model you can lorem ipsum
1515
- ipsum 1
1616
- ipsum 2
1717
"""
1818
)
19-
disease = gr.inputs.CheckboxGroup(
20-
["Covid", "Malaria", "Lung Cancer"], label="Disease to Scan For"
21-
)
19+
disease = gr.components.CheckboxGroup(choices=["Covid", "Malaria", "Lung Cancer"], label="Disease to Scan For")
2220

2321
with gr.Tabs():
2422
with gr.TabItem("X-ray"):
2523
with gr.Row():
26-
xray_scan = gr.inputs.Image()
27-
xray_results = gr.outputs.JSON()
28-
output_textbox = gr.outputs.Textbox()
29-
input_textbox = gr.inputs.Textbox(default="Hello This Is a Input Textbox")
30-
xray_run = gr.Button("Run")
24+
xray_scan = gr.components.Image()
25+
xray_results = gr.components.JSON()
26+
output_textbox = gr.components.Textbox()
27+
input_textbox = gr.components.Textbox(default_value="Hello This Is a Input Textbox")
28+
xray_run = gr.Button("Run", css={
29+
"background-color": "red",
30+
"--hover-color": "orange"
31+
})
3132
xray_run.click(xray_model, inputs=[disease, xray_scan], outputs=xray_results)
3233
xray_run.click(xray_model, inputs=[disease, xray_scan], outputs=output_textbox)
3334

3435
with gr.TabItem("CT Scan"):
3536
with gr.Row():
36-
ct_scan = gr.inputs.Image()
37-
ct_results = gr.outputs.JSON()
37+
ct_scan = gr.components.Image()
38+
ct_results = gr.components.JSON()
3839
ct_run = gr.Button("Run")
3940
ct_run.click(ct_model, inputs=[disease, ct_scan], outputs=ct_results)
4041

41-
overall_probability = gr.outputs.Textbox()
42+
overall_probability = gr.components.Textbox()
4243

4344
# TODO: remove later
4445
import json

gradio/blocks.py

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,33 @@ def __init__(self):
1919
Context.root_block.blocks[self._id] = self
2020
self.events = []
2121

22-
def click(self, fn, inputs, outputs):
23-
if not isinstance(inputs, list):
24-
inputs = [inputs]
25-
if not isinstance(outputs, list):
26-
outputs = [outputs]
27-
Context.root_block.fns.append(fn)
28-
Context.root_block.dependencies.append(
29-
{
30-
"id": len(Context.root_block.dependencies),
31-
"targets": [self._id],
32-
"trigger": "click",
33-
"inputs": [block._id for block in inputs],
34-
"outputs": [block._id for block in outputs],
35-
}
36-
)
37-
38-
def change(
39-
self, fn: str, inputs: List["Component"], outputs: List["Component"]
22+
def set_event_trigger(
23+
self,
24+
event_name: str,
25+
fn: Callable,
26+
inputs: List[Component],
27+
outputs: List[Component],
4028
) -> None:
4129
"""
42-
Adds change event to the component's dependencies.
43-
44-
Whenever the component changes the function is triggered.
45-
30+
Adds an event to the component's dependencies.
4631
Parameters:
47-
fn: function name
32+
event_name: event name
33+
fn: Callable function
4834
inputs: input list
4935
outputs: output list
50-
5136
Returns: None
52-
5337
"""
38+
# Support for singular parameter
5439
if not isinstance(inputs, list):
5540
inputs = [inputs]
5641
if not isinstance(outputs, list):
5742
outputs = [outputs]
58-
Context.root_block.fns.append(fn)
59-
Context.root_block.dependencies.append(
60-
{
61-
"targets": [self._id],
62-
"trigger": "change",
63-
"inputs": [block._id for block in inputs],
64-
"outputs": [block._id for block in outputs],
65-
}
66-
)
6743

68-
def save(
69-
self, fn: str, inputs: List["Component"], outputs: List["Component"]
70-
) -> None:
71-
"""
72-
Adds save event to the component's dependencies.
73-
74-
Whenever the component is saved the function is triggered.
75-
76-
Parameters:
77-
fn: function name
78-
inputs: input list
79-
outputs: output list
80-
81-
Returns: None
82-
83-
"""
84-
if not isinstance(inputs, list):
85-
inputs = [inputs]
86-
if not isinstance(outputs, list):
87-
outputs = [outputs]
8844
Context.root_block.fns.append(fn)
8945
Context.root_block.dependencies.append(
9046
{
9147
"targets": [self._id],
92-
"trigger": "save",
48+
"trigger": event_name,
9349
"inputs": [block._id for block in inputs],
9450
"outputs": [block._id for block in outputs],
9551
}
@@ -120,7 +76,15 @@ def get_template_context(self):
12076

12177

12278
class Tabs(BlockContext):
123-
pass
79+
def change(self, fn: Callable, inputs: List[Component], outputs: List[Component]):
80+
"""
81+
Parameters:
82+
fn: Callable function
83+
inputs: List of inputs
84+
outputs: List of outputs
85+
Returns: None
86+
"""
87+
self.set_event_trigger("change", fn, inputs, outputs)
12488

12589

12690
class TabItem(BlockContext):
@@ -131,6 +95,16 @@ def __init__(self, label):
13195
def get_template_context(self):
13296
return {"label": self.label}
13397

98+
def change(self, fn: Callable, inputs: List[Component], outputs: List[Component]):
99+
"""
100+
Parameters:
101+
fn: Callable function
102+
inputs: List of inputs
103+
outputs: List of outputs
104+
Returns: None
105+
"""
106+
self.set_event_trigger("change", fn, inputs, outputs)
107+
134108

135109
class Blocks(Launchable, BlockContext):
136110
def __init__(self, theme="default"):

0 commit comments

Comments
 (0)