JoPmt commited on
Commit
ce48c34
1 Parent(s): 6dd7f71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -12
app.py CHANGED
@@ -1,20 +1,72 @@
1
- from diffusers import AutoPipelineForText2Image, DiffusionPipeline, StableDiffusionPipeline, UniPCMultistepScheduler, EulerAncestralDiscreteScheduler
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
- pipe = accelerator.prepare(StableDiffusionPipeline.from_single_file("https://huggingface.co/lllyasviel/fav_models/fav/DreamShaper_8_pruned.safetensors",torch_dtype=torch.float32, variant=None, use_safetensors=True, safety_checker=None))
13
- ##pipe.scheduler = accelerator.prepare(EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config))
 
 
 
 
14
  pipe.unet.to(memory_format=torch.channels_last)
15
- pipe.to("cpu")
16
- def plex(prompt,neg_prompt,stips,scaly,nut):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  apol=[]
 
 
18
  if nut == 0:
19
  nm = random.randint(1, 2147483616)
20
  while nm % 32 != 0:
@@ -22,11 +74,32 @@ def plex(prompt,neg_prompt,stips,scaly,nut):
22
  else:
23
  nm=nut
24
  generator = torch.Generator(device="cpu").manual_seed(nm)
25
- image = pipe(prompt=prompt, negative_prompt=neg_prompt, generator=generator, num_inference_steps=stips, guidance_scale=scaly)
26
  for a, imze in enumerate(image["images"]):
27
  apol.append(imze)
 
 
28
  return apol
29
 
30
- iface = gr.Interface(fn=plex,inputs=[gr.Textbox(label="Prompt"), gr.Textbox(label="negative_prompt", value="low quality, bad quality"), gr.Slider(label="num inference steps",minimum=1,step=1,maximum=30,value=25), gr.Slider(label="guidance_scale",minimum=1,step=1,maximum=10,value=7),gr.Slider(label="manual seed (leave 0 for random)",minimum=0,step=32,maximum=2147483616,value=0)],outputs=gr.Gallery(label="Generated Output Image", columns=1),description="Running on cpu, very slow! by JoPmt.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  iface.queue(max_size=1,api_open=False)
32
- iface.launch(max_threads=1)
 
1
+ from diffusers import AutoPipelineForText2Image, PNDMScheduler, StableDiffusionPipeline, EulerAncestralDiscreteScheduler
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(StableDiffusionPipeline.from_single_file("https://huggingface.co/lllyasviel/fav_models/fav/DreamShaper_8_pruned.safetensors", torch_dtype=torch.float32, variant=None, use_safetensors=True, safety_checker=None))
23
+ pipe.scheduler = EulerAncestralDiscreteScheduler.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="DrmShpr_SD"
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,neg_prompt,stips,nut,wit,het,gaul,progress=gr.Progress(track_tqdm=True)):
66
+ gc.collect()
67
  apol=[]
68
+ modil="DreamShaper_8_pruned"
69
+ fnamo=""+str(int(time.time()))+""
70
  if nut == 0:
71
  nm = random.randint(1, 2147483616)
72
  while nm % 32 != 0:
 
74
  else:
75
  nm=nut
76
  generator = torch.Generator(device="cpu").manual_seed(nm)
77
+ image = pipe(prompt=[prompt]*2, negative_prompt=[neg_prompt]*2, generator=generator, num_inference_steps=stips,height=het,width=wit)
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
+ 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")
97
+ inyt=gr.Slider(label="Num inference steps",minimum=1,step=1,maximum=30,value=20)
98
+ indt=gr.Slider(label="Manual seed (leave 0 for random)",minimum=0,step=32,maximum=2147483616,value=0)
99
+ inwt=gr.Slider(label="Width",minimum=256,step=32,maximum=1024,value=768)
100
+ inht=gr.Slider(label="Height",minimum=256,step=32,maximum=1024,value=768)
101
+
102
+ btn.click(fn=plax,inputs=gaul,outputs=gaul).then(fn=plex, outputs=[out], inputs=[inut,inet,inyt,indt,inwt,inht,gaul])
103
+
104
  iface.queue(max_size=1,api_open=False)
105
+ iface.launch(max_threads=20,inline=False,show_api=False)