JoPmt commited on
Commit
5a02deb
1 Parent(s): c0533c2

Update app.py

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