Download handler.py from wordgen/multi-model-wordgen-2: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/wordgen/multi-model-wordgen-2/resolve/main/handler.py
- Command line
-
hf download hf://wordgen/multi-model-wordgen-2/handler.py
-
curl -L -o handler.py https://huggingface.co/wordgen/multi-model-wordgen-2/resolve/main/handler.py
1.55 kB
| # handler.py | |
| import torch | |
| from diffusers import DiffusionPipeline | |
| # check for GPU | |
| device = 0 if torch.cuda.is_available() else -1 | |
| # multi-model list | |
| multi_model_list = [ | |
| {"model_id": "dreamlike-art/dreamlike-photoreal-2.0", "task": "text-to-image"}, | |
| {"model_id": "Lykon/DreamShaper", "task": "text-to-image"}, | |
| {"model_id": "Envvi/Inkpunk-Diffusion", "task": "text-to-image"}, | |
| # Add other models if needed | |
| ] | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| self.multi_model = {} | |
| # load all the models | |
| for model in multi_model_list: | |
| self.multi_model[model["model_id"]] = DiffusionPipeline.from_pretrained(model["model_id"]) | |
| def __call__(self, data): | |
| # deserialize incoming request | |
| inputs = data.pop("inputs", data) | |
| parameters = data.pop("parameters", None) | |
| model_id = data.pop("model_id", None) | |
| # check if model_id is in the list of models | |
| if model_id is None or model_id not in self.multi_model: | |
| raise ValueError(f"model_id: {model_id} is not valid. Available models are: {list(self.multi_model.keys())}") | |
| # move the model to the appropriate device before making predictions | |
| self.multi_model[model_id].to(device) | |
| # pass inputs with all kwargs in data | |
| if parameters is not None: | |
| prediction = self.multi_model[model_id](inputs, **parameters) | |
| else: | |
| prediction = self.multi_model[model_id](inputs) | |
| # postprocess the prediction | |
| return prediction |