File size: 4,182 Bytes
24a6868
 
a9cbac4
24a6868
 
 
 
 
 
 
ccacd4d
ceadb77
dc704d6
ccacd4d
24a6868
 
858c6b5
 
 
 
 
 
24a6868
 
 
 
 
 
 
 
ceadb77
24a6868
 
 
 
 
e413311
406a75d
dd96114
e413311
c1281b8
39c9ce9
24a6868
 
6095378
 
 
 
 
 
 
 
7de4c3e
c7f68b4
 
 
 
0563fad
24a6868
 
 
 
 
 
 
 
e9947e2
24a6868
0563fad
 
e9947e2
24a6868
0563fad
24a6868
d62c968
5a7e564
24a6868
 
 
c6a4957
e9947e2
ce0c3b0
ebc69eb
0563fad
24a6868
0563fad
24a6868
 
3eeb179
c7f68b4
ffaf784
 
24a6868
3389734
 
 
00acfc9
7de4c3e
6095378
00acfc9
 
 
6095378
 
 
 
 
5355c9b
6095378
e9947e2
6095378
 
3389734
24a6868
6095378
e7066e9
24a6868
e9947e2
3389734
24a6868
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Imports
import gradio as gr
import subprocess
import random
import spaces
import torch
import numpy
import uuid
import json
import os

from diffusers import StableDiffusionXLPipeline, ControlNetModel
from diffusers.models import AutoencoderKL
from PIL import Image

# Pre-Initialize
subprocess.run(
    "pip install flash-attn --no-build-isolation",
    env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
    shell=True,
)

DEVICE = "auto"
if DEVICE == "auto":
    DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"[SYSTEM] | Using {DEVICE} type compute device.")

# Variables
MAX_SEED = 9007199254740991
DEFAULT_INPUT = ""
DEFAULT_NEGATIVE_INPUT = "deformed, distorted, disfigured, disconnected, disgusting, mutation, mutated, blur, blurry, scribble, abstract, ugly, amputation, limb, limbs, leg, legs, foot, feet, toe, toes, arm, arms, hand, hands, finger, fingers, head, heads, exposed, porn, nude, nudity, naked, nsfw, NSFW"
DEFAULT_HEIGHT = 1024
DEFAULT_WIDTH = 1024

REPO = "sd-community/sdxl-flash"

vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
controlnet = ControlNetModel.from_pretrained("MakiPan/controlnet-encoded-hands-130k", torch_dtype=torch.float16)

model = StableDiffusionXLPipeline.from_pretrained(REPO, vae=vae, controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True, add_watermarker=False)
model.load_lora_weights("ehristoforu/dalle-3-xl-v2", adapter_name="base")
model.set_adapters(["base"], adapter_weights=[0.7])
model.to(DEVICE)

css = '''
.gradio-container{max-width: 560px !important}
h1{text-align:center}
footer {
    visibility: hidden
}
'''

# Functions
def save_image(img, seed):
    name = f"{seed}-{uuid.uuid4()}.png"
    img.save(name)
    return name
    
def get_seed(seed):
    seed = seed.strip()
    if seed.isdigit():
        return int(seed)
    else:
        return random.randint(0, MAX_SEED)

@spaces.GPU(duration=30)
def generate(input=DEFAULT_INPUT, negative_input=DEFAULT_NEGATIVE_INPUT, height=DEFAULT_HEIGHT, width=DEFAULT_WIDTH, steps=1, guidance=0, number=1, seed=None):
    
    seed = get_seed(seed)

    print(input, negative_input, height, width, steps, guidance, number, seed)

    model.to(DEVICE)
    parameters  = {
        "prompt": input,
        "negative_prompt": negative_input,
        "height": height,
        "width": width,
        "num_inference_steps": steps,
        "guidance_scale": guidance,
        "num_images_per_prompt": number,
        "controlnet_conditioning_scale": 1,
        "cross_attention_kwargs": {"scale": 1},
        "generator": torch.Generator().manual_seed(seed),
        "use_resolution_binning": True,
        "output_type":"pil",
    }
    
    images = model(**parameters).images
    image_paths = [save_image(img, seed) for img in images]
    print(image_paths)
    return image_paths

def cloud():
    print("[CLOUD] | Space maintained.")


# Initialize
with gr.Blocks(css=css) as main:
    with gr.Column():
        gr.Markdown("🪄 Generate high quality images on all styles.")
        
    with gr.Column():
        input = gr.Textbox(lines=1, value=DEFAULT_INPUT, label="Input")
        negative_input = gr.Textbox(lines=1, value=DEFAULT_NEGATIVE_INPUT, label="Input Negative")
        height = gr.Slider(minimum=1, maximum=2160, step=1, value=DEFAULT_HEIGHT, label="Height")
        width = gr.Slider(minimum=1, maximum=2160, step=1, value=DEFAULT_WIDTH, label="Width")
        steps = gr.Slider(minimum=0, maximum=100, step=1, value=15, label="Steps")
        guidance = gr.Slider(minimum=0, maximum=100, step=0.001, value=3, label = "Guidance")
        number = gr.Slider(minimum=1, maximum=4, step=1, value=1, label="Number")
        seed = gr.Textbox(lines=1, value="", label="Seed (Blank for random)")
        submit = gr.Button("▶")
        maintain = gr.Button("☁️")

    with gr.Column():
        images = gr.Gallery(columns=1, label="Image")
            
    submit.click(generate, inputs=[input, negative_input, height, width, steps, guidance, number, seed], outputs=[images], queue=False)
    maintain.click(cloud, inputs=[], outputs=[], queue=False)

main.launch(show_api=True)