JoPmt commited on
Commit
f98e9ca
1 Parent(s): 58baa83

Update app.py

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