Skip to content

[Blocks] Support Blocks components as Plug and Use components #960

@omerXfaruq

Description

@omerXfaruq

The design of Blocks does not support multiple Blocks defined outside the main Blocks scope. But this is needed for complex Blocks components as it will enable users to package their modules under smaller Blocks

We can potentially support it with a new design,

  • either a workaround function that passes every component, event and layouts inside the smaller Blocks to Parent Blocks
  • or have smaller Blocks as a component inside the Bigger Blocks with its config (this will need changes within frontend)

Usage example:

import gradio as gr

import random

xray_model = lambda diseases, img: {disease: random.random() for disease in diseases}
ct_model = lambda diseases, img: {disease: 0.1 for disease in diseases}

with gr.Blocks() as demo_1:
    disease = gr.CheckboxGroup(
        choices=["Covid", "Malaria", "Lung Cancer"], label="Disease to Scan For"
    )
    with gr.Tabs():
        with gr.TabItem("Blocks2"):
            with gr.Row():
                xray_scan = gr.Image()
                xray_results = gr.JSON()
            xray_run = gr.Button("Run")
            xray_run.click(
                xray_model, inputs=[disease, xray_scan], outputs=[xray_results]
            )

with gr.Blocks() as demo_2:
     with gr.Row():
            ct_scan = gr.Image()
            ct_results = gr.JSON()
            ct_run = gr.Button("Run")
            ct_run.click(ct_model, inputs=[disease, ct_scan], outputs=ct_results)


with gr.Blocks() as demo:
    gr.Markdown(
        """
# Detect Disease From Scan
With this model you can lorem ipsum
- ipsum 1
- ipsum 2
"""
    )
    disease = gr.CheckboxGroup(
        choices=["Covid", "Malaria", "Lung Cancer"], label="Disease to Scan For"
    )

    with gr.Tabs():
        with gr.TabItem("X-ray"):
            demo_1.render()
        with gr.TabItem("CT Scan"):
            demo_2.render()
    overall_probability = gr.Textbox()

if __name__ == "__main__":
    print(demo.get_config_file())
    demo.launch()

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions