Using the prompt to switch model
Hello,I am new to gradio and tried to  implement a way to switch models by identifying keyword inside the promptseehuggingface.coapp.py · AlStable/stable-diffusion-2-1 at mainWe’re on a journey to advance and democratize artificial intelligence through open source and open science.the problem  seems to be that whatever is selectedit is ignored by gradioCan  someone explain, what is wrong in my codeand if this is feasible or not?Thanks
2023-01-21T02:33:44Z
Hi@blogclif, you can’t usegr.Interface.load()with afn, you can only load a single model at a time. However, it is possible to do what you have in mind by noting the fact that an a model loaded withgr.Interface.load()can be used as a function. So you could do something like this:models = [
 gr.Interface.load("model1name"),
 gr.Interface.load("model2name")
 ...
]

def prediction(model_choice, input):
  model = models[model_choice]
  output = model(input)
  return output = model

gr.Interface(fn=prediciton, inputs=[gr.Dropdown(["model1name", "model2name"], type="index"), gr.Textbox()], outputs=gr.Image()].launch()This is just a code skeleton but let me know if it makes sense!
2023-01-21T16:55:57Z
Thanks a lot for looking into itI will try, I like the fact that the prompt was including the model in a text format ; that seemed really transparent, but having to select the model via a menu list is surely better, and more reliablethank a lot
2023-01-23T19:14:25Z
hi, this is a cool example using this approachapp.py · Omnibus/maximum_diffusion at a451648ba3f1b4e498072f0e4a462d4c265fe713from this projectMaximum Diffusion - a Hugging Face Space by Omnibuscurrently down but I’ve ping@Omnibusto look at the error.
2023-01-24T15:24:36Z
Thanks  a lot for the help, I am writing some more codeyesterday and todaylet’s see what will work best.I will most likely go in the direction of a dropdown menu ; as I understand now that extracting the model from the prompt is not accepted by gradio, because gradio needs two variables, and won’t accept the second variable after it has been called.I guess, that was the misunderstanding I had at the very beginning
2023-01-24T17:29:54Z
@OmnibusI did some more testing and found another user who had the interface I was looking forinterface-al-stable1710×493 22.9 KBthere is a new problem nowThis error is popping upOSError: We couldn’t connect to ‘https://huggingface.co’ to load this model, couldn’t find it in the cached files and it looks like models/ItsJayQz/Marvel_WhatIf_Diffusion is not the path to a directory containing a model_index.json file.for all the models
2023-01-24T17:48:34Z
will try, here was the link to the other space that had the interface I was trying to have in the first place.huggingface.coText To Image AllModels - a Hugging Face Space by JoeythemonsterDiscover amazing ML apps made by the communityI left a comment in his discussion thread
2023-01-24T17:59:49Z
Feels like your solution above is workingremoving the “models/” and using get_default_dtypenow in the log I seeDownloading:  71%|███████   | 2.47G/3.46G [00:35<00:18, 54.4MB/s]it still shows an ‘Error’ in the endinterface-al-stable1751×493 28.4 KBand the logs are still scrolling … I can’t really get an error this time
2023-01-24T18:19:19Z
This is what I have:Cannot initialize model with low cpu memory usage becauseacceleratewas not found in the environment. Defaulting tolow_cpu_mem_usage=False. It is strongly recommended to installacceleratefor faster and less memory-intense model loading. You can do so with:pip install accelerate.Traceback (most recent call last):File “/home/user/.local/lib/python3.8/site-packages/gradio/routes.py”, line 321, in run_predictoutput = await app.blocks.process_api(File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 1015, in process_apiresult = await self.call_function(fn_index, inputs, iterator, request)File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 856, in call_functionprediction = await anyio.to_thread.run_sync(File “/home/user/.local/lib/python3.8/site-packages/anyio/to_thread.py”, line 31, in run_syncreturn await get_asynclib().run_sync_in_worker_thread(File “/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py”, line 937, in run_sync_in_worker_threadreturn await futureFile “/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py”, line 867, in runresult = context.run(func, *args)File “app.py”, line 18, in TextToImagepipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.get_default_dtype)File “/home/user/.local/lib/python3.8/site-packages/diffusers/pipeline_utils.py”, line 708, in from_pretrainedloaded_sub_model = load_method(os.path.join(cached_folder, name), **loading_kwargs)File “/home/user/.local/lib/python3.8/site-packages/transformers/modeling_utils.py”, line 2325, in from_pretraineddtype_orig = cls._set_default_torch_dtype(torch_dtype)File “/home/user/.local/lib/python3.8/site-packages/transformers/modeling_utils.py”, line 1102, in _set_default_torch_dtypeif not dtype.is_floating_point:AttributeError: ‘builtin_function_or_method’ object has no attribute ‘is_floating_point’Traceback (most recent call last):File “/home/user/.local/lib/python3.8/site-packages/gradio/routes.py”, line 321, in run_predictoutput = await app.blocks.process_api(File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 1013, in process_apiinputs = self.preprocess_data(fn_index, inputs, state)File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 923, in preprocess_dataprocessed_input.append(block.preprocess(inputs[i]))IndexError: list index out of rangeTraceback (most recent call last):File “/home/user/.local/lib/python3.8/site-packages/diffusers/configuration_utils.py”, line 326, in load_configconfig_file = REDACTED_TOKEN_download(File “/home/user/.local/lib/python3.8/site-packages/huggingface_hub/utils/_validators.py”, line 114, in _inner_fnvalidate_repo_id(arg_value)File “/home/user/.local/lib/python3.8/site-packages/huggingface_hub/utils/_validators.py”, line 172, in validate_repo_idraise HFValidationError(huggingface_hub.utils.validators.HFValidationError: Repo id must use alphanumeric chars or ‘-’, '', ‘.’, ‘–’ and ‘…’ are forbidden, ‘-’ and ‘.’ cannot start or end the name, max length is 96: ‘’.During handling of the above exception, another exception occurred:Traceback (most recent call last):File “/home/user/.local/lib/python3.8/site-packages/gradio/routes.py”, line 321, in run_predictoutput = await app.blocks.process_api(File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 1015, in process_apiresult = await self.call_function(fn_index, inputs, iterator, request)File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 856, in call_functionprediction = await anyio.to_thread.run_sync(File “/home/user/.local/lib/python3.8/site-packages/anyio/to_thread.py”, line 31, in run_syncreturn await get_asynclib().run_sync_in_worker_thread(File “/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py”, line 937, in run_sync_in_worker_threadreturn await futureFile “/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py”, line 867, in runresult = context.run(func, *args)File “app.py”, line 18, in TextToImagepipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.get_default_dtype)File “/home/user/.local/lib/python3.8/site-packages/diffusers/pipeline_utils.py”, line 459, in from_pretrainedconfig_dict = cls.load_config(File “/home/user/.local/lib/python3.8/site-packages/diffusers/configuration_utils.py”, line 363, in load_configraise EnvironmentError(OSError: We couldn’t connect to ‘https://huggingface.co’ to load this model, couldn’t find it in the cached files and it looks like  is not the path to a directory containing a model_index.json file.Checkout your internet connection or see how to run the library in offline mode at ‘Installation’.To get this error  I left the model as ‘blank’
2023-01-25T08:33:57Z
@OmnibusOh well, this time the generating part never endsthere are no error it just won’t finishI give up
2023-01-25T09:54:59Z
Thanks again, for your assistance,I let it run for 3 hours and just closed the tab (after taking this screenshot)workin-11591×521 38.8 KBthe logs are showing a lot of downloads and sometime I see thisFetching 16 files: 100%|██████████| 16/16 [02:48<00:00, 10.53s/it]/home/user/.local/lib/python3.8/site-packages/transformers/models/clip/feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.warnings.warn(
2023-01-25T10:00:56Z
Omnibus:Gradio.Interface.load(“models/Repo/Name”)if this would load the models once and for all?that could really speed up the process
2023-01-25T10:12:55Z
Thanks a lot, I will use this new code aboveand the solution provided by@abidlabsHopefully, this will work faster
2023-01-25T14:24:36Z
looks promising, I am not having the version working yetI made the space private in the meantime to get faster resultWill make the space public once I’m done
2023-01-25T15:08:18Z
I am finishing up, but it works nowThanks a lot for your help, I am glad this is resolvedand I learned a lot in the last 2 days thanks to youHere is the final version:https://alstable-marvel.hf.spaceAs a sidenote - I had to get rid of one the styleFetching model from:DGSpitzer/Guan-Yu-Diffusion · Hugging Faceas it was giving an errorValueError: Unsupported pipeline type: NoneThis thread can now be closed
2023-01-25T15:24:43Z
here is a fresh exampleblackwidow1512×512 28.1 KB