Skip to main content

A basic gradio class, class function, and functional decorator

Project description

gradioWrapper 🎁

License: MIT

@Author Luca Vivona 🙈

Github github/LVivona

Table of contents

Quick Start Import

from gradioWrapper import register, GradioCompiler, functionalCompiler, tabularGradio

Whats New in v0.0.7

  • Genral Functional Decorator
  • Genral Tablular Functional Decorator

What is it? 🤨

In essence it's extension to the gradio, by using wrappers/decorators that is built into python I'm able wrap class function into overlapping function and compile the information required to run local gradio applications

How does it work? 🤔

There are two major things that allow this to be possible in which will go into, and those are function wrapper, and the class wrapper.

What are wrapper/decorators? Decorators/wrappers is a powerful tool in python represented by a @ and position like in the example below

@nameofdecorator 
def Foo(*args, **kwargs):
    ...

that allow us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. How this tool is essential used is like gradio a Function has an input and output and as the user were able to define which function we want to convert into a UI. By using the decorators function the wrapper is able to store that information within a dictionary with the key representing the name of the function and the values being an dictionary of input and output holding the information you would put into your

Import gradio as gr
gr.interface(fn=Foo, inputs=[...], outputs=[...], ...)

inputs and outputs. From there the wrapper class initializer will call the compile function which would read the dictionary and put it in a tabular Interface which gradio provides.

Class Functional decorator

def register(inputs, outputs, examples=None):
    def register_gradio(func):
        def wrap(self, *args, **kwargs):            
            try:
                self.registered_gradio_functons
            except AttributeError:
                print("✨Initializing Class Functions...✨\n")
                self.registered_gradio_functons = dict()

            fn_name = func.__name__ 
            if fn_name in self.registered_gradio_functons: 
                result = func(self, *args, **kwargs)
                return result
            else:
                self.registered_gradio_functons[fn_name] = dict(inputs=inputs, outputs=outputs, examples=examples)
                return None
        return wrap
    return register_gradio

Class Decorator

def GradioCompiler(cls):
    class GradioWrapper:

        def __init__(self) -> None:
            self.cls = cls()


        def get_funcs(self):
            return [func for func in dir(self.cls) if not func.startswith("__") and type(getattr(self.cls, func, None)) == type(self.get_funcs) ]

        def compile(self, **kwargs):
            print("Just putting on the finishing touches... 🔧🧰")
            for func in self.get_funcs():
                this = getattr(self.cls, func, None)
                if this.__name__ == "wrap":
                    this()

            demos, names = [], []
            for func, param in self.get_registered_gradio_functons().items():                
                names.append(func)
                demos.append(gr.Interface(fn=getattr(self.cls, func, None),
                                            inputs=param['inputs'],
                                            outputs=param['outputs'],
                                            examples=param['examples'],
                                            cache_examples=kwargs['cache_examples'] if "cache_examples" in kwargs else None,
                                            examples_per_page=kwargs['cache_examples'] if "cache_examples" in kwargs else 10,
                                            interpretation=kwargs['interpretation'] if "interpretation" in kwargs else None,
                                            num_shap=kwargs['num_shap'] if "num_shap" in kwargs else 2.0,
                                            title=kwargs['title'] if "title" in kwargs else None,
                                            article=kwargs['article'] if "article" in kwargs else None,
                                            thumbnail=kwargs['thumbnail'] if "thumbnail" in kwargs else None,
                                            css=kwargs['css'] if "css" in kwargs else None,
                                            live=kwargs['live'] if "live" in kwargs else False,
                                            allow_flagging=kwargs['allow_flagging'] if "allow_flagging" in kwargs else None,
                                            theme='default', 
                                            ))
                print(f"{func}....{bcolor.BOLD}{bcolor.OKGREEN} done {bcolor.ENDC}")

            print("\nHappy Visualizing... 🚀")
            return gr.TabbedInterface(demos, names)
            
        def get_registered_gradio_functons(self):
            try:
                self.cls.registered_gradio_functons
            except AttributeError:
                return None
            return self.cls.registered_gradio_functons
        

        def run(self, **kwargs):
            port= kwargs["port"] if "port" in kwargs else DOCKER_PORT.determinePort() 

            self.compile(live=kwargs[ 'live' ] if "live" in kwargs else False,
                         allow_flagging=kwargs[ 'allow_flagging' ] if "allow_flagging" in kwargs else None,
                         cache_examples=kwargs['cache_examples'] if "cache_examples" in kwargs else None,
                         examples_per_page=kwargs['cache_examples'] if "cache_examples" in kwargs else 10,
                         interpretation=kwargs['interpretation'] if "interpretation" in kwargs else None,
                         num_shap=kwargs['num_shap'] if "num_shap" in kwargs else 2.0,
                         title=kwargs['title'] if "title" in kwargs else None,
                         article=kwargs['article'] if "article" in kwargs else None,
                         thumbnail=kwargs['thumbnail'] if "thumbnail" in kwargs else None,
                         css=kwargs['css'] if "css" in kwargs else None,
                         theme=kwargs['theme'] if "theme" in kwargs else None, 
                         ).launch(server_port=port,
                                  inline= kwargs['inline'] if "inline" in kwargs else True,
                                  share=kwargs['share'] if "share" in kwargs else None,
                                  debug=kwargs['debug'] if "debug" in kwargs else False,
                                  enable_queue=kwargs['enable_queue'] if "enable_queue" in kwargs else None,
                                  max_threads=kwargs['max_threads'] if "max_threads" in kwargs else None,
                                  auth=kwargs['auth'] if "auth" in kwargs else None,
                                  auth_message=kwargs['auth_message'] if "auth_message" in kwargs else None,
                                  prevent_thread_lock=kwargs['prevent_thread_lock'] if "prevent_thread_lock" in kwargs else False,
                                  show_error=kwargs['show_error'] if "show_error" in kwargs else True,
                                  show_tips=kwargs['show_tips'] if "show_tips" in kwargs else False,
                                  height=kwargs['height'] if "height" in kwargs else 500,
                                  width=kwargs['width'] if "width" in kwargs else 900,
                                  encrypt=kwargs['encrypt'] if "encrypt" in kwargs else False,
                                  favicon_path=kwargs['favicon_path'] if "favicon_path" in kwargs else None,
                                  ssl_keyfile=kwargs['ssl_keyfile'] if "ssl_keyfile" in kwargs else None,
                                  ssl_certfile=kwargs['ssl_certfile'] if "ssl_certfile" in kwargs else None,
                                  ssl_keyfile_password=kwargs['ssl_keyfile_password'] if "ssl_keyfile_password" in kwargs else None,
                                  quiet=kwargs['quiet'] if "quiet" in kwargs else False) 


    return GradioWrapper

Functional Decorator

def functionalCompiler(inputs, outputs, **kwargs):
    def register_func(func):
        def wrap():
            inter = gr.Interface(fn=func,
                                 inputs=inputs,
                                 outputs=outputs,
                                 examples=kwargs['examples'] if "examples" in kwargs else None,
                                 live=kwargs[ 'live' ] if "live" in kwargs else False,
                                 allow_flagging=kwargs[ 'allow_flagging' ] if "allow_flagging" in kwargs else None,
                                 cache_examples=kwargs['cache_examples'] if "cache_examples" in kwargs else None,
                                 examples_per_page=kwargs['cache_examples'] if "cache_examples" in kwargs else 10,
                                 interpretation=kwargs['interpretation'] if "interpretation" in kwargs else None,
                                 num_shap=kwargs['num_shap'] if "num_shap" in kwargs else 2.0,
                                 title=kwargs['title'] if "title" in kwargs else None,
                                 article=kwargs['article'] if "article" in kwargs else None,
                                 thumbnail=kwargs['thumbnail'] if "thumbnail" in kwargs else None,
                                 css=kwargs['css'] if "css" in kwargs else None,
                                 theme=kwargs['theme'] if "theme" in kwargs else None)
            return inter
        return wrap
    return register_func

Examples 🧪

Functional Example

@functionalCompiler(inputs=[gr.Textbox(label="name")], outputs=['text'])
def Hello_World(name):
        return f"Hello {name}, and welcome to Gradio Flow 🤗" 

@functionalCompiler(inputs=[gr.Textbox(label="name")], outputs=['text'])
def Goodbye(name):
        return f"Goodbye {name}" 

Class Example

@GradioCompiler
class Greeting:

    @register(inputs=[gr.Textbox(label="name")], outputs=['text'])
    def Hello_World(self, name):
        return f"Hello {name}, and welcome to Gradio Flow 🤗" 

    @register(inputs=[gr.Textbox(label="name")], outputs=['text'])
    def Goodbye(name):
        return f"Goodbye {name}" 

How To Run ⚙️

Run Class

# Greeting class from Class Example
###################################
a = Greeting()
a.run() # or a.run()

Run Singular Function

# HelloWorld function from Function Example
###################################
HelloWorld().launch()

Run Multiple Functions

# HelloWorld, and Goodbye function from Function Example
###################################
tabularGradio([Hello_World(),Goodbye()],["Hello World", "Goodbye"])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gradioWrapper-0.0.7.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gradioWrapper-0.0.7-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file gradioWrapper-0.0.7.tar.gz.

File metadata

  • Download URL: gradioWrapper-0.0.7.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for gradioWrapper-0.0.7.tar.gz
Algorithm Hash digest
SHA256 66a0df7c58385112813e97e6e7317569ad491d655f82fd0ae441750d67d66c49
MD5 cdcf3c69accefc5338febc6e6062ff69
BLAKE2b-256 9a79d3e80330d91e0ee90fe25c104a748adaebfa065364a29e23b839d305b174

See more details on using hashes here.

File details

Details for the file gradioWrapper-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: gradioWrapper-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for gradioWrapper-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b89aa53d38b359eec16126a9470369949872fac9ca8174432bbbde366853a6e9
MD5 9979674546f24ee4b500d9ba0082f67e
BLAKE2b-256 4e4c8047f642d4a545ab3d1f80d3e22c51d0d066b52ab4e5ea76b2f132be55f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page