John6666 commited on
Commit
a1b31a2
1 Parent(s): 2a4be46

Upload genimage.py

Browse files
Files changed (1) hide show
  1. genimage.py +66 -0
genimage.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+
3
+
4
+ def load_pipeline():
5
+ from diffusers import StableDiffusionXLPipeline
6
+ import torch
7
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
8
+ pipe = StableDiffusionXLPipeline.from_pretrained(
9
+ "John6666/rae-diffusion-xl-v2-sdxl-spo-pcm",
10
+ custom_pipeline="lpw_stable_diffusion_xl",
11
+ #custom_pipeline="nyanko7/sdxl_smoothed_energy_guidance",
12
+ torch_dtype=torch.float16,
13
+ )
14
+ pipe.to(device)
15
+ return pipe
16
+
17
+
18
+ def save_image(image, metadata, output_dir):
19
+ import os
20
+ import uuid
21
+ import json
22
+ from PIL import PngImagePlugin
23
+ filename = str(uuid.uuid4()) + ".png"
24
+ os.makedirs(output_dir, exist_ok=True)
25
+ filepath = os.path.join(output_dir, filename)
26
+ metadata_str = json.dumps(metadata)
27
+ info = PngImagePlugin.PngInfo()
28
+ info.add_text("metadata", metadata_str)
29
+ image.save(filepath, "PNG", pnginfo=info)
30
+ return filepath
31
+
32
+
33
+ pipe = load_pipeline()
34
+
35
+
36
+ @spaces.GPU
37
+ def generate_image(prompt, neg_prompt):
38
+ metadata = {
39
+ "prompt": prompt + ", anime, masterpiece, best quality, very aesthetic, absurdres",
40
+ "negative_prompt": neg_prompt + ", bad hands, bad feet, lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract], photo, deformed, disfigured, low contrast, photo, deformed, disfigured, low contrast",
41
+ "resolution": f"{1024} x {1024}",
42
+ "guidance_scale": 7.0,
43
+ "num_inference_steps": 28,
44
+ "sampler": "Euler",
45
+ }
46
+ try:
47
+ images = pipe(
48
+ prompt=prompt + ", anime, masterpiece, best quality, very aesthetic, absurdres",
49
+ negative_prompt=neg_prompt + ", bad hands, bad feet, lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract], photo, deformed, disfigured, low contrast, photo, deformed, disfigured, low contrast",
50
+ width=1024,
51
+ height=1024,
52
+ guidance_scale=7.0,# seg_scale=3.0, seg_applied_layers=["mid"],
53
+ num_inference_steps=28,
54
+ output_type="pil",
55
+ clip_skip=1,
56
+ ).images
57
+ if images:
58
+ image_paths = [
59
+ save_image(image, metadata, "./outputs")
60
+ for image in images
61
+ ]
62
+ return image_paths
63
+ except Exception as e:
64
+ print(e)
65
+ return []
66
+