shellypeng commited on
Commit
5b9202b
·
1 Parent(s): c6590a6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -0
app.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ from transformers import pipeline
5
+ from diffusers import StableDiffusionDepth2ImgPipeline, StableDiffusionPipeline, StableDiffusionControlNetPipeline, StableDiffusionUpscalePipeline, StableDiffusionImg2ImgPipeline, AutoPipelineForImage2Image
6
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler, DPMSolverMultistepScheduler
7
+
8
+ import torch
9
+ from controlnet_aux import HEDdetector
10
+ from diffusers.utils import load_image
11
+ from huggingface_hub import notebook_login, login
12
+ import concurrent.futures
13
+ from threading import Thread
14
+
15
+ hidden_booster_text = "beautiful face, beautiful hand, small boobs, a cup"
16
+ 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"
17
+
18
+ hed = HEDdetector.from_pretrained('lllyasviel/ControlNet')
19
+
20
+ controlnet_scribble = ControlNetModel.from_pretrained(
21
+ "lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16
22
+ )
23
+
24
+ pipe_scribble = StableDiffusionControlNetPipeline.from_single_file(
25
+ "https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors", controlnet=controlnet_scribble,
26
+ torch_dtype=torch.float16,
27
+ )
28
+
29
+ pipe_scribble.load_textual_inversion("shellypeng/bad-prompt")
30
+ pipe_scribble.load_textual_inversion("shellypeng/badhandv4")
31
+ # pipe.load_textual_inversion("shellypeng/easynegative")
32
+ pipe_scribble.load_textual_inversion("shellypeng/deepnegative")
33
+ pipe_scribble.load_textual_inversion("shellypeng/verybadimagenegative")
34
+ pipe_scribble.scheduler = DPMSolverMultistepScheduler.from_config(pipe_scribble.scheduler.config, use_karras_sigmas=True)
35
+ # pipe.enable_model_cpu_offload()
36
+ def dummy(images, **kwargs):
37
+ return images, False
38
+ pipe_scribble.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
39
+ pipe_scribble.to("cuda")
40
+
41
+ def scribble_to_image(text, input_img):
42
+ """
43
+ pass the sd model and do scribble to image
44
+ include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
45
+ expression to improve hand)
46
+ """
47
+ # change param "bag" below to text, image param below to input_img
48
+ input_img = Image.fromarray(input_img)
49
+ input_img = hed(input_img, scribble=True)
50
+ input_img = load_image(input_img)
51
+ # global prompt
52
+ prompt = text + hidden_booster_text
53
+ res_image0 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
54
+ res_image1 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
55
+ res_image2 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
56
+ res_image3 = pipe_scribble(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
57
+
58
+ return res_image0, res_image1, res_image2, res_image3
59
+
60
+ theme = gr.themes.Soft(
61
+ primary_hue="orange",
62
+ secondary_hue="orange",
63
+ ).set(
64
+ block_background_fill='*primary_50'
65
+ )
66
+
67
+ pipe_img2img = StableDiffusionImg2ImgPipeline.from_single_file("https://huggingface.co/shellypeng/anime-god/blob/main/animeGod_v10.safetensors",
68
+ torch_dtype=torch.float16)
69
+ pipe_img2img.load_lora_weights("shellypeng/detail-tweaker")
70
+ pipe_img2img.fuse_lora(lora_scale=0.1)
71
+ pipe_img2img.load_textual_inversion("shellypeng/bad-prompt")
72
+ pipe_img2img.load_textual_inversion("shellypeng/badhandv4")
73
+ # pipe.load_textual_inversion("shellypeng/easynegative")
74
+ pipe_img2img.load_textual_inversion("shellypeng/deepnegative")
75
+ pipe_img2img.load_textual_inversion("shellypeng/verybadimagenegative")
76
+ pipe_img2img.scheduler = DPMSolverMultistepScheduler.from_config(pipe_img2img.scheduler.config, use_karras_sigmas=True)
77
+ # pipe.enable_model_cpu_offload()
78
+ def dummy(images, **kwargs):
79
+ return images, False
80
+ pipe_img2img.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
81
+ pipe_img2img = pipe_img2img.to("cuda")
82
+
83
+ def real_img2img_to_anime(text, input_img):
84
+ """
85
+ pass the sd model and do scribble to image
86
+ include Adetailer, detail tweaker lora, prompt backend include: beautiful eyes, beautiful face, beautiful hand, (maybe infer from user's prompt for gesture and facial
87
+ expression to improve hand)
88
+ """
89
+ input_img = Image.fromarray(input_img)
90
+ input_img = load_image(input_img)
91
+ prompt = text + hidden_booster_text
92
+ # input_img = depth_estimator(input_img)['depth']
93
+ res_image0 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
94
+ res_image1 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
95
+ res_image2 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
96
+ res_image3 = pipe_img2img(prompt, input_img, negative_prompt=hidden_negative, num_inference_steps=40).images[0]
97
+
98
+ return res_image0, res_image1, res_image2, res_image3
99
+
100
+
101
+ theme = gr.themes.Soft(
102
+ primary_hue="orange",
103
+ secondary_hue="orange",
104
+ ).set(
105
+ block_background_fill='*primary_50'
106
+ )
107
+
108
+
109
+ generator = torch.manual_seed(33)
110
+
111
+ with gr.Blocks(theme=theme, css="footer {visibility: hidden}", title="ShellAI Apps") as iface:
112
+ with gr.Tab("Animefier"):
113
+ with gr.Row(equal_height=True):
114
+ with gr.Column():
115
+ prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt")
116
+ image_box = gr.Image(label="Input Image", height=350)
117
+ gen_btn = gr.Button(value="Generate")
118
+ with gr.Row(equal_height=True):
119
+ global image1
120
+ global image2
121
+ global image3
122
+ global image4
123
+ image1 = gr.Image()
124
+ image2 = gr.Image()
125
+ image3 = gr.Image()
126
+ image4 = gr.Image()
127
+
128
+ def mult_thread(prompt_box, image_box):
129
+ with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
130
+ future = executor.submit(real_img2img_to_anime, prompt_box, image_box)
131
+ image1, image2, image3, image4 = future.result()
132
+ return image1, image2, image3, image4
133
+
134
+ gen_btn.click(mult_thread, [prompt_box, image_box], [image1, image2, image3, image4])
135
+ with gr.Tab("AniSketch"):
136
+ with gr.Row(equal_height=True):
137
+ with gr.Column():
138
+ prompt_box = gr.Textbox(label="Prompt", placeholder="Enter a prompt")
139
+ image_box = gr.Image(label="Input Image", height=350)
140
+ gen_btn = gr.Button(value="Generate")
141
+ with gr.Row(equal_height=True):
142
+ image1 = gr.Image()
143
+ image2 = gr.Image()
144
+ image3 = gr.Image()
145
+ image4 = gr.Image()
146
+
147
+ def mult_thread(prompt_box, image_box):
148
+ with concurrent.futures.ThreadPoolExecutor(max_workers=12000) as executor:
149
+ future = executor.submit(scribble_to_image, prompt_box, image_box)
150
+ image1, image2, image3, image4 = future.result()
151
+ return image1, image2, image3, image4
152
+
153
+ gen_btn.click(mult_thread, [prompt_box, image_box], [image1, image2, image3, image4])
154
+
155
+ iface.launch(inline=False)
156
+
157
+
158
+