gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.39k stars 2.52k forks source link

[Blocks] String shortcuts for components #731

Closed aliabd closed 2 years ago

aliabd commented 2 years ago

Can we support string shortcuts for components like in Interface?

shortcuts like "sketchpad" save from having to define 4 parameters (gr.inputs.Image(image_mode='L', source="canvas", shape=(28, 28), invert_colors=True))

Not currently supported in blocks.

abidlabs commented 2 years ago

That's a good point, creating a sketchpad is way too much effort. Maybe a generic Component() which could take string shortcuts?

omerXfaruq commented 2 years ago

I think using strings for component names are not very proper and feels hacky to me. We can have these pre-prepared components in a class or in a file and import from there.

from gradio.component.Templates import Sketchpad
inputs = [Sketchpad()]

or

from gradio import Templates
inputs = [Templates.Sketchpad()]

or

from gradio.component import Sketchpad
inputs = [Sketchpad()]
abidlabs commented 2 years ago

String identifiers are pretty common practice in parameters the way we have them in inputs and outputs. For example, see https://keras.io/api/optimizers/ ("adam" and Adam() both work). I actually think they are part of the convenience of high level APIs like Keras and Gradio.

That being said, they are not very convenient in the Blocks context, since you can't just have a string by itself the way you could have a Class being instantiated. I think the approach you are suggesting @FarukOzderim make sense to me. I would prefer the last one, if possible, so that all of the components live in the same namespace. I think there is a small typo and it should read:

from gradio.component import Sketchpad
inputs = Sketchpad()
omerXfaruq commented 2 years ago

About this issue let's give the final decision

abidlabs commented 2 years ago

for blocks we want to have Template components that are implemented by us.

To clarify, how would this work? Would it be that we pass in Template("sketchpad") and it renders the Image component with the "sketchpad" parameters? If so, that seems reasonable to me.

What about Interface? Would you want to drop support shortcuts within Interface and move to Templates usage as well? Or still support shortcuts within Interface?

We should still support shortcuts within Interface: (a) because they make Interface easy to use for common use cases (b) backwards compatibility

omerXfaruq commented 2 years ago

Ok then;

abidlabs commented 2 years ago

Ah okay, got it! So Sketchpad will subclass Image, right? Sounds good to me.

cc @aliabid94

aliabid94 commented 2 years ago

Yep I like @FarukOzderim suggestion

omerXfaruq commented 2 years ago

To distinguish real components and Custom or Template components I want to create a hierarchy and import mechanism like this, WDYT? The size of custom components can increase a lot in the future, we can even accept custom components from community.

Please also share your thoughts about naming

We could have

from gradio import Templates

sketchpad = Templates.Sketchpad()
from gradio.Templates import Sketchpad

sketchpad = Sketchpad()
from gradio import CustomComponents

sketchpad = CustomComponents.Sketchpad()
abidlabs commented 2 years ago

To distinguish real components and Custom or Template components I want to create a hierarchy and import mechanism like this, WDYT? The size of custom components can increase a lot in the future, we can even accept custom components from community.

Sounds good to me. I prefer Templates as it is shorter than CustomComponents and a single word

omerXfaruq commented 2 years ago

@abidlabs @aliabd Would we want to have templates for these shortcuts?

  Dataframe
{
            "dataframe": {"type": "pandas"},
            "numpy": {"type": "numpy"},
            "matrix": {"type": "array"},
            "list": {"type": "array", "col_count": 1},
        }
abidlabs commented 2 years ago

I think we should. Not sure how commonly they are used, but there should be no harm in including them.