JoPmt commited on
Commit
4b754eb
1 Parent(s): e5da75b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionXLPipeline, 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(StableDiffusionXLPipeline.from_pretrained("Lykon/dreamshaper-xl-lightning", torch_dtype=torch.bfloat16, use_safetensors=True, variant=None, safety_checker=False))
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="Lkn_drmshpr_XL_lghtng"
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="Lykon/dreamshaper-xl-lightning"
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, negative_prompt=[neg_prompt]*2, generator=generator, denoising_end=1.0,num_inference_steps=stips, output_type="pil",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=2,step=1,maximum=10,value=4)
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)