File size: 10,931 Bytes
4bae37e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-
"""Copy of Anime_Pack_Gradio.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1RxVCwOkq3Q5qlEkQxhFGeUxICBujjEjR
"""


from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")

model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en")

# from retrying import retry
from transformers import CLIPTextModel, CLIPTokenizer, BertTokenizer, BertForSequenceClassification, ChineseCLIPProcessor, ChineseCLIPModel, AutoModel
import gradio as gr
import numpy as np
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler, DPMSolverMultistepScheduler, StableDiffusionImg2ImgPipeline

import torch
from controlnet_aux import HEDdetector
from diffusers.utils import load_image
import concurrent.futures
from threading import Thread
from compel import Compel



device="cuda" if torch.cuda.is_available() else "cpu"

hidden_booster_text = "beautiful face, small boobs, a cup"
hidden_negative = "big boobs, huge boobs, sexy, dirty, d cup, e cup, g cup, slutty, badhandv4, ng_deepnegative_v1_75t, worst quality, low quality, extra digits, text, signature, bad anatomy, mutated hand, error, missing finger, cropped, worse quality, bad quality, lowres, floating limbs, bad hands, anatomical nonsense"
hidden_cn_booster_text = "漂亮的脸,小胸,贫乳,a罩杯"
hidden_cn_negative = "大胸, ,, !, 。, ;,巨乳,性感,脏,d罩杯,e罩杯,g罩杯,骚,骚气,badhandv4, ng_deepnegative_v1_75t"


def translate(prompt):
    trans_text = prompt
    translated = model.generate(**tokenizer(trans_text, return_tensors="pt", padding=True))
    tgt_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
    tgt_text = ''.join(tgt_text)[:-1]
    return tgt_text

from PIL import Image

hed = HEDdetector.from_pretrained('lllyasviel/ControlNet')

controlnet_scribble = ControlNetModel.from_pretrained(
    "lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16
)

pipe_scribble = StableDiffusionControlNetPipeline.from_single_file(
    "https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors", controlnet=controlnet_scribble,
    torch_dtype=torch.float16,
)

# pipe.load_lora_weights("shellypeng/detail-tweaker")
# pipe.load_lora_weights("shellypeng/midjourney-anime")

# pipe.load_lora_weights("shellypeng/animetarot")
# pipe.load_lora_weights("shellypeng/anime-stickers-v3")
# pipe.load_lora_weights("shellypeng/anime-magazine")

# pipe_img2img.load_lora_weights("yenojunie/slit-pupils")

# pipe_scribble.load_lora_weights("shellypeng/detail-tweaker")
# pipe_scribble.fuse_lora(lora_scale=0.1)
# pipe_scribble.load_lora_weights("shellypeng/lora-eyes")
# pipe_scribble.fuse_lora(lora_scale=0.1)
# pipe_scribble.load_lora_weights("shellypeng/beautiful-eyes")
# pipe_scribble.fuse_lora(lora_scale=0.1)

pipe_scribble.load_textual_inversion("shellypeng/bad-prompt")
pipe_scribble.load_textual_inversion("shellypeng/badhandv4")
# pipe.load_textual_inversion("shellypeng/easynegative")
pipe_scribble.load_textual_inversion("shellypeng/deepnegative")
pipe_scribble.load_textual_inversion("shellypeng/verybadimagenegative")
pipe_scribble.scheduler = DPMSolverMultistepScheduler.from_config(pipe_scribble.scheduler.config, use_karras_sigmas=True)
# pipe.enable_model_cpu_offload()
pipe_scribble.safety_checker = None
pipe_scribble.requires_safety_checker = False
pipe_scribble.to(device)


def scribble_to_image(text, input_img, chinese_check):
    """
    pass the sd model and do scribble to image
    include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
    expression to improve hand)
    """
    # change param "bag" below to text, image param below to input_img
    input_img = Image.fromarray(input_img)
    input_img = hed(input_img, scribble=True)
    input_img = load_image(input_img)
    # global prompt
    compel_proc = Compel(tokenizer=pipe_scribble.tokenizer, text_encoder=pipe_scribble.text_encoder)
    if chinese_check:
        text = translate(text)
        print("prompt text:", text)
    prompt = text + hidden_booster_text
    prompt_embeds = compel_proc(prompt)

    res_image0 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
    res_image1 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
    res_image2 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
    res_image3 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]

    return res_image0, res_image1, res_image2, res_image3

from PIL import Image

from transformers import pipeline
from diffusers import StableDiffusionDepth2ImgPipeline, StableDiffusionPipeline, StableDiffusionControlNetPipeline, StableDiffusionUpscalePipeline,  StableDiffusionImg2ImgPipeline, AutoPipelineForImage2Image

# Commented out IPython magic to ensure Python compatibility.
# %cd /content/drive/MyDrive/stable-diffusion-webui-colab/stable-diffusion-webui

pipe_img2img = StableDiffusionImg2ImgPipeline.from_single_file("https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors",
                                                               torch_dtype=torch.float16)

# pipe_img2img.load_lora_weights("shellypeng/detail-tweaker")
# pipe_img2img.fuse_lora(lora_scale=0.1)
# pipe_img2img.load_lora_weights("shellypeng/lora-eyes")
# pipe_img2img.fuse_lora(lora_scale=0.1)
# pipe_img2img.load_lora_weights("shellypeng/beautiful-eyes")
# pipe_img2img.fuse_lora(lora_scale=0.1)

pipe_img2img.load_textual_inversion("shellypeng/bad-prompt")
pipe_img2img.load_textual_inversion("shellypeng/badhandv4")
# pipe.load_textual_inversion("shellypeng/easynegative")
pipe_img2img.load_textual_inversion("shellypeng/deepnegative")
pipe_img2img.load_textual_inversion("shellypeng/verybadimagenegative")
pipe_img2img.scheduler = DPMSolverMultistepScheduler.from_config(pipe_img2img.scheduler.config, use_karras_sigmas=True)
# pipe.enable_model_cpu_offload()
pipe_img2img.safety_checker = None
pipe_img2img.requires_safety_checker = False
pipe_img2img.to(device)

def real_img2img_to_anime(text, input_img, chinese_check):
    """
    pass the sd model and do scribble to image
    include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
    expression to improve hand)
    """
    input_img = Image.fromarray(input_img)
    input_img = load_image(input_img)
    compel_proc = Compel(tokenizer=pipe_scribble.tokenizer, text_encoder=pipe_scribble.text_encoder)
    if chinese_check:
        text = translate(text)
        print("prompt text:", text)
    prompt = text + hidden_booster_text

    prompt_embeds = compel_proc(prompt)
    res_image0 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
    res_image1 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
    res_image2 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
    res_image3 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]

    return res_image0, res_image1, res_image2, res_image3


theme = gr.themes.Soft(
    primary_hue="orange",
    secondary_hue="orange",
).set(
    block_background_fill='*primary_50'
)

from transformers import pipeline

text = [
    "Brevity is the soul of wit.",
    "Amor, ch'a nullo amato amar perdona."
]

model_ckpt = "papluca/xlm-roberta-base-language-detection"
pipe = pipeline("text-classification", model=model_ckpt)
pipe(text, top_k=1, truncation=True)

with gr.Blocks(theme=theme, css="footer {visibility: hidden}", title="ShellAI Apps") as iface:
    with gr.Tab("Animefier"):
        with gr.Row(equal_height=True):
            with gr.Column():
                with gr.Row(equal_height=True):
                    prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt", scale=1)
                    chinese_check = gr.Checkbox(label="Chinese Prompt Mode", info="Click here to enable Chinese Prompting(点此触发中文提示词输入)", scale=0.3)

                image_box = gr.Image(label="Input Image", height=350)
                gen_btn = gr.Button(value="Generate")

        with gr.Row(equal_height=True):
            global image1
            global image2
            global image3
            global image4
            image1 = gr.Image()
            image2 = gr.Image()
            image3 = gr.Image()
            image4 = gr.Image()

        def mult_thread(prompt_box, image_box, chinese_check):
            with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
                future = executor.submit(real_img2img_to_anime, prompt_box, image_box, chinese_check)
                image1, image2, image3, image4 = future.result()
            return image1, image2, image3, image4
        gen_btn.click(mult_thread, [prompt_box, image_box, chinese_check], [image1, image2, image3, image4])

    with gr.Tab("AniSketch"):
        with gr.Row(equal_height=True):
            with gr.Column():
                with gr.Row(equal_height=True):
                    prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt", scale=1)
                    chinese_check = gr.Checkbox(label="Chinese Prompt Mode", info="Click here to enable Chinese Prompting(点此触发中文提示词输入)", scale=0.3)

                image_box = gr.Image(label="Input Image", height=350)
                gen_btn = gr.Button(value="Generate")
        with gr.Row(equal_height=True):
            image1 = gr.Image()
            image2 = gr.Image()
            image3 = gr.Image()
            image4 = gr.Image()

        def mult_thread(prompt_box, image_box, chinese_check):
            with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
                future = executor.submit(scribble_to_image, prompt_box, image_box, chinese_check)
                image1, image2, image3, image4 = future.result()
            return image1, image2, image3, image4

        gen_btn.click(mult_thread, [prompt_box, image_box, chinese_check], [image1, image2, image3, image4])

iface.launch(debug=True, share=True)