Spaces:
Running
Running
Commit
·
c94b544
1
Parent(s):
4f9b231
:sparkles: :clown_face: init commit of little freaks
Browse files- .gitignore +162 -0
- README.md +6 -11
- app.py +427 -0
- requirements.txt +7 -0
- willpaint-imgs/chaosclicker-willpaint.png +0 -0
- willpaint-imgs/contentconnoisseur-willpaint.png +0 -0
- willpaint-imgs/digitaldaydreamer-willpaint.png +0 -0
- willpaint-imgs/ecoexplorer-willpaint.png +0 -0
- willpaint-imgs/fandomfox-willpaint.png +0 -0
- willpaint-imgs/mememaven-willpaint.png +0 -0
- willpaint-imgs/newnerd-willpaint.png +0 -0
- willpaint-imgs/nostalgicnavigator-willpaint.png +0 -0
- willpaint-imgs/scrollseeker-willpaint.png +0 -0
- willpaint-imgs/trendtracker-willpaint.png +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# poetry
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 102 |
+
#poetry.lock
|
| 103 |
+
|
| 104 |
+
# pdm
|
| 105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 106 |
+
#pdm.lock
|
| 107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 108 |
+
# in version control.
|
| 109 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
| 110 |
+
.pdm.toml
|
| 111 |
+
.pdm-python
|
| 112 |
+
.pdm-build/
|
| 113 |
+
|
| 114 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 115 |
+
__pypackages__/
|
| 116 |
+
|
| 117 |
+
# Celery stuff
|
| 118 |
+
celerybeat-schedule
|
| 119 |
+
celerybeat.pid
|
| 120 |
+
|
| 121 |
+
# SageMath parsed files
|
| 122 |
+
*.sage.py
|
| 123 |
+
|
| 124 |
+
# Environments
|
| 125 |
+
.env
|
| 126 |
+
.venv
|
| 127 |
+
env/
|
| 128 |
+
venv/
|
| 129 |
+
ENV/
|
| 130 |
+
env.bak/
|
| 131 |
+
venv.bak/
|
| 132 |
+
|
| 133 |
+
# Spyder project settings
|
| 134 |
+
.spyderproject
|
| 135 |
+
.spyproject
|
| 136 |
+
|
| 137 |
+
# Rope project settings
|
| 138 |
+
.ropeproject
|
| 139 |
+
|
| 140 |
+
# mkdocs documentation
|
| 141 |
+
/site
|
| 142 |
+
|
| 143 |
+
# mypy
|
| 144 |
+
.mypy_cache/
|
| 145 |
+
.dmypy.json
|
| 146 |
+
dmypy.json
|
| 147 |
+
|
| 148 |
+
# Pyre type checker
|
| 149 |
+
.pyre/
|
| 150 |
+
|
| 151 |
+
# pytype static type analyzer
|
| 152 |
+
.pytype/
|
| 153 |
+
|
| 154 |
+
# Cython debug symbols
|
| 155 |
+
cython_debug/
|
| 156 |
+
|
| 157 |
+
# PyCharm
|
| 158 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 159 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 160 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 161 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 162 |
+
#.idea/
|
README.md
CHANGED
|
@@ -1,14 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
license: apache-2.0
|
| 11 |
-
short_description: make little freaks at the click of a button!
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
title: little freaks gradio
|
| 2 |
+
emoji: :clown_face:
|
| 3 |
+
colorFrom: purple
|
| 4 |
+
colorTo: purple
|
|
|
|
| 5 |
sdk: gradio
|
| 6 |
+
sdk_version: 3.1.7
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
+
license: apache-2.0
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### BACKEND
|
| 2 |
+
import requests
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
from diffusers import StableUnCLIPImg2ImgPipeline, UnCLIPImageVariationPipeline, ImagePipelineOutput
|
| 8 |
+
|
| 9 |
+
import inspect
|
| 10 |
+
from typing import List, Optional, Union
|
| 11 |
+
|
| 12 |
+
import PIL.Image
|
| 13 |
+
import torch
|
| 14 |
+
from torch.nn import functional as F
|
| 15 |
+
from transformers import (
|
| 16 |
+
CLIPImageProcessor,
|
| 17 |
+
CLIPTextModelWithProjection,
|
| 18 |
+
CLIPTokenizer,
|
| 19 |
+
CLIPVisionModelWithProjection,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
import gradio as gr
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class customUnClipPipeline(UnCLIPImageVariationPipeline):
|
| 26 |
+
def _encode_prompt(self, prompt, device, num_images_per_prompt, do_classifier_free_guidance, negative_prompt = "",):
|
| 27 |
+
batch_size = len(prompt) if isinstance(prompt, list) else 1
|
| 28 |
+
|
| 29 |
+
# get prompt text embeddings
|
| 30 |
+
text_inputs = self.tokenizer(
|
| 31 |
+
prompt,
|
| 32 |
+
padding="max_length",
|
| 33 |
+
max_length=self.tokenizer.model_max_length,
|
| 34 |
+
return_tensors="pt",
|
| 35 |
+
)
|
| 36 |
+
text_input_ids = text_inputs.input_ids
|
| 37 |
+
text_mask = text_inputs.attention_mask.bool().to(device)
|
| 38 |
+
text_encoder_output = self.text_encoder(text_input_ids.to(device))
|
| 39 |
+
|
| 40 |
+
prompt_embeds = text_encoder_output.text_embeds
|
| 41 |
+
text_encoder_hidden_states = text_encoder_output.last_hidden_state
|
| 42 |
+
|
| 43 |
+
prompt_embeds = prompt_embeds.repeat_interleave(num_images_per_prompt, dim=0)
|
| 44 |
+
text_encoder_hidden_states = text_encoder_hidden_states.repeat_interleave(num_images_per_prompt, dim=0)
|
| 45 |
+
text_mask = text_mask.repeat_interleave(num_images_per_prompt, dim=0)
|
| 46 |
+
|
| 47 |
+
if do_classifier_free_guidance:
|
| 48 |
+
uncond_tokens = [negative_prompt] * batch_size
|
| 49 |
+
|
| 50 |
+
max_length = text_input_ids.shape[-1]
|
| 51 |
+
uncond_input = self.tokenizer(
|
| 52 |
+
uncond_tokens,
|
| 53 |
+
padding="max_length",
|
| 54 |
+
max_length=max_length,
|
| 55 |
+
truncation=True,
|
| 56 |
+
return_tensors="pt",
|
| 57 |
+
)
|
| 58 |
+
uncond_text_mask = uncond_input.attention_mask.bool().to(device)
|
| 59 |
+
negative_prompt_embeds_text_encoder_output = self.text_encoder(uncond_input.input_ids.to(device))
|
| 60 |
+
|
| 61 |
+
negative_prompt_embeds = negative_prompt_embeds_text_encoder_output.text_embeds
|
| 62 |
+
uncond_text_encoder_hidden_states = negative_prompt_embeds_text_encoder_output.last_hidden_state
|
| 63 |
+
|
| 64 |
+
# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
|
| 65 |
+
|
| 66 |
+
seq_len = negative_prompt_embeds.shape[1]
|
| 67 |
+
negative_prompt_embeds = negative_prompt_embeds.repeat(1, num_images_per_prompt)
|
| 68 |
+
negative_prompt_embeds = negative_prompt_embeds.view(batch_size * num_images_per_prompt, seq_len)
|
| 69 |
+
|
| 70 |
+
seq_len = uncond_text_encoder_hidden_states.shape[1]
|
| 71 |
+
uncond_text_encoder_hidden_states = uncond_text_encoder_hidden_states.repeat(1, num_images_per_prompt, 1)
|
| 72 |
+
uncond_text_encoder_hidden_states = uncond_text_encoder_hidden_states.view(
|
| 73 |
+
batch_size * num_images_per_prompt, seq_len, -1
|
| 74 |
+
)
|
| 75 |
+
uncond_text_mask = uncond_text_mask.repeat_interleave(num_images_per_prompt, dim=0)
|
| 76 |
+
|
| 77 |
+
# done duplicates
|
| 78 |
+
|
| 79 |
+
# For classifier free guidance, we need to do two forward passes.
|
| 80 |
+
# Here we concatenate the unconditional and text embeddings into a single batch
|
| 81 |
+
# to avoid doing two forward passes
|
| 82 |
+
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])
|
| 83 |
+
text_encoder_hidden_states = torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states])
|
| 84 |
+
|
| 85 |
+
text_mask = torch.cat([uncond_text_mask, text_mask])
|
| 86 |
+
|
| 87 |
+
return prompt_embeds, text_encoder_hidden_states, text_mask
|
| 88 |
+
|
| 89 |
+
@torch.no_grad()
|
| 90 |
+
def __call__(
|
| 91 |
+
self,
|
| 92 |
+
text_input: str = "",
|
| 93 |
+
negative_prompt: str = "",
|
| 94 |
+
image: Optional[Union[PIL.Image.Image, List[PIL.Image.Image], torch.Tensor]] = None,
|
| 95 |
+
num_images_per_prompt: int = 1,
|
| 96 |
+
decoder_num_inference_steps: int = 25,
|
| 97 |
+
super_res_num_inference_steps: int = 7,
|
| 98 |
+
generator: Optional[torch.Generator] = None,
|
| 99 |
+
decoder_latents: Optional[torch.Tensor] = None,
|
| 100 |
+
super_res_latents: Optional[torch.Tensor] = None,
|
| 101 |
+
image_embeddings: Optional[torch.Tensor] = None,
|
| 102 |
+
decoder_guidance_scale: float = 8.0,
|
| 103 |
+
output_type: Optional[str] = "pil",
|
| 104 |
+
return_dict: bool = True,
|
| 105 |
+
):
|
| 106 |
+
"""
|
| 107 |
+
The call function to the pipeline for generation.
|
| 108 |
+
|
| 109 |
+
Args:
|
| 110 |
+
image (`PIL.Image.Image` or `List[PIL.Image.Image]` or `torch.Tensor`):
|
| 111 |
+
`Image` or tensor representing an image batch to be used as the starting point. If you provide a
|
| 112 |
+
tensor, it needs to be compatible with the [`CLIPImageProcessor`]
|
| 113 |
+
[configuration](https://huggingface.co/fusing/karlo-image-variations-diffusers/blob/main/feature_extractor/preprocessor_config.json).
|
| 114 |
+
Can be left as `None` only when `image_embeddings` are passed.
|
| 115 |
+
num_images_per_prompt (`int`, *optional*, defaults to 1):
|
| 116 |
+
The number of images to generate per prompt.
|
| 117 |
+
decoder_num_inference_steps (`int`, *optional*, defaults to 25):
|
| 118 |
+
The number of denoising steps for the decoder. More denoising steps usually lead to a higher quality
|
| 119 |
+
image at the expense of slower inference.
|
| 120 |
+
super_res_num_inference_steps (`int`, *optional*, defaults to 7):
|
| 121 |
+
The number of denoising steps for super resolution. More denoising steps usually lead to a higher
|
| 122 |
+
quality image at the expense of slower inference.
|
| 123 |
+
generator (`torch.Generator`, *optional*):
|
| 124 |
+
A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
|
| 125 |
+
generation deterministic.
|
| 126 |
+
decoder_latents (`torch.Tensor` of shape (batch size, channels, height, width), *optional*):
|
| 127 |
+
Pre-generated noisy latents to be used as inputs for the decoder.
|
| 128 |
+
super_res_latents (`torch.Tensor` of shape (batch size, channels, super res height, super res width), *optional*):
|
| 129 |
+
Pre-generated noisy latents to be used as inputs for the decoder.
|
| 130 |
+
decoder_guidance_scale (`float`, *optional*, defaults to 4.0):
|
| 131 |
+
A higher guidance scale value encourages the model to generate images closely linked to the text
|
| 132 |
+
`prompt` at the expense of lower image quality. Guidance scale is enabled when `guidance_scale > 1`.
|
| 133 |
+
image_embeddings (`torch.Tensor`, *optional*):
|
| 134 |
+
Pre-defined image embeddings that can be derived from the image encoder. Pre-defined image embeddings
|
| 135 |
+
can be passed for tasks like image interpolations. `image` can be left as `None`.
|
| 136 |
+
output_type (`str`, *optional*, defaults to `"pil"`):
|
| 137 |
+
The output format of the generated image. Choose between `PIL.Image` or `np.array`.
|
| 138 |
+
return_dict (`bool`, *optional*, defaults to `True`):
|
| 139 |
+
Whether or not to return a [`~pipelines.ImagePipelineOutput`] instead of a plain tuple.
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
[`~pipelines.ImagePipelineOutput`] or `tuple`:
|
| 143 |
+
If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is
|
| 144 |
+
returned where the first element is a list with the generated images.
|
| 145 |
+
"""
|
| 146 |
+
if image is not None:
|
| 147 |
+
if isinstance(image, PIL.Image.Image):
|
| 148 |
+
batch_size = 1
|
| 149 |
+
elif isinstance(image, list):
|
| 150 |
+
batch_size = len(image)
|
| 151 |
+
else:
|
| 152 |
+
batch_size = image.shape[0]
|
| 153 |
+
else:
|
| 154 |
+
batch_size = image_embeddings.shape[0]
|
| 155 |
+
|
| 156 |
+
prompt = [text_input] * batch_size
|
| 157 |
+
|
| 158 |
+
device = self._execution_device
|
| 159 |
+
|
| 160 |
+
batch_size = batch_size * num_images_per_prompt
|
| 161 |
+
|
| 162 |
+
do_classifier_free_guidance = decoder_guidance_scale > 1.0
|
| 163 |
+
|
| 164 |
+
prompt_embeds, text_encoder_hidden_states, text_mask = self._encode_prompt(
|
| 165 |
+
prompt, device, num_images_per_prompt, do_classifier_free_guidance, negative_prompt ,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
image_embeddings = self._encode_image(image, device, num_images_per_prompt, image_embeddings)
|
| 169 |
+
|
| 170 |
+
# decoder
|
| 171 |
+
text_encoder_hidden_states, additive_clip_time_embeddings = self.text_proj(
|
| 172 |
+
image_embeddings=image_embeddings,
|
| 173 |
+
prompt_embeds=prompt_embeds,
|
| 174 |
+
text_encoder_hidden_states=text_encoder_hidden_states,
|
| 175 |
+
do_classifier_free_guidance=do_classifier_free_guidance,
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
if device.type == "mps":
|
| 179 |
+
# HACK: MPS: There is a panic when padding bool tensors,
|
| 180 |
+
# so cast to int tensor for the pad and back to bool afterwards
|
| 181 |
+
text_mask = text_mask.type(torch.int)
|
| 182 |
+
decoder_text_mask = F.pad(text_mask, (self.text_proj.clip_extra_context_tokens, 0), value=1)
|
| 183 |
+
decoder_text_mask = decoder_text_mask.type(torch.bool)
|
| 184 |
+
else:
|
| 185 |
+
decoder_text_mask = F.pad(text_mask, (self.text_proj.clip_extra_context_tokens, 0), value=True)
|
| 186 |
+
|
| 187 |
+
self.decoder_scheduler.set_timesteps(decoder_num_inference_steps, device=device)
|
| 188 |
+
decoder_timesteps_tensor = self.decoder_scheduler.timesteps
|
| 189 |
+
|
| 190 |
+
num_channels_latents = self.decoder.config.in_channels
|
| 191 |
+
height = self.decoder.config.sample_size
|
| 192 |
+
width = self.decoder.config.sample_size
|
| 193 |
+
|
| 194 |
+
if decoder_latents is None:
|
| 195 |
+
decoder_latents = self.prepare_latents(
|
| 196 |
+
(batch_size, num_channels_latents, height, width),
|
| 197 |
+
text_encoder_hidden_states.dtype,
|
| 198 |
+
device,
|
| 199 |
+
generator,
|
| 200 |
+
decoder_latents,
|
| 201 |
+
self.decoder_scheduler,
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
for i, t in enumerate(self.progress_bar(decoder_timesteps_tensor)):
|
| 205 |
+
# expand the latents if we are doing classifier free guidance
|
| 206 |
+
latent_model_input = torch.cat([decoder_latents] * 2) if do_classifier_free_guidance else decoder_latents
|
| 207 |
+
|
| 208 |
+
noise_pred = self.decoder(
|
| 209 |
+
sample=latent_model_input,
|
| 210 |
+
timestep=t,
|
| 211 |
+
encoder_hidden_states=text_encoder_hidden_states,
|
| 212 |
+
class_labels=additive_clip_time_embeddings,
|
| 213 |
+
attention_mask=decoder_text_mask,
|
| 214 |
+
).sample
|
| 215 |
+
|
| 216 |
+
if do_classifier_free_guidance:
|
| 217 |
+
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
|
| 218 |
+
noise_pred_uncond, _ = noise_pred_uncond.split(latent_model_input.shape[1], dim=1)
|
| 219 |
+
noise_pred_text, predicted_variance = noise_pred_text.split(latent_model_input.shape[1], dim=1)
|
| 220 |
+
noise_pred = noise_pred_uncond + decoder_guidance_scale * (noise_pred_text - noise_pred_uncond)
|
| 221 |
+
noise_pred = torch.cat([noise_pred, predicted_variance], dim=1)
|
| 222 |
+
|
| 223 |
+
if i + 1 == decoder_timesteps_tensor.shape[0]:
|
| 224 |
+
prev_timestep = None
|
| 225 |
+
else:
|
| 226 |
+
prev_timestep = decoder_timesteps_tensor[i + 1]
|
| 227 |
+
|
| 228 |
+
# compute the previous noisy sample x_t -> x_t-1
|
| 229 |
+
decoder_latents = self.decoder_scheduler.step(
|
| 230 |
+
noise_pred, t, decoder_latents, prev_timestep=prev_timestep, generator=generator
|
| 231 |
+
).prev_sample
|
| 232 |
+
|
| 233 |
+
decoder_latents = decoder_latents.clamp(-1, 1)
|
| 234 |
+
|
| 235 |
+
image_small = decoder_latents
|
| 236 |
+
|
| 237 |
+
# done decoder
|
| 238 |
+
|
| 239 |
+
# super res
|
| 240 |
+
|
| 241 |
+
self.super_res_scheduler.set_timesteps(super_res_num_inference_steps, device=device)
|
| 242 |
+
super_res_timesteps_tensor = self.super_res_scheduler.timesteps
|
| 243 |
+
|
| 244 |
+
channels = self.super_res_first.config.in_channels // 2
|
| 245 |
+
height = self.super_res_first.config.sample_size
|
| 246 |
+
width = self.super_res_first.config.sample_size
|
| 247 |
+
|
| 248 |
+
if super_res_latents is None:
|
| 249 |
+
super_res_latents = self.prepare_latents(
|
| 250 |
+
(batch_size, channels, height, width),
|
| 251 |
+
image_small.dtype,
|
| 252 |
+
device,
|
| 253 |
+
generator,
|
| 254 |
+
super_res_latents,
|
| 255 |
+
self.super_res_scheduler,
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
if device.type == "mps":
|
| 259 |
+
# MPS does not support many interpolations
|
| 260 |
+
image_upscaled = F.interpolate(image_small, size=[height, width])
|
| 261 |
+
else:
|
| 262 |
+
interpolate_antialias = {}
|
| 263 |
+
if "antialias" in inspect.signature(F.interpolate).parameters:
|
| 264 |
+
interpolate_antialias["antialias"] = True
|
| 265 |
+
|
| 266 |
+
image_upscaled = F.interpolate(
|
| 267 |
+
image_small, size=[height, width], mode="bicubic", align_corners=False, **interpolate_antialias
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
for i, t in enumerate(self.progress_bar(super_res_timesteps_tensor)):
|
| 271 |
+
# no classifier free guidance
|
| 272 |
+
|
| 273 |
+
if i == super_res_timesteps_tensor.shape[0] - 1:
|
| 274 |
+
unet = self.super_res_last
|
| 275 |
+
else:
|
| 276 |
+
unet = self.super_res_first
|
| 277 |
+
|
| 278 |
+
latent_model_input = torch.cat([super_res_latents, image_upscaled], dim=1)
|
| 279 |
+
|
| 280 |
+
noise_pred = unet(
|
| 281 |
+
sample=latent_model_input,
|
| 282 |
+
timestep=t,
|
| 283 |
+
).sample
|
| 284 |
+
|
| 285 |
+
if i + 1 == super_res_timesteps_tensor.shape[0]:
|
| 286 |
+
prev_timestep = None
|
| 287 |
+
else:
|
| 288 |
+
prev_timestep = super_res_timesteps_tensor[i + 1]
|
| 289 |
+
|
| 290 |
+
# compute the previous noisy sample x_t -> x_t-1
|
| 291 |
+
super_res_latents = self.super_res_scheduler.step(
|
| 292 |
+
noise_pred, t, super_res_latents, prev_timestep=prev_timestep, generator=generator
|
| 293 |
+
).prev_sample
|
| 294 |
+
|
| 295 |
+
image = super_res_latents
|
| 296 |
+
|
| 297 |
+
# done super res
|
| 298 |
+
self.maybe_free_model_hooks()
|
| 299 |
+
|
| 300 |
+
# post processing
|
| 301 |
+
|
| 302 |
+
image = image * 0.5 + 0.5
|
| 303 |
+
image = image.clamp(0, 1)
|
| 304 |
+
image = image.cpu().permute(0, 2, 3, 1).float().numpy()
|
| 305 |
+
|
| 306 |
+
if output_type == "pil":
|
| 307 |
+
image = self.numpy_to_pil(image)
|
| 308 |
+
|
| 309 |
+
if not return_dict:
|
| 310 |
+
return (image,)
|
| 311 |
+
|
| 312 |
+
return ImagePipelineOutput(images=image)
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
### ADDITIONAL PIPELINE CODE FOR KARLO
|
| 316 |
+
pipe = customUnClipPipeline.from_pretrained("kakaobrain/karlo-v1-alpha-image-variations", torch_dtype=torch.float16, trust_remote_code=True, accelerator='ort', device='cpu')
|
| 317 |
+
# pipe.enable_model_cpu_offload()
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
# func for getting tensor embeddings from cand image
|
| 321 |
+
|
| 322 |
+
def load_image(image_dir):
|
| 323 |
+
image = Image.open(image_dir).convert("RGB")
|
| 324 |
+
return image
|
| 325 |
+
|
| 326 |
+
def load_img_from_URL(URL):
|
| 327 |
+
response = requests.get(URL)
|
| 328 |
+
init_image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 329 |
+
return init_image
|
| 330 |
+
|
| 331 |
+
def embed_img(input_image):
|
| 332 |
+
tokens = pipe.feature_extractor(input_image)
|
| 333 |
+
img_model = pipe.image_encoder.to('cpu')
|
| 334 |
+
with torch.no_grad():
|
| 335 |
+
embeds = img_model(torch.tensor(tokens.pixel_values[0]).unsqueeze(0))
|
| 336 |
+
|
| 337 |
+
return embeds.image_embeds.to('cpu')
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def localimg_2_embed(image_dir):
|
| 341 |
+
embeds = embed_img(load_image(image_dir))
|
| 342 |
+
return embeds
|
| 343 |
+
|
| 344 |
+
def URLimg_2_embed(URL):
|
| 345 |
+
embeds = embed_img(load_img_from_URL(URL))
|
| 346 |
+
return embeds
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
# random generator for softmaxxed outputs
|
| 350 |
+
|
| 351 |
+
def random_probdist(num_cands):
|
| 352 |
+
random_numbers = torch.randn(num_cands)
|
| 353 |
+
softmax_output = torch.nn.functional.softmax(random_numbers, dim=0).reshape((num_cands,1))
|
| 354 |
+
return softmax_output
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
def scalesum_candtensors(list_scale, cand_tensors):
|
| 358 |
+
'''
|
| 359 |
+
quick note - just make sure your list_scale is the same length as ur cand_tensors, and also adds up to 1
|
| 360 |
+
'''
|
| 361 |
+
assert sum(list_scale) == 1, f"you didn't input a valid probability distribution - make sure your scales add up to 1, currently it adds up to {sum(list_scale)}"
|
| 362 |
+
assert len(list_scale) == len(cand_tensors), f"your scale list is not the same length as your list of candidate tensors. len list = {len(list_scale)}, len cand tensors = {len(cand_tensors)}"
|
| 363 |
+
|
| 364 |
+
scaled = torch.tensor(list_scale), cand_tensors
|
| 365 |
+
output = scaled.sum(dim=0)
|
| 366 |
+
return output
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
def random_candtensor(cand_tensors):
|
| 370 |
+
scaled = random_probdist(len(cand_tensors)) * cand_tensors
|
| 371 |
+
output = scaled.sum(dim=0)
|
| 372 |
+
return output
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
# for displaying images
|
| 376 |
+
def image_grid(imgs, rows, cols):
|
| 377 |
+
assert len(imgs) == rows*cols
|
| 378 |
+
|
| 379 |
+
w, h = imgs[0].size
|
| 380 |
+
grid = Image.new('RGB', size=(cols*w, rows*h))
|
| 381 |
+
grid_w, grid_h = grid.size
|
| 382 |
+
|
| 383 |
+
for i, img in enumerate(imgs):
|
| 384 |
+
grid.paste(img, box=(i%cols*w, i//cols*h))
|
| 385 |
+
return grid
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
chaosclicker_willtensor = localimg_2_embed('willpaint-imgs/chaosclicker-willpaint.png').to('cpu')
|
| 389 |
+
contentcnsr_willtensor = localimg_2_embed('willpaint-imgs/contentconnoisseur-willpaint.png').to('cpu')
|
| 390 |
+
digdaydrmr_willtensor = localimg_2_embed('willpaint-imgs/digitaldaydreamer-willpaint.png').to('cpu')
|
| 391 |
+
ecoexplr_willtensor = localimg_2_embed('willpaint-imgs/ecoexplorer-willpaint.png').to('cpu')
|
| 392 |
+
fandomfox_willtensor = localimg_2_embed('willpaint-imgs/fandomfox-willpaint.png').to('cpu')
|
| 393 |
+
mememaven_willtensor = localimg_2_embed('willpaint-imgs/mememaven-willpaint.png').to('cpu')
|
| 394 |
+
newsnerd_willtensor = localimg_2_embed('willpaint-imgs/newnerd-willpaint.png').to('cpu')
|
| 395 |
+
nostalgicnvgtr_willtensor = localimg_2_embed('willpaint-imgs/nostalgicnavigator-willpaint.png').to('cpu')
|
| 396 |
+
scrollseeker_willtensor = localimg_2_embed('willpaint-imgs/scrollseeker-willpaint.png').to('cpu')
|
| 397 |
+
trendtracker_willtensor = localimg_2_embed('willpaint-imgs/trendtracker-willpaint.png').to('cpu')
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
will_cand_tensors = torch.cat([chaosclicker_willtensor,
|
| 401 |
+
contentcnsr_willtensor ,
|
| 402 |
+
digdaydrmr_willtensor,
|
| 403 |
+
ecoexplr_willtensor,
|
| 404 |
+
fandomfox_willtensor,
|
| 405 |
+
mememaven_willtensor,
|
| 406 |
+
newsnerd_willtensor,
|
| 407 |
+
nostalgicnvgtr_willtensor,
|
| 408 |
+
scrollseeker_willtensor,
|
| 409 |
+
trendtracker_willtensor,], dim=0)
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
### FUNCTION FOR EXECUTION
|
| 414 |
+
|
| 415 |
+
def generate_freak():
|
| 416 |
+
will_randomised_input = random_candtensor(will_cand_tensors).unsqueeze(0).half()
|
| 417 |
+
output = pipe(image_embeddings=will_randomised_input, num_images_per_prompt=1)
|
| 418 |
+
return output.images[0]
|
| 419 |
+
|
| 420 |
+
### GRADIO BACKEND
|
| 421 |
+
gr.Interface(
|
| 422 |
+
generate_freak,
|
| 423 |
+
inputs=None,
|
| 424 |
+
outputs=gr.Image(),
|
| 425 |
+
title="Make a little freak!",
|
| 426 |
+
description="click the button and make a freak!"
|
| 427 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
diffusers
|
| 4 |
+
onnx
|
| 5 |
+
onnxruntime
|
| 6 |
+
gradio
|
| 7 |
+
pillow
|
willpaint-imgs/chaosclicker-willpaint.png
ADDED
|
willpaint-imgs/contentconnoisseur-willpaint.png
ADDED
|
willpaint-imgs/digitaldaydreamer-willpaint.png
ADDED
|
willpaint-imgs/ecoexplorer-willpaint.png
ADDED
|
willpaint-imgs/fandomfox-willpaint.png
ADDED
|
willpaint-imgs/mememaven-willpaint.png
ADDED
|
willpaint-imgs/newnerd-willpaint.png
ADDED
|
willpaint-imgs/nostalgicnavigator-willpaint.png
ADDED
|
willpaint-imgs/scrollseeker-willpaint.png
ADDED
|
willpaint-imgs/trendtracker-willpaint.png
ADDED
|
|