A little code modification

#7
by classicmaniagames - opened

Without meaning to be annoying, I assume that the code in your app.py has a reason for being like this but... would it be possible to change the loading of models in the 'models2' variable using a loop? Allow me a suggestion:

New optimized code begin

models2 = [
gr.Interface.load(f"models/{model}", live=True, preprocess=False) for model in models
]

New optimized code ends

I have tested it and it seems to work without problems, if you have doubts you can simply comment the old code using triple quotes before and after to check if it works, something like this:

Old repetitive code begin

"""
models2=[
gr.Interface.load(f"models/{models[0]}",live=True,preprocess=False),
gr.Interface.load(f"models/{models[1]}",live=True,preprocess=False),
gr.Interface.load(f"models/{models[2]}",live=True,preprocess=False),
gr.Interface.load(f"models/{models[3]}",live=True,preprocess=False),
...
#Because there's a model 0, to know the number of models you add 1 to {models[n]}
]
"""

Old repetitive code ends

A friendly greeting.

P.S: My apologies if the suggestion bothered you.

Thanks for the suggestion, what I'd want is a looping code like the one at my Diffusion60XX space:

https://huggingface.co/spaces/Yntec/Diffusion60XX/blob/main/app.py

Which isn't just a loop, it also makes all models load super fast so the big list doesn't take as long to fetch when building as in this space, so it's a big improvement on all fronts. Alas, when I tried to implement it I just couldn't make it work, and I don't have the knowledge to make the codes compatible, so that's the reason I don't have a loop, I want a loop that makes such a difference, so building this space can be done as fast as Diffusion60XX.

Well, I think you can use a text file with a model name on each line, then load it into a list and use that list to load the models according to the example I gave you.

Doing that is functionally equivalent to what I have now, there's no optimization and the entire list of models takes like 5 minutes to be fetched when building. The code that optimizes the loading of models with a loop from Diffusion60XX looks like this:

def load_fn(models):
global models_load
models_load = {}

for model in models:
    if model not in models_load.keys():
        try:
            m = gr.load(f'models/{model}')
        except Exception as error:
            m = gr.Interface(lambda txt: None, ['text'], ['image'])
        models_load.update({model: m})

load_fn(models)

And then they're loaded with the prompt at this part:

def gen_fn(model_str, prompt):
if model_str == 'NA':
return None
noise = str('') # This empty string can be changed to a random number to allow the user to generate multiple images that are different with the same prompt.
return models_load[model_str](f'{prompt} {noise}')

That's optimized code that makes building the space a very fast procedure, but when trying to implement it I got:

Runtime error:
module 'gradio' has no attribute 'load'

And I got stuck and that's why I didn't implement the loop. I need to figure a way to use gr.load(f'models/{model}') instead of gr.Interface.load(f'models/{model}') so the optimized code makes a difference.

Thanks for the help.

Sign up or log in to comment