Package: gradioWrapper
gradioWrapper 🎁@Author Luca Vivona 🙈Githubgithub/LVivonaTable of contentsv0.0.7Quick Start ImportWhats New in v0.0.7What is it?How does it work?Class functional decoratorClass decoratorExamplesHow To RunQuick Start ImportfromgradioWrapperimportregister,GradioCompiler,functionalCompiler,tabularGradioWhats New in v0.0.7Genral Functional DecoratorGenral Tablular Functional DecoratorWhat 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 applicationsHow 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@nameofdecoratordefFoo(*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 yourImportgradioasgrgr.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 decoratordefregister(inputs,outputs,examples=None):defregister_gradio(func):defwrap(self,*args,**kwargs):try:self.registered_gradio_functonsexceptAttributeError:print("✨Initializing Class Functions...✨\n")self.registered_gradio_functons=dict()fn_name=func.__name__iffn_nameinself.registered_gradio_functons:result=func(self,*args,**kwargs)returnresultelse:self.registered_gradio_functons[fn_name]=dict(inputs=inputs,outputs=outputs,examples=examples)returnNonereturnwrapreturnregister_gradioClass DecoratordefGradioCompiler(cls):classGradioWrapper:def__init__(self)->None:self.cls=cls()defget_funcs(self):return[funcforfuncindir(self.cls)ifnotfunc.startswith("__")andtype(getattr(self.cls,func,None))==type(self.get_funcs)]defcompile(self,**kwargs):print("Just putting on the finishing touches... 🔧🧰")forfuncinself.get_funcs():this=getattr(self.cls,func,None)ifthis.__name__=="wrap":this()demos,names=[],[]forfunc,paraminself.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"inkwargselseNone,examples_per_page=kwargs['cache_examples']if"cache_examples"inkwargselse10,interpretation=kwargs['interpretation']if"interpretation"inkwargselseNone,num_shap=kwargs['num_shap']if"num_shap"inkwargselse2.0,title=kwargs['title']if"title"inkwargselseNone,article=kwargs['article']if"article"inkwargselseNone,thumbnail=kwargs['thumbnail']if"thumbnail"inkwargselseNone,css=kwargs['css']if"css"inkwargselseNone,live=kwargs['live']if"live"inkwargselseFalse,allow_flagging=kwargs['allow_flagging']if"allow_flagging"inkwargselseNone,theme='default',))print(f"{func}....{bcolor.BOLD}{bcolor.OKGREEN}done{bcolor.ENDC}")print("\nHappy Visualizing... 🚀")returngr.TabbedInterface(demos,names)defget_registered_gradio_functons(self):try:self.cls.registered_gradio_functonsexceptAttributeError:returnNonereturnself.cls.registered_gradio_functonsdefrun(self,**kwargs):port=kwargs["port"]if"port"inkwargselseDOCKER_PORT.determinePort()self.compile(live=kwargs['live']if"live"inkwargselseFalse,allow_flagging=kwargs['allow_flagging']if"allow_flagging"inkwargselseNone,cache_examples=kwargs['cache_examples']if"cache_examples"inkwargselseNone,examples_per_page=kwargs['cache_examples']if"cache_examples"inkwargselse10,interpretation=kwargs['interpretation']if"interpretation"inkwargselseNone,num_shap=kwargs['num_shap']if"num_shap"inkwargselse2.0,title=kwargs['title']if"title"inkwargselseNone,article=kwargs['article']if"article"inkwargselseNone,thumbnail=kwargs['thumbnail']if"thumbnail"inkwargselseNone,css=kwargs['css']if"css"inkwargselseNone,theme=kwargs['theme']if"theme"inkwargselseNone,).launch(server_port=port,inline=kwargs['inline']if"inline"inkwargselseTrue,share=kwargs['share']if"share"inkwargselseNone,debug=kwargs['debug']if"debug"inkwargselseFalse,enable_queue=kwargs['enable_queue']if"enable_queue"inkwargselseNone,max_threads=kwargs['max_threads']if"max_threads"inkwargselseNone,auth=kwargs['auth']if"auth"inkwargselseNone,auth_message=kwargs['auth_message']if"auth_message"inkwargselseNone,prevent_thread_lock=kwargs['prevent_thread_lock']if"prevent_thread_lock"inkwargselseFalse,show_error=kwargs['show_error']if"show_error"inkwargselseTrue,show_tips=kwargs['show_tips']if"show_tips"inkwargselseFalse,height=kwargs['height']if"height"inkwargselse500,width=kwargs['width']if"width"inkwargselse900,encrypt=kwargs['encrypt']if"encrypt"inkwargselseFalse,favicon_path=kwargs['favicon_path']if"favicon_path"inkwargselseNone,ssl_keyfile=kwargs['ssl_keyfile']if"ssl_keyfile"inkwargselseNone,ssl_certfile=kwargs['ssl_certfile']if"ssl_certfile"inkwargselseNone,ssl_keyfile_password=kwargs['ssl_keyfile_password']if"ssl_keyfile_password"inkwargselseNone,quiet=kwargs['quiet']if"quiet"inkwargselseFalse)returnGradioWrapperFunctional DecoratordeffunctionalCompiler(inputs,outputs,**kwargs):defregister_func(func):defwrap():inter=gr.Interface(fn=func,inputs=inputs,outputs=outputs,examples=kwargs['examples']if"examples"inkwargselseNone,live=kwargs['live']if"live"inkwargselseFalse,allow_flagging=kwargs['allow_flagging']if"allow_flagging"inkwargselseNone,cache_examples=kwargs['cache_examples']if"cache_examples"inkwargselseNone,examples_per_page=kwargs['cache_examples']if"cache_examples"inkwargselse10,interpretation=kwargs['interpretation']if"interpretation"inkwargselseNone,num_shap=kwargs['num_shap']if"num_shap"inkwargselse2.0,title=kwargs['title']if"title"inkwargselseNone,article=kwargs['article']if"article"inkwargselseNone,thumbnail=kwargs['thumbnail']if"thumbnail"inkwargselseNone,css=kwargs['css']if"css"inkwargselseNone,theme=kwargs['theme']if"theme"inkwargselseNone)returninterreturnwrapreturnregister_funcExamples 🧪Functional Example@functionalCompiler(inputs=[gr.Textbox(label="name")],outputs=['text'])defHello_World(name):returnf"Hello{name}, and welcome to Gradio Flow 🤗"@functionalCompiler(inputs=[gr.Textbox(label="name")],outputs=['text'])defGoodbye(name):returnf"Goodbye{name}"Class Example@GradioCompilerclassGreeting:@register(inputs=[gr.Textbox(label="name")],outputs=['text'])defHello_World(self,name):returnf"Hello{name}, and welcome to Gradio Flow 🤗"@register(inputs=[gr.Textbox(label="name")],outputs=['text'])defGoodbye(name):returnf"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"])