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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import DiffusionPipeline
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os, random, gc, re, json, time
|
| 6 |
+
import PIL.Image
|
| 7 |
+
import tqdm
|
| 8 |
+
from accelerate import Accelerator
|
| 9 |
+
from huggingface_hub import HfApi, list_models, InferenceClient, ModelCard, RepoCard, upload_folder, hf_hub_download, HfFileSystem
|
| 10 |
+
HfApi=HfApi()
|
| 11 |
+
HF_TOKEN=os.getenv("HF_TOKEN")
|
| 12 |
+
HF_HUB_DISABLE_TELEMETRY=1
|
| 13 |
+
DO_NOT_TRACK=1
|
| 14 |
+
accelerator = Accelerator(cpu=True)
|
| 15 |
+
InferenceClient=InferenceClient()
|
| 16 |
+
|
| 17 |
+
models =[]
|
| 18 |
+
loris=[]
|
| 19 |
+
apol=[]
|
| 20 |
+
|
| 21 |
+
def hgfdm(models):
|
| 22 |
+
models=models
|
| 23 |
+
poi=InferenceClient.list_deployed_models()
|
| 24 |
+
voi=poi["text-to-image"]
|
| 25 |
+
for met in voi:
|
| 26 |
+
pio=""+met+""
|
| 27 |
+
models.append(pio)
|
| 28 |
+
return models
|
| 29 |
+
|
| 30 |
+
def smdls(models):
|
| 31 |
+
models=models
|
| 32 |
+
mtlst=HfApi.list_models(filter="diffusers:StableDiffusionPipeline",limit=500,full=True,)
|
| 33 |
+
if mtlst:
|
| 34 |
+
for nea in mtlst:
|
| 35 |
+
vmh=""+str(nea.id)+""
|
| 36 |
+
models.append(vmh)
|
| 37 |
+
return models
|
| 38 |
+
|
| 39 |
+
def sldls(loris):
|
| 40 |
+
loris=loris
|
| 41 |
+
ltlst=HfApi.list_models(filter="stable-diffusion",search="lora",limit=500,full=True,)
|
| 42 |
+
if ltlst:
|
| 43 |
+
for noa in ltlst:
|
| 44 |
+
lmh=""+str(noa.id)+""
|
| 45 |
+
loris.append(lmh)
|
| 46 |
+
return loris
|
| 47 |
+
|
| 48 |
+
def chdr(apol,prompt,modil,los,stips,gaul):
|
| 49 |
+
try:
|
| 50 |
+
type="SD"
|
| 51 |
+
fnamo=str(int(time.time()))
|
| 52 |
+
flng=["yssup", "sllab", "stsaerb", "sinep", "selppin", "ssa", "tnuc", "mub", "kcoc", "kcid", "anigav", "dekan", "edun", "slatineg", "xes", "nrop", "stit", "ttub", "bojwolb", "noitartenep", "kcuf", "kcus", "kcil",]
|
| 53 |
+
flng=[itm[::-1] for itm in flng]
|
| 54 |
+
ptn = r"\b" + r"\b|\b".join(flng) + r"\b"
|
| 55 |
+
if re.search(ptn, prompt, re.IGNORECASE):
|
| 56 |
+
print("onon buddy")
|
| 57 |
+
else:
|
| 58 |
+
dobj={'img_name':fnamo,'model':modil,'lora':los,'prompt':prompt,'steps':stips,'type':type}
|
| 59 |
+
tre='./tmpo/'+fnamo+'.json'
|
| 60 |
+
with open(tre, 'w') as f:
|
| 61 |
+
json.dump(dobj, f)
|
| 62 |
+
HfApi.upload_folder(repo_id="JoPmt/hf_community_images",folder_path="./tmpo",repo_type="dataset",path_in_repo="./",token=HF_TOKEN)
|
| 63 |
+
dobj={'img_name':fnamo,'model':modil,'lora':los,'prompt':prompt,'steps':stips,'type':type,'haed':gaul,}
|
| 64 |
+
tre='./tmpo/'+fnamo+'.json'
|
| 65 |
+
with open(tre, 'w') as f:
|
| 66 |
+
json.dump(dobj, f)
|
| 67 |
+
HfApi.upload_folder(repo_id="JoPmt/Tst_datast_imgs",folder_path="./tmpo",repo_type="dataset",path_in_repo="./",token=HF_TOKEN)
|
| 68 |
+
except:
|
| 69 |
+
print("failed to umake obj")
|
| 70 |
+
|
| 71 |
+
def crll(dnk):
|
| 72 |
+
lix=""
|
| 73 |
+
lotr=HfApi.list_files_info(repo_id=""+dnk+"",repo_type="model")
|
| 74 |
+
for flre in list(lotr):
|
| 75 |
+
fllr=[]
|
| 76 |
+
gar=re.match(r'.+(\.pt|\.ckpt|\.bin|\.safetensors)$', flre.path)
|
| 77 |
+
yir=re.search(r'[^/]+$', flre.path)
|
| 78 |
+
if gar:
|
| 79 |
+
fllr.append(""+str(yir.group(0))+"")
|
| 80 |
+
lix=""+fllr[-1]+""
|
| 81 |
+
else:
|
| 82 |
+
lix=""
|
| 83 |
+
return lix
|
| 84 |
+
|
| 85 |
+
def plax(gaul,req: gr.Request):
|
| 86 |
+
gaul=str(req.headers)
|
| 87 |
+
return gaul
|
| 88 |
+
|
| 89 |
+
def plex(prompt,neg_prompt,modil,stips,scaly,nut,wei,hei,los,loca,gaul,progress=gr.Progress(track_tqdm=True)):
|
| 90 |
+
gc.collect()
|
| 91 |
+
adi=""
|
| 92 |
+
ldi=""
|
| 93 |
+
try:
|
| 94 |
+
crda=ModelCard.load(""+modil+"")
|
| 95 |
+
card=ModelCard.load(""+modil+"").data.to_dict().get("instance_prompt")
|
| 96 |
+
cerd=ModelCard.load(""+modil+"").data.to_dict().get("custom_prompt")
|
| 97 |
+
cird=ModelCard.load(""+modil+"").data.to_dict().get("lora_prompt")
|
| 98 |
+
mtch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*(.*?)\s*(?=to trigger)', crda.text, re.IGNORECASE)
|
| 99 |
+
moch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*([^.]*)', crda.text, re.IGNORECASE)
|
| 100 |
+
if moch:
|
| 101 |
+
adi+=""+str(moch.group(1))+", "
|
| 102 |
+
else:
|
| 103 |
+
print("no floff trigger")
|
| 104 |
+
if mtch:
|
| 105 |
+
adi+=""+str(mtch.group(1))+", "
|
| 106 |
+
else:
|
| 107 |
+
print("no fluff trigger")
|
| 108 |
+
if card:
|
| 109 |
+
adi+=""+str(card)+", "
|
| 110 |
+
else:
|
| 111 |
+
print("no instance")
|
| 112 |
+
if cerd:
|
| 113 |
+
adi+=""+str(cerd)+", "
|
| 114 |
+
else:
|
| 115 |
+
print("no custom")
|
| 116 |
+
if cird:
|
| 117 |
+
adi+=""+str(cird)+", "
|
| 118 |
+
else:
|
| 119 |
+
print("no lora")
|
| 120 |
+
except:
|
| 121 |
+
print("no card")
|
| 122 |
+
try:
|
| 123 |
+
pipe=accelerator.prepare(DiffusionPipeline.from_pretrained(""+modil+"",torch_dtype=torch.float, variant=None, use_safetensors=True, safety_checker=None))
|
| 124 |
+
except:
|
| 125 |
+
pipe=accelerator.prepare(DiffusionPipeline.from_pretrained(""+modil+"",torch_dtype=torch.float, variant=None, use_safetensors=False, safety_checker=None))
|
| 126 |
+
if los:
|
| 127 |
+
try:
|
| 128 |
+
lrda=ModelCard.load(""+los+"")
|
| 129 |
+
lard=ModelCard.load(""+los+"").data.to_dict().get("instance_prompt")
|
| 130 |
+
lerd=ModelCard.load(""+los+"").data.to_dict().get("custom_prompt")
|
| 131 |
+
lird=ModelCard.load(""+los+"").data.to_dict().get("stable-diffusion")
|
| 132 |
+
ltch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*(.*?)\s*(?=to trigger)', lrda.text, re.IGNORECASE)
|
| 133 |
+
loch=re.search(r'(?:(?<=trigger words:)|(?<=trigger:)|(?<=You could use)|(?<=You should use))\s*([^.]*)', lrda.text, re.IGNORECASE)
|
| 134 |
+
if loch and lird:
|
| 135 |
+
ldi+=""+str(loch.group(1))+", "
|
| 136 |
+
else:
|
| 137 |
+
print("no lloff trigger")
|
| 138 |
+
if ltch and lird:
|
| 139 |
+
ldi+=""+str(ltch.group(1))+", "
|
| 140 |
+
else:
|
| 141 |
+
print("no lluff trigger")
|
| 142 |
+
if lard and lird:
|
| 143 |
+
ldi+=""+str(lard)+", "
|
| 144 |
+
else:
|
| 145 |
+
print("no instance")
|
| 146 |
+
ldi+=""
|
| 147 |
+
if lerd and lird:
|
| 148 |
+
ldi+=""+str(lerd)+", "
|
| 149 |
+
else:
|
| 150 |
+
print("no custom")
|
| 151 |
+
ldi+=""
|
| 152 |
+
except:
|
| 153 |
+
print("no trigger")
|
| 154 |
+
try:
|
| 155 |
+
pipe.load_lora_weights(""+los+"", weight_name=""+str(crll(los))+"",)
|
| 156 |
+
pipe.fuse_lora(fuse_unet=True,fuse_text_encoder=False)
|
| 157 |
+
except:
|
| 158 |
+
print("no can do")
|
| 159 |
+
else:
|
| 160 |
+
los=""
|
| 161 |
+
pipe.unet.to(memory_format=torch.channels_last)
|
| 162 |
+
pipe.to("cpu")
|
| 163 |
+
gc.collect()
|
| 164 |
+
apol=[]
|
| 165 |
+
lora_scale=loca
|
| 166 |
+
if nut == 0:
|
| 167 |
+
nm = random.randint(1, 2147483616)
|
| 168 |
+
while nm % 32 != 0:
|
| 169 |
+
nm = random.randint(1, 2147483616)
|
| 170 |
+
else:
|
| 171 |
+
nm=nut
|
| 172 |
+
generator = torch.Generator(device="cpu").manual_seed(nm)
|
| 173 |
+
image = pipe(prompt=""+str(adi)+str(ldi)+prompt+"", negative_prompt=neg_prompt, generator=generator, num_inference_steps=stips, guidance_scale=scaly, width=wei, height=hei, cross_attention_kwargs={"scale": lora_scale})
|
| 174 |
+
for a, imze in enumerate(image["images"]):
|
| 175 |
+
apol.append(imze)
|
| 176 |
+
imze.save('./tmpo/'+str(int(time.time()))+'.png', 'PNG')
|
| 177 |
+
chdr(apol,prompt,modil,los,stips,gaul)
|
| 178 |
+
return apol
|
| 179 |
+
|
| 180 |
+
def aip(ill,api_name="/run"):
|
| 181 |
+
return
|
| 182 |
+
def pit(ill,api_name="/predict"):
|
| 183 |
+
return
|
| 184 |
+
|
| 185 |
+
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:
|
| 186 |
+
iface.description="Running on cpu, very slow! by JoPmt."
|
| 187 |
+
out=gr.Gallery(label="Generated Output Image", columns=1)
|
| 188 |
+
inut=gr.Textbox(label="Prompt")
|
| 189 |
+
gaul=gr.Textbox(visible=False)
|
| 190 |
+
inot=gr.Dropdown(choices=smdls(models),value=random.choice(models), type="value")
|
| 191 |
+
btn=gr.Button("GENERATE")
|
| 192 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 193 |
+
inlt=gr.Dropdown(choices=sldls(loris),value=None, type="value")
|
| 194 |
+
inet=gr.Textbox(label="Negative_prompt", value="low quality, bad quality,")
|
| 195 |
+
inyt=gr.Slider(label="Num inference steps",minimum=1,step=1,maximum=30,value=20)
|
| 196 |
+
inat=gr.Slider(label="Guidance_scale",minimum=1,step=1,maximum=20,value=7)
|
| 197 |
+
loca=gr.Slider(label="Lora scale",minimum=0.1,step=0.1,maximum=0.9,value=0.5)
|
| 198 |
+
indt=gr.Slider(label="Manual seed (leave 0 for random)",minimum=0,step=32,maximum=2147483616,value=0)
|
| 199 |
+
inwt=gr.Slider(label="Width",minimum=512,step=32,maximum=1024,value=512)
|
| 200 |
+
inht=gr.Slider(label="Height",minimum=512,step=32,maximum=1024,value=512)
|
| 201 |
+
|
| 202 |
+
btn.click(fn=plax,inputs=gaul,outputs=gaul,).then(
|
| 203 |
+
fn=plex, outputs=[out], inputs=[inut, inet, inot, inyt, inat, indt, inwt, inht, inlt, loca, gaul])
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
iface.queue(max_size=1,api_open=False)
|
| 207 |
+
iface.launch(max_threads=10,inline=False,show_api=False)
|