JoPmt commited on
Commit
10882d8
1 Parent(s): 0d5a242

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionLDM3DPipeline, DDIMScheduler
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(StableDiffusionLDM3DPipeline.from_pretrained("Intel/ldm3d-pano"), torch_dtype=torch.bfloat16, variant=None, use_safetensors=False, safety_checker=None))
23
+ pipe.scheduler = DDIMScheduler.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="LDM3D"
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="Intel/ldm3d-pano"
69
+ fnamo=""+str(int(time.time()))+""
70
+ prompt="360 view of a "+prompt+""
71
+ if nut == 0:
72
+ nm = random.randint(1, 2147483616)
73
+ while nm % 32 != 0:
74
+ nm = random.randint(1, 2147483616)
75
+ else:
76
+ nm=nut
77
+ generator = torch.Generator(device="cpu").manual_seed(nm)
78
+ image = pipe(prompt=[prompt]*2, negative_prompt=[neg_prompt]*2, generator=generator, guidance_scale=7.0, num_inference_steps=stips,height=het,width=wit)
79
+ for a, imze in enumerate(image["rgb"]):
80
+ apol.append(imze)
81
+ imze.save('./tmpo/'+fnamo+'_'+str(a)+'.png', 'PNG')
82
+ for b, imbe in enumerate(image["depth"]):
83
+ apol.append(imbe)
84
+ imbe.save('./tmpo/'+fnamo+'_'+str(b)+'.png', 'PNG')
85
+ chdr(apol,prompt,modil,stips,fnamo,gaul)
86
+ return apol
87
+
88
+ def aip(ill,api_name="/run"):
89
+ return
90
+ def pit(ill,api_name="/predict"):
91
+ return
92
+
93
+ 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:
94
+ ##iface.description="Running on cpu, very slow! by JoPmt."
95
+ out=gr.Gallery(label="Generated Output Image", columns=1)
96
+ inut=gr.Textbox(label="Prompt")
97
+ gaul=gr.Textbox(visible=False)
98
+ btn=gr.Button("GENERATE")
99
+ with gr.Accordion("Advanced Settings", open=False):
100
+ 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")
101
+ inyt=gr.Slider(label="Num inference steps",minimum=1,step=1,maximum=30,value=20)
102
+ indt=gr.Slider(label="Manual seed (leave 0 for random)",minimum=0,step=32,maximum=2147483616,value=0)
103
+ inwt=gr.Slider(label="Width",minimum=256,step=32,maximum=1024,value=1024)
104
+ inht=gr.Slider(label="Height",minimum=256,step=32,maximum=1024,value=512)
105
+
106
+ btn.click(fn=plax,inputs=gaul,outputs=gaul).then(fn=plex, outputs=[out], inputs=[inut,inet,inyt,indt,inwt,inht,gaul])
107
+
108
+ iface.queue(max_size=1,api_open=False)
109
+ iface.launch(max_threads=20,inline=False,show_api=False)