shellypeng commited on
Commit
4bae37e
·
1 Parent(s): de7859c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +235 -0
app.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Copy of Anime_Pack_Gradio.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1RxVCwOkq3Q5qlEkQxhFGeUxICBujjEjR
8
+ """
9
+
10
+
11
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
14
+
15
+ model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
16
+
17
+ # from retrying import retry
18
+ from transformers import CLIPTextModel, CLIPTokenizer, BertTokenizer, BertForSequenceClassification, ChineseCLIPProcessor, ChineseCLIPModel, AutoModel
19
+ import gradio as gr
20
+ import numpy as np
21
+ from PIL import Image
22
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler, DPMSolverMultistepScheduler, StableDiffusionImg2ImgPipeline
23
+
24
+ import torch
25
+ from controlnet_aux import HEDdetector
26
+ from diffusers.utils import load_image
27
+ import concurrent.futures
28
+ from threading import Thread
29
+ from compel import Compel
30
+
31
+
32
+
33
+ device="cuda" if torch.cuda.is_available() else "cpu"
34
+
35
+ hidden_booster_text = "beautiful face, small boobs, a cup"
36
+ 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"
37
+ hidden_cn_booster_text = "漂亮的脸,小胸,贫乳,a罩杯"
38
+ hidden_cn_negative = "大胸, ,, !, 。, ;,巨乳,性感,脏,d罩杯,e罩杯,g罩杯,骚,骚气,badhandv4, ng_deepnegative_v1_75t"
39
+
40
+
41
+ def translate(prompt):
42
+ trans_text = prompt
43
+ translated = model.generate(**tokenizer(trans_text, return_tensors="pt", padding=True))
44
+ tgt_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
45
+ tgt_text = ''.join(tgt_text)[:-1]
46
+ return tgt_text
47
+
48
+ from PIL import Image
49
+
50
+ hed = HEDdetector.from_pretrained('lllyasviel/ControlNet')
51
+
52
+ controlnet_scribble = ControlNetModel.from_pretrained(
53
+ "lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16
54
+ )
55
+
56
+ pipe_scribble = StableDiffusionControlNetPipeline.from_single_file(
57
+ "https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors", controlnet=controlnet_scribble,
58
+ torch_dtype=torch.float16,
59
+ )
60
+
61
+ # pipe.load_lora_weights("shellypeng/detail-tweaker")
62
+ # pipe.load_lora_weights("shellypeng/midjourney-anime")
63
+
64
+ # pipe.load_lora_weights("shellypeng/animetarot")
65
+ # pipe.load_lora_weights("shellypeng/anime-stickers-v3")
66
+ # pipe.load_lora_weights("shellypeng/anime-magazine")
67
+
68
+ # pipe_img2img.load_lora_weights("yenojunie/slit-pupils")
69
+
70
+ # pipe_scribble.load_lora_weights("shellypeng/detail-tweaker")
71
+ # pipe_scribble.fuse_lora(lora_scale=0.1)
72
+ # pipe_scribble.load_lora_weights("shellypeng/lora-eyes")
73
+ # pipe_scribble.fuse_lora(lora_scale=0.1)
74
+ # pipe_scribble.load_lora_weights("shellypeng/beautiful-eyes")
75
+ # pipe_scribble.fuse_lora(lora_scale=0.1)
76
+
77
+ pipe_scribble.load_textual_inversion("shellypeng/bad-prompt")
78
+ pipe_scribble.load_textual_inversion("shellypeng/badhandv4")
79
+ # pipe.load_textual_inversion("shellypeng/easynegative")
80
+ pipe_scribble.load_textual_inversion("shellypeng/deepnegative")
81
+ pipe_scribble.load_textual_inversion("shellypeng/verybadimagenegative")
82
+ pipe_scribble.scheduler = DPMSolverMultistepScheduler.from_config(pipe_scribble.scheduler.config, use_karras_sigmas=True)
83
+ # pipe.enable_model_cpu_offload()
84
+ pipe_scribble.safety_checker = None
85
+ pipe_scribble.requires_safety_checker = False
86
+ pipe_scribble.to(device)
87
+
88
+
89
+ def scribble_to_image(text, input_img, chinese_check):
90
+ """
91
+ pass the sd model and do scribble to image
92
+ include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
93
+ expression to improve hand)
94
+ """
95
+ # change param "bag" below to text, image param below to input_img
96
+ input_img = Image.fromarray(input_img)
97
+ input_img = hed(input_img, scribble=True)
98
+ input_img = load_image(input_img)
99
+ # global prompt
100
+ compel_proc = Compel(tokenizer=pipe_scribble.tokenizer, text_encoder=pipe_scribble.text_encoder)
101
+ if chinese_check:
102
+ text = translate(text)
103
+ print("prompt text:", text)
104
+ prompt = text + hidden_booster_text
105
+ prompt_embeds = compel_proc(prompt)
106
+
107
+ res_image0 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
108
+ res_image1 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
109
+ res_image2 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
110
+ res_image3 = pipe_scribble(image=input_img, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
111
+
112
+ return res_image0, res_image1, res_image2, res_image3
113
+
114
+ from PIL import Image
115
+
116
+ from transformers import pipeline
117
+ from diffusers import StableDiffusionDepth2ImgPipeline, StableDiffusionPipeline, StableDiffusionControlNetPipeline, StableDiffusionUpscalePipeline, StableDiffusionImg2ImgPipeline, AutoPipelineForImage2Image
118
+
119
+ # Commented out IPython magic to ensure Python compatibility.
120
+ # %cd /content/drive/MyDrive/stable-diffusion-webui-colab/stable-diffusion-webui
121
+
122
+ pipe_img2img = StableDiffusionImg2ImgPipeline.from_single_file("https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors",
123
+ torch_dtype=torch.float16)
124
+
125
+ # pipe_img2img.load_lora_weights("shellypeng/detail-tweaker")
126
+ # pipe_img2img.fuse_lora(lora_scale=0.1)
127
+ # pipe_img2img.load_lora_weights("shellypeng/lora-eyes")
128
+ # pipe_img2img.fuse_lora(lora_scale=0.1)
129
+ # pipe_img2img.load_lora_weights("shellypeng/beautiful-eyes")
130
+ # pipe_img2img.fuse_lora(lora_scale=0.1)
131
+
132
+ pipe_img2img.load_textual_inversion("shellypeng/bad-prompt")
133
+ pipe_img2img.load_textual_inversion("shellypeng/badhandv4")
134
+ # pipe.load_textual_inversion("shellypeng/easynegative")
135
+ pipe_img2img.load_textual_inversion("shellypeng/deepnegative")
136
+ pipe_img2img.load_textual_inversion("shellypeng/verybadimagenegative")
137
+ pipe_img2img.scheduler = DPMSolverMultistepScheduler.from_config(pipe_img2img.scheduler.config, use_karras_sigmas=True)
138
+ # pipe.enable_model_cpu_offload()
139
+ pipe_img2img.safety_checker = None
140
+ pipe_img2img.requires_safety_checker = False
141
+ pipe_img2img.to(device)
142
+
143
+ def real_img2img_to_anime(text, input_img, chinese_check):
144
+ """
145
+ pass the sd model and do scribble to image
146
+ include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
147
+ expression to improve hand)
148
+ """
149
+ input_img = Image.fromarray(input_img)
150
+ input_img = load_image(input_img)
151
+ compel_proc = Compel(tokenizer=pipe_scribble.tokenizer, text_encoder=pipe_scribble.text_encoder)
152
+ if chinese_check:
153
+ text = translate(text)
154
+ print("prompt text:", text)
155
+ prompt = text + hidden_booster_text
156
+
157
+ prompt_embeds = compel_proc(prompt)
158
+ res_image0 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
159
+ res_image1 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
160
+ res_image2 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
161
+ res_image3 = pipe_img2img(image=input_img, strength=0.6, prompt_embeds=prompt_embeds, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
162
+
163
+ return res_image0, res_image1, res_image2, res_image3
164
+
165
+
166
+ theme = gr.themes.Soft(
167
+ primary_hue="orange",
168
+ secondary_hue="orange",
169
+ ).set(
170
+ block_background_fill='*primary_50'
171
+ )
172
+
173
+ from transformers import pipeline
174
+
175
+ text = [
176
+ "Brevity is the soul of wit.",
177
+ "Amor, ch'a nullo amato amar perdona."
178
+ ]
179
+
180
+ model_ckpt = "papluca/xlm-roberta-base-language-detection"
181
+ pipe = pipeline("text-classification", model=model_ckpt)
182
+ pipe(text, top_k=1, truncation=True)
183
+
184
+ with gr.Blocks(theme=theme, css="footer {visibility: hidden}", title="ShellAI Apps") as iface:
185
+ with gr.Tab("Animefier"):
186
+ with gr.Row(equal_height=True):
187
+ with gr.Column():
188
+ with gr.Row(equal_height=True):
189
+ prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt", scale=1)
190
+ chinese_check = gr.Checkbox(label="Chinese Prompt Mode", info="Click here to enable Chinese Prompting(点此触发中文提示词输入)", scale=0.3)
191
+
192
+ image_box = gr.Image(label="Input Image", height=350)
193
+ gen_btn = gr.Button(value="Generate")
194
+
195
+ with gr.Row(equal_height=True):
196
+ global image1
197
+ global image2
198
+ global image3
199
+ global image4
200
+ image1 = gr.Image()
201
+ image2 = gr.Image()
202
+ image3 = gr.Image()
203
+ image4 = gr.Image()
204
+
205
+ def mult_thread(prompt_box, image_box, chinese_check):
206
+ with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
207
+ future = executor.submit(real_img2img_to_anime, prompt_box, image_box, chinese_check)
208
+ image1, image2, image3, image4 = future.result()
209
+ return image1, image2, image3, image4
210
+ gen_btn.click(mult_thread, [prompt_box, image_box, chinese_check], [image1, image2, image3, image4])
211
+
212
+ with gr.Tab("AniSketch"):
213
+ with gr.Row(equal_height=True):
214
+ with gr.Column():
215
+ with gr.Row(equal_height=True):
216
+ prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt", scale=1)
217
+ chinese_check = gr.Checkbox(label="Chinese Prompt Mode", info="Click here to enable Chinese Prompting(点此触发中文提示词输入)", scale=0.3)
218
+
219
+ image_box = gr.Image(label="Input Image", height=350)
220
+ gen_btn = gr.Button(value="Generate")
221
+ with gr.Row(equal_height=True):
222
+ image1 = gr.Image()
223
+ image2 = gr.Image()
224
+ image3 = gr.Image()
225
+ image4 = gr.Image()
226
+
227
+ def mult_thread(prompt_box, image_box, chinese_check):
228
+ with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
229
+ future = executor.submit(scribble_to_image, prompt_box, image_box, chinese_check)
230
+ image1, image2, image3, image4 = future.result()
231
+ return image1, image2, image3, image4
232
+
233
+ gen_btn.click(mult_thread, [prompt_box, image_box, chinese_check], [image1, image2, image3, image4])
234
+
235
+ iface.launch(debug=True, share=True)