Delete app.py
Browse files
app.py
DELETED
@@ -1,125 +0,0 @@
|
|
1 |
-
#@title
|
2 |
-
import numpy as np
|
3 |
-
import gradio as gr
|
4 |
-
|
5 |
-
import argparse
|
6 |
-
import itertools
|
7 |
-
import math
|
8 |
-
import os
|
9 |
-
import random
|
10 |
-
|
11 |
-
import numpy as np
|
12 |
-
import torch
|
13 |
-
import torch.nn.functional as F
|
14 |
-
import torch.utils.checkpoint
|
15 |
-
from torch.utils.data import Dataset
|
16 |
-
|
17 |
-
import PIL
|
18 |
-
from diffusers import StableDiffusionImg2ImgPipeline # 修正箇所
|
19 |
-
from diffusers import AutoencoderKL, DDPMScheduler, PNDMScheduler, UNet2DConditionModel
|
20 |
-
from diffusers.hub_utils import init_git_repo, push_to_hub
|
21 |
-
from diffusers.optimization import get_scheduler
|
22 |
-
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
|
23 |
-
from PIL import Image
|
24 |
-
from torchvision import transforms
|
25 |
-
from tqdm.auto import tqdm
|
26 |
-
from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
|
27 |
-
|
28 |
-
MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
|
29 |
-
YOUR_TOKEN=MY_SECRET_TOKEN
|
30 |
-
device="cpu"
|
31 |
-
|
32 |
-
pretrained_model_name_or_path = "CompVis/stable-diffusion-v1-4" #@param {type:"string"}
|
33 |
-
|
34 |
-
from IPython.display import Markdown
|
35 |
-
from huggingface_hub import hf_hub_download
|
36 |
-
#@title Load your concept here
|
37 |
-
#@markdown Enter the `repo_id` for a concept you like (you can find pre-learned concepts in the public [SD Concepts Library](https://huggingface.co/sd-concepts-library))
|
38 |
-
repo_id_embeds = "sd-concepts-library/mikako-methodi2i" #@param {type:"string"}
|
39 |
-
|
40 |
-
def image_grid(imgs, rows, cols):
|
41 |
-
assert len(imgs) == rows*cols
|
42 |
-
|
43 |
-
w, h = imgs[0].size
|
44 |
-
grid = Image.new('RGB', size=(cols*w, rows*h))
|
45 |
-
grid_w, grid_h = grid.size
|
46 |
-
|
47 |
-
for i, img in enumerate(imgs):
|
48 |
-
grid.paste(img, box=(i%cols*w, i//cols*h))
|
49 |
-
return grid
|
50 |
-
|
51 |
-
#@title Set up the Tokenizer and the Text Encoder
|
52 |
-
tokenizer = CLIPTokenizer.from_pretrained(
|
53 |
-
pretrained_model_name_or_path,
|
54 |
-
subfolder="tokenizer",
|
55 |
-
use_auth_token=YOUR_TOKEN,
|
56 |
-
)
|
57 |
-
text_encoder = CLIPTextModel.from_pretrained(
|
58 |
-
pretrained_model_name_or_path, subfolder="text_encoder", use_auth_token=YOUR_TOKEN
|
59 |
-
)
|
60 |
-
|
61 |
-
#@title Load the newly learned embeddings into CLIP
|
62 |
-
def load_learned_embed_in_clip(learned_embeds_path, text_encoder, tokenizer, token=None):
|
63 |
-
loaded_learned_embeds = torch.load(learned_embeds_path, map_location="cpu")
|
64 |
-
|
65 |
-
# separate token and the embeds
|
66 |
-
trained_token = list(loaded_learned_embeds.keys())[0]
|
67 |
-
embeds = loaded_learned_embeds[trained_token]
|
68 |
-
|
69 |
-
# cast to dtype of text_encoder
|
70 |
-
dtype = text_encoder.get_input_embeddings().weight.dtype
|
71 |
-
embeds.to(dtype)
|
72 |
-
|
73 |
-
# add the token in tokenizer
|
74 |
-
token = token if token is not None else trained_token
|
75 |
-
num_added_tokens = tokenizer.add_tokens(token)
|
76 |
-
if num_added_tokens == 0:
|
77 |
-
raise ValueError(f"The tokenizer already contains the token {token}. Please pass a different `token` that is not already in the tokenizer.")
|
78 |
-
|
79 |
-
# resize the token embeddings
|
80 |
-
text_encoder.resize_token_embeddings(len(tokenizer))
|
81 |
-
|
82 |
-
# get the id for the token and assign the embeds
|
83 |
-
token_id = tokenizer.convert_tokens_to_ids(token)
|
84 |
-
text_encoder.get_input_embeddings().weight.data[token_id] = embeds
|
85 |
-
load_learned_embed_in_clip(learned_embeds_path, text_encoder, tokenizer)
|
86 |
-
|
87 |
-
def crop_center(pil_img, crop_width, crop_height):
|
88 |
-
img_width, img_height = pil_img.size
|
89 |
-
return pil_img.crop(((img_width - crop_width) // 2,
|
90 |
-
(img_height - crop_height) // 2,
|
91 |
-
(img_width + crop_width) // 2,
|
92 |
-
(img_height + crop_height) // 2))
|
93 |
-
|
94 |
-
#@title Run the Stable Diffusion pipeline
|
95 |
-
#@markdown Don't forget to use the placeholder token in your prompt
|
96 |
-
|
97 |
-
from torch import autocast
|
98 |
-
|
99 |
-
#pipe = StableDiffusionPipeline.from_pretrained(
|
100 |
-
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
101 |
-
pretrained_model_name_or_path,
|
102 |
-
torch_dtype=torch.float16,
|
103 |
-
text_encoder=text_encoder,
|
104 |
-
tokenizer=tokenizer,
|
105 |
-
use_auth_token=True,
|
106 |
-
crop = True,
|
107 |
-
).to(device)
|
108 |
-
pipe.enable_attention_slicing()
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
def inmm(init_image, prompt):
|
113 |
-
(w,h) = init_image.size
|
114 |
-
if w>h :
|
115 |
-
init_image = init_image.crop(((w - h) // 2,0,(w-h)//2 + h,h))
|
116 |
-
init_image = init_image.resize(512,512)
|
117 |
-
|
118 |
-
with autocast(device):
|
119 |
-
image = pipe([prompt], num_inference_steps=50, guidance_scale=7, init_image=init_image)["sample"]
|
120 |
-
return image[0]
|
121 |
-
|
122 |
-
demo = gr.Interface(inmm, inputs=[gr.Image(shape=(512, 512),type="pil"),gr.Textbox(lines=2, placeholder="どんな絵が欲しいか",value ="a heartwarming and calming landscape drawing in <m-mi2i> style")], outputs="image",
|
123 |
-
examples=[["a_img.png", "A heartwarming Canadian wheat field scene in <m-mi2i> style, some houses, silos, and a lake in the distance"],
|
124 |
-
["c_img.png","A heartwarming Landscape on the lake, scenery mirrored on the lake, in <m-mi2i> style"]])
|
125 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|