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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, StableDiffusionPipeline
|
2 |
+
import torch
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from transformers import pipeline
|
6 |
+
import gradio as gr
|
7 |
+
from PIL import Image
|
8 |
+
from diffusers.utils import load_image
|
9 |
+
import os, random, gc, re, json, time, shutil, glob
|
10 |
+
import PIL.Image
|
11 |
+
import tqdm
|
12 |
+
from controlnet_aux import OpenposeDetector
|
13 |
+
from accelerate import Accelerator
|
14 |
+
from huggingface_hub import HfApi, list_models, InferenceClient, ModelCard, RepoCard, upload_folder, hf_hub_download, HfFileSystem
|
15 |
+
HfApi=HfApi()
|
16 |
+
HF_TOKEN=os.getenv("HF_TOKEN")
|
17 |
+
HF_HUB_DISABLE_TELEMETRY=1
|
18 |
+
DO_NOT_TRACK=1
|
19 |
+
HF_HUB_ENABLE_HF_TRANSFER=0
|
20 |
+
accelerator = Accelerator(cpu=True)
|
21 |
+
InferenceClient=InferenceClient()
|
22 |
+
|
23 |
+
models =[]
|
24 |
+
loris=[]
|
25 |
+
apol=[]
|
26 |
+
|
27 |
+
def hgfdm(models):
|
28 |
+
models=models
|
29 |
+
poi=InferenceClient.list_deployed_models()
|
30 |
+
voi=poi["text-to-image"]
|
31 |
+
for met in voi:
|
32 |
+
pio=""+met+""
|
33 |
+
models.append(pio)
|
34 |
+
return models
|
35 |
+
|
36 |
+
def smdls(models):
|
37 |
+
models=models
|
38 |
+
mtlst=HfApi.list_models(filter="diffusers:StableDiffusionPipeline",limit=500,full=True,)
|
39 |
+
if mtlst:
|
40 |
+
for nea in mtlst:
|
41 |
+
vmh=""+str(nea.id)+""
|
42 |
+
models.append(vmh)
|
43 |
+
return models
|
44 |
+
|
45 |
+
def sldls(loris):
|
46 |
+
loris=loris
|
47 |
+
ltlst=HfApi.list_models(filter="stable-diffusion",search="lora",limit=500,full=True,)
|
48 |
+
if ltlst:
|
49 |
+
for noa in ltlst:
|
50 |
+
lmh=""+str(noa.id)+""
|
51 |
+
loris.append(lmh)
|
52 |
+
return loris
|
53 |
+
|
54 |
+
def chdr(apol,prompt,modil,los,stips,fnamo,gaul):
|
55 |
+
try:
|
56 |
+
type="SD_controlnet"
|
57 |
+
tre='./tmpo/'+fnamo+'.json'
|
58 |
+
tra='./tmpo/'+fnamo+'_0.png'
|
59 |
+
trm='./tmpo/'+fnamo+'_1.png'
|
60 |
+
trv='./tmpo/'+fnamo+'_pose.png'
|
61 |
+
trh='./tmpo/'+fnamo+'_canny.png'
|
62 |
+
trg='./tmpo/'+fnamo+'_cann_im.png'
|
63 |
+
trq='./tmpo/'+fnamo+'_tilage.png'
|
64 |
+
flng=["yssup", "sllab", "stsaerb", "sinep", "selppin", "ssa", "tnuc", "mub", "kcoc", "kcid", "anigav", "dekan", "edun", "slatineg", "xes", "nrop", "stit", "ttub", "bojwolb", "noitartenep", "kcuf", "kcus", "kcil", "elttil", "gnuoy", "thgit", "lrig", "etitep", "dlihc", "yxes"]
|
65 |
+
flng=[itm[::-1] for itm in flng]
|
66 |
+
ptn = r"\b" + r"\b|\b".join(flng) + r"\b"
|
67 |
+
if re.search(ptn, prompt, re.IGNORECASE):
|
68 |
+
print("onon buddy")
|
69 |
+
else:
|
70 |
+
dobj={'img_name':fnamo,'model':modil,'lora':los,'prompt':prompt,'steps':stips,'type':type}
|
71 |
+
with open(tre, 'w') as f:
|
72 |
+
json.dump(dobj, f)
|
73 |
+
HfApi.upload_folder(repo_id="JoPmt/hf_community_images",folder_path="./tmpo",repo_type="dataset",path_in_repo="./",token=HF_TOKEN)
|
74 |
+
dobj={'img_name':fnamo,'model':modil,'lora':los,'prompt':prompt,'steps':stips,'type':type,'haed':gaul,}
|
75 |
+
with open(tre, 'w') as f:
|
76 |
+
json.dump(dobj, f)
|
77 |
+
HfApi.upload_folder(repo_id="JoPmt/Tst_datast_imgs",folder_path="./tmpo",repo_type="dataset",path_in_repo="./",token=HF_TOKEN)
|
78 |
+
try:
|
79 |
+
for pgn in glob.glob('./tmpo/*.png'):
|
80 |
+
os.remove(pgn)
|
81 |
+
for jgn in glob.glob('./tmpo/*.json'):
|
82 |
+
os.remove(jgn)
|
83 |
+
del tre
|
84 |
+
del tra
|
85 |
+
del trm
|
86 |
+
del trv
|
87 |
+
del trh
|
88 |
+
del trg
|
89 |
+
del trq
|
90 |
+
except:
|
91 |
+
print("cant")
|
92 |
+
except:
|
93 |
+
print("failed to umake obj")
|
94 |
+
|
95 |
+
def crll(dnk):
|
96 |
+
lix=""
|
97 |
+
lotr=HfApi.list_files_info(repo_id=""+dnk+"",repo_type="model")
|
98 |
+
for flre in list(lotr):
|
99 |
+
fllr=[]
|
100 |
+
gar=re.match(r'.+(\.pt|\.ckpt|\.bin|\.safetensors)$', flre.path)
|
101 |
+
yir=re.search(r'[^/]+$', flre.path)
|
102 |
+
if gar:
|
103 |
+
fllr.append(""+str(yir.group(0))+"")
|
104 |
+
lix=""+fllr[-1]+""
|
105 |
+
else:
|
106 |
+
lix=""
|
107 |
+
return lix
|
108 |
+
|
109 |
+
def plax(gaul,req: gr.Request):
|
110 |
+
gaul=str(req.headers)
|
111 |
+
return gaul
|
112 |
+
|
113 |
+
def plex(prompt,mput,neg_prompt,modil,stips,scaly,csal,csbl,nut,wei,hei,los,loca,gaul,progress=gr.Progress(track_tqdm=True)):
|
114 |
+
gc.collect()
|
115 |
+
adi=""
|
116 |
+
ldi=""
|
117 |
+
|
118 |
+
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
119 |
+
controlnet = [
|
120 |
+
ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-openpose", torch_dtype=torch.float32),
|
121 |
+
ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float32),
|
122 |
+
]
|
123 |
+
try:
|
124 |
+
crda=ModelCard.load(""+modil+"")
|
125 |
+
card=ModelCard.load(""+modil+"").data.to_dict().get("instance_prompt")
|
126 |
+
cerd=ModelCard.load(""+modil+"").data.to_dict().get("custom_prompt")
|
127 |
+
cird=ModelCard.load(""+modil+"").data.to_dict().get("lora_prompt")
|
128 |
+
mtch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*(.*?)\s*(?=to trigger)', crda.text, re.IGNORECASE)
|
129 |
+
moch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*([^.]*)', crda.text, re.IGNORECASE)
|
130 |
+
if moch:
|
131 |
+
adi+=""+str(moch.group(1))+", "
|
132 |
+
else:
|
133 |
+
print("no floff trigger")
|
134 |
+
if mtch:
|
135 |
+
adi+=""+str(mtch.group(1))+", "
|
136 |
+
else:
|
137 |
+
print("no fluff trigger")
|
138 |
+
if card:
|
139 |
+
adi+=""+str(card)+", "
|
140 |
+
else:
|
141 |
+
print("no instance")
|
142 |
+
if cerd:
|
143 |
+
adi+=""+str(cerd)+", "
|
144 |
+
else:
|
145 |
+
print("no custom")
|
146 |
+
if cird:
|
147 |
+
adi+=""+str(cird)+", "
|
148 |
+
else:
|
149 |
+
print("no lora")
|
150 |
+
except:
|
151 |
+
print("no card")
|
152 |
+
try:
|
153 |
+
pope = accelerator.prepare(StableDiffusionPipeline.from_pretrained(""+modil+"", use_safetensors=False,torch_dtype=torch.float32, safety_checker=None))
|
154 |
+
pipe = accelerator.prepare(StableDiffusionControlNetPipeline.from_pretrained(""+modil+"", use_safetensors=False,controlnet=controlnet,torch_dtype=torch.float32,safety_checker=None))
|
155 |
+
except:
|
156 |
+
gc.collect()
|
157 |
+
pope = accelerator.prepare(StableDiffusionPipeline.from_pretrained(""+modil+"", use_safetensors=True,torch_dtype=torch.float32, safety_checker=None))
|
158 |
+
pipe = accelerator.prepare(StableDiffusionControlNetPipeline.from_pretrained(""+modil+"", use_safetensors=True,controlnet=controlnet,torch_dtype=torch.float32,safety_checker=None))
|
159 |
+
if los:
|
160 |
+
try:
|
161 |
+
lrda=ModelCard.load(""+los+"")
|
162 |
+
lard=ModelCard.load(""+los+"").data.to_dict().get("instance_prompt")
|
163 |
+
lerd=ModelCard.load(""+los+"").data.to_dict().get("custom_prompt")
|
164 |
+
lird=ModelCard.load(""+los+"").data.to_dict().get("stable-diffusion")
|
165 |
+
ltch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*(.*?)\s*(?=to trigger)', lrda.text, re.IGNORECASE)
|
166 |
+
loch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*([^.]*)', lrda.text, re.IGNORECASE)
|
167 |
+
if loch and lird:
|
168 |
+
ldi+=""+str(loch.group(1))+", "
|
169 |
+
else:
|
170 |
+
print("no lloff trigger")
|
171 |
+
if ltch and lird:
|
172 |
+
ldi+=""+str(ltch.group(1))+", "
|
173 |
+
else:
|
174 |
+
print("no lluff trigger")
|
175 |
+
if lard and lird:
|
176 |
+
ldi+=""+str(lard)+", "
|
177 |
+
else:
|
178 |
+
print("no instance")
|
179 |
+
ldi+=""
|
180 |
+
if lerd and lird:
|
181 |
+
ldi+=""+str(lerd)+", "
|
182 |
+
else:
|
183 |
+
print("no custom")
|
184 |
+
ldi+=""
|
185 |
+
except:
|
186 |
+
print("no trigger")
|
187 |
+
try:
|
188 |
+
pope.load_lora_weights(""+los+"", weight_name=""+str(crll(los))+"",)
|
189 |
+
pope.fuse_lora(fuse_unet=True,fuse_text_encoder=False)
|
190 |
+
except:
|
191 |
+
print("no can do")
|
192 |
+
else:
|
193 |
+
los=""
|
194 |
+
pope.unet.to(memory_format=torch.channels_last)
|
195 |
+
pope = accelerator.prepare(pope.to("cpu"))
|
196 |
+
pipe.unet.to(memory_format=torch.channels_last)
|
197 |
+
pipe = accelerator.prepare(pipe.to("cpu"))
|
198 |
+
gc.collect()
|
199 |
+
apol=[]
|
200 |
+
height=hei
|
201 |
+
width=wei
|
202 |
+
prompt=""+str(adi)+""+str(ldi)+""+prompt+""
|
203 |
+
negative_prompt=""+neg_prompt+""
|
204 |
+
lora_scale=loca
|
205 |
+
if nut == 0:
|
206 |
+
nm = random.randint(1, 2147483616)
|
207 |
+
while nm % 32 != 0:
|
208 |
+
nm = random.randint(1, 2147483616)
|
209 |
+
else:
|
210 |
+
nm=nut
|
211 |
+
generator = torch.Generator(device="cpu").manual_seed(nm)
|
212 |
+
tilage = pope(prompt,num_inference_steps=5,height=height,width=width,generator=generator,cross_attention_kwargs={"scale": lora_scale}).images[0]
|
213 |
+
cannyimage = np.array(tilage)
|
214 |
+
low_threshold = 100
|
215 |
+
high_threshold = 200
|
216 |
+
fnamo=""+str(int(time.time()))+""
|
217 |
+
cannyimage = cv2.Canny(cannyimage, low_threshold, high_threshold)
|
218 |
+
cammyimage=Image.fromarray(cannyimage).save('./tmpo/'+fnamo+'_canny.png', 'PNG')
|
219 |
+
zero_start = cannyimage.shape[1] // 4
|
220 |
+
zero_end = zero_start + cannyimage.shape[1] // 2
|
221 |
+
cannyimage[:, zero_start:zero_end] = 0
|
222 |
+
cannyimage = cannyimage[:, :, None]
|
223 |
+
cannyimage = np.concatenate([cannyimage, cannyimage, cannyimage], axis=2)
|
224 |
+
canny_image = Image.fromarray(cannyimage)
|
225 |
+
pose_image = load_image(mput).resize((512, 512))
|
226 |
+
openpose_image = openpose(pose_image)
|
227 |
+
images = [openpose_image, canny_image]
|
228 |
+
omage=pipe([prompt]*2,images,num_inference_steps=stips,generator=generator,negative_prompt=[neg_prompt]*2,controlnet_conditioning_scale=[csal, csbl])
|
229 |
+
for i, imge in enumerate(omage["images"]):
|
230 |
+
apol.append(imge)
|
231 |
+
imge.save('./tmpo/'+fnamo+'_'+str(i)+'.png', 'PNG')
|
232 |
+
apol.append(openpose_image)
|
233 |
+
apol.append(cammyimage)
|
234 |
+
apol.append(canny_image)
|
235 |
+
apol.append(tilage)
|
236 |
+
openpose_image.save('./tmpo/'+fnamo+'_pose.png', 'PNG')
|
237 |
+
canny_image.save('./tmpo/'+fnamo+'_cann_im.png', 'PNG')
|
238 |
+
tilage.save('./tmpo/'+fnamo+'_tilage.png', 'PNG')
|
239 |
+
chdr(apol,prompt,modil,los,stips,fnamo,gaul)
|
240 |
+
return apol
|
241 |
+
|
242 |
+
def aip(ill,api_name="/run"):
|
243 |
+
return
|
244 |
+
def pit(ill,api_name="/predict"):
|
245 |
+
return
|
246 |
+
|
247 |
+
with gr.Blocks(theme=random.choice([gr.themes.Monochrome(),gr.themes.Base.from_hub("gradio/seafoam"),gr.themes.Base.from_hub("freddyaboulton/dracula_revamped"),gr.themes.Glass(),gr.themes.Base(),]),analytics_enabled=False) as iface:
|
248 |
+
out=gr.Gallery(label="Generated Output Image", columns=1)
|
249 |
+
inut=gr.Textbox(label="Prompt")
|
250 |
+
mput=gr.Image(type="filepath")
|
251 |
+
gaul=gr.Textbox(visible=False)
|
252 |
+
inot=gr.Dropdown(choices=smdls(models),value=random.choice(models), type="value")
|
253 |
+
btn=gr.Button("GENERATE")
|
254 |
+
with gr.Accordion("Advanced Settings", open=False):
|
255 |
+
inlt=gr.Dropdown(choices=sldls(loris),value=None, type="value")
|
256 |
+
inet=gr.Textbox(label="Negative_prompt", value="low quality, bad quality,")
|
257 |
+
inyt=gr.Slider(label="Num inference steps",minimum=1,step=1,maximum=30,value=20)
|
258 |
+
inat=gr.Slider(label="Guidance_scale",minimum=1,step=1,maximum=20,value=7)
|
259 |
+
csal=gr.Slider(label="condition_scale_canny", value=0.5, minimum=0.1, step=0.1, maximum=1)
|
260 |
+
csbl=gr.Slider(label="condition_scale_pose", value=0.5, minimum=0.1, step=0.1, maximum=1)
|
261 |
+
loca=gr.Slider(label="Lora scale",minimum=0.1,step=0.1,maximum=0.9,value=0.5)
|
262 |
+
indt=gr.Slider(label="Manual seed (leave 0 for random)",minimum=0,step=32,maximum=2147483616,value=0)
|
263 |
+
inwt=gr.Slider(label="Width",minimum=512,step=32,maximum=1024,value=512)
|
264 |
+
inht=gr.Slider(label="Height",minimum=512,step=32,maximum=1024,value=512)
|
265 |
+
|
266 |
+
btn.click(fn=plax,inputs=gaul,outputs=gaul).then(fn=plex, outputs=[out], inputs=[inut,mput,inet,inot,inyt,inat,csal,csbl,indt,inwt,inht,inlt,loca,gaul])
|
267 |
+
|
268 |
+
iface.queue(max_size=1,api_open=False)
|
269 |
+
iface.launch(max_threads=20,inline=False,show_api=False)
|