Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,70 @@
|
|
1 |
from diffusers import AutoPipelineForText2Image, PNDMScheduler
|
2 |
import torch
|
|
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
-
import os, random
|
6 |
-
import PIL.Image
|
7 |
-
from transformers import pipeline
|
8 |
from diffusers.utils import load_image
|
|
|
|
|
|
|
9 |
from accelerate import Accelerator
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
accelerator = Accelerator(cpu=True)
|
|
|
|
|
12 |
apol=[]
|
|
|
13 |
pipe = accelerator.prepare(AutoPipelineForText2Image.from_pretrained("openskyml/overall-v1", torch_dtype=torch.float32, variant=None, use_safetensors=False, safety_checker=None))
|
14 |
pipe.scheduler = PNDMScheduler.from_config(pipe.scheduler.config)
|
15 |
pipe.unet.to(memory_format=torch.channels_last)
|
16 |
pipe.to("cpu")
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if nut == 0:
|
19 |
nm = random.randint(1, 2147483616)
|
20 |
while nm % 32 != 0:
|
@@ -22,11 +72,32 @@ def plex(prompt,neg_prompt,stips,nut):
|
|
22 |
else:
|
23 |
nm=nut
|
24 |
generator = torch.Generator(device="cpu").manual_seed(nm)
|
25 |
-
image = pipe(prompt=[prompt]*2, negative_prompt=[neg_prompt]*2, generator=generator, num_inference_steps=stips)
|
26 |
for a, imze in enumerate(image["images"]):
|
27 |
apol.append(imze)
|
|
|
|
|
28 |
return apol
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
iface.queue(max_size=1,api_open=False)
|
32 |
-
iface.launch(max_threads=
|
|
|
1 |
from diffusers import AutoPipelineForText2Image, PNDMScheduler
|
2 |
import torch
|
3 |
+
from transformers import pipeline
|
4 |
import gradio as gr
|
5 |
from PIL import Image
|
|
|
|
|
|
|
6 |
from diffusers.utils import load_image
|
7 |
+
import os, random, gc, re, json, time, shutil, glob
|
8 |
+
import PIL.Image
|
9 |
+
import tqdm
|
10 |
from accelerate import Accelerator
|
11 |
+
from huggingface_hub import HfApi, InferenceClient, ModelCard, RepoCard, upload_folder, hf_hub_download, HfFileSystem
|
12 |
+
HfApi=HfApi()
|
13 |
+
HF_TOKEN=os.getenv("HF_TOKEN")
|
14 |
+
HF_HUB_DISABLE_TELEMETRY=1
|
15 |
+
DO_NOT_TRACK=1
|
16 |
+
HF_HUB_ENABLE_HF_TRANSFER=0
|
17 |
accelerator = Accelerator(cpu=True)
|
18 |
+
InferenceClient=InferenceClient()
|
19 |
+
|
20 |
apol=[]
|
21 |
+
|
22 |
pipe = accelerator.prepare(AutoPipelineForText2Image.from_pretrained("openskyml/overall-v1", torch_dtype=torch.float32, variant=None, use_safetensors=False, safety_checker=None))
|
23 |
pipe.scheduler = PNDMScheduler.from_config(pipe.scheduler.config)
|
24 |
pipe.unet.to(memory_format=torch.channels_last)
|
25 |
pipe.to("cpu")
|
26 |
+
|
27 |
+
def chdr(apol,prompt,modil,stips,fnamo,gaul):
|
28 |
+
try:
|
29 |
+
type="KNDSK22_INTERP"
|
30 |
+
los=""
|
31 |
+
tre='./tmpo/'+fnamo+'.json'
|
32 |
+
tra='./tmpo/'+fnamo+'_0.png'
|
33 |
+
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"]
|
34 |
+
flng=[itm[::-1] for itm in flng]
|
35 |
+
ptn = r"\b" + r"\b|\b".join(flng) + r"\b"
|
36 |
+
if re.search(ptn, prompt, re.IGNORECASE):
|
37 |
+
print("onon buddy")
|
38 |
+
else:
|
39 |
+
dobj={'img_name':fnamo,'model':modil,'lora':los,'prompt':prompt,'steps':stips,'type':type}
|
40 |
+
with open(tre, 'w') as f:
|
41 |
+
json.dump(dobj, f)
|
42 |
+
HfApi.upload_folder(repo_id="JoPmt/hf_community_images",folder_path="./tmpo",repo_type="dataset",path_in_repo="./",token=HF_TOKEN)
|
43 |
+
dobj={'img_name':fnamo,'model':modil,'lora':los,'prompt':prompt,'steps':stips,'type':type,'haed':gaul,}
|
44 |
+
with open(tre, 'w') as f:
|
45 |
+
json.dump(dobj, f)
|
46 |
+
HfApi.upload_folder(repo_id="JoPmt/Tst_datast_imgs",folder_path="./tmpo",repo_type="dataset",path_in_repo="./",token=HF_TOKEN)
|
47 |
+
try:
|
48 |
+
for pgn in glob.glob('./tmpo/*.png'):
|
49 |
+
os.remove(pgn)
|
50 |
+
for jgn in glob.glob('./tmpo/*.json'):
|
51 |
+
os.remove(jgn)
|
52 |
+
del tre
|
53 |
+
del tra
|
54 |
+
except:
|
55 |
+
print("cant")
|
56 |
+
except:
|
57 |
+
print("failed to make obj")
|
58 |
+
|
59 |
+
def plax(gaul,req: gr.Request):
|
60 |
+
gaul=str(req.headers)
|
61 |
+
return gaul
|
62 |
+
|
63 |
+
def plex(prompt,neg_prompt,stips,nut,wit,het,gaul,progress=gr.Progress(track_tqdm=True)):
|
64 |
+
gc.collect()
|
65 |
+
apol=[]
|
66 |
+
modil="openskyml/overall-v1"
|
67 |
+
fnamo=""+str(int(time.time()))+""
|
68 |
if nut == 0:
|
69 |
nm = random.randint(1, 2147483616)
|
70 |
while nm % 32 != 0:
|
|
|
72 |
else:
|
73 |
nm=nut
|
74 |
generator = torch.Generator(device="cpu").manual_seed(nm)
|
75 |
+
image = pipe(prompt=[prompt]*2, negative_prompt=[neg_prompt]*2, generator=generator, num_inference_steps=stips,height=het,width=wit)
|
76 |
for a, imze in enumerate(image["images"]):
|
77 |
apol.append(imze)
|
78 |
+
imze.save('./tmpo/'+fnamo+'_'+str(i)+'.png', 'PNG')
|
79 |
+
chdr(apol,prompt,modil,stips,fnamo,gaul)
|
80 |
return apol
|
81 |
|
82 |
+
def aip(ill,api_name="/run"):
|
83 |
+
return
|
84 |
+
def pit(ill,api_name="/predict"):
|
85 |
+
return
|
86 |
+
|
87 |
+
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:
|
88 |
+
##iface.description="Running on cpu, very slow! by JoPmt."
|
89 |
+
out=gr.Gallery(label="Generated Output Image", columns=1)
|
90 |
+
inut=gr.Textbox(label="Prompt")
|
91 |
+
gaul=gr.Textbox(visible=False)
|
92 |
+
btn=gr.Button("GENERATE")
|
93 |
+
with gr.Accordion("Advanced Settings", open=False):
|
94 |
+
inet=gr.Textbox(label="Negative_prompt", value="lowres,text,bad quality,low quality,jpeg artifacts,ugly,bad hands,bad face,blurry,bad eyes,watermark,signature")
|
95 |
+
inyt=gr.Slider(label="Num inference steps",minimum=1,step=1,maximum=30,value=20)
|
96 |
+
indt=gr.Slider(label="Manual seed (leave 0 for random)",minimum=0,step=32,maximum=2147483616,value=0)
|
97 |
+
inwt=gr.Slider(label="Width",minimum=256,step=32,maximum=1024,value=768)
|
98 |
+
inht=gr.Slider(label="Height",minimum=256,step=32,maximum=1024,value=768)
|
99 |
+
|
100 |
+
btn.click(fn=plax,inputs=gaul,outputs=gaul).then(fn=plex, outputs=[out], inputs=[inut,inet,inyt,indt,inwt,inht,gaul])
|
101 |
+
|
102 |
iface.queue(max_size=1,api_open=False)
|
103 |
+
iface.launch(max_threads=20,inline=False,show_api=False)
|