JoPmt commited on
Commit
3f4f255
1 Parent(s): f04a638

Update app.py

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