File size: 7,313 Bytes
2d52fac
d6e73d2
b3692f8
 
e5ab631
 
b3692f8
2d52fac
c493374
e5ab631
 
 
 
 
 
 
2c1a223
 
 
e5ab631
2c1a223
 
e5ab631
 
 
 
 
 
 
 
 
 
 
 
 
 
c50baf3
c493374
c50baf3
c493374
c50baf3
 
 
 
 
 
 
 
c493374
 
c50baf3
c493374
c50baf3
 
 
 
 
 
 
 
 
e5ab631
 
 
 
 
d6e73d2
c50baf3
 
 
 
 
e5ab631
 
 
 
 
 
1f065a3
e5ab631
 
 
 
 
 
 
 
 
d6e73d2
e5ab631
 
 
 
 
 
 
d6e73d2
e5ab631
 
 
 
 
 
 
d6e73d2
e5ab631
 
 
 
2d52fac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6e73d2
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import shutil

import gradio as gr

import torch
from diffusers import DiffusionPipeline

import wandb
from wandb.integration.diffusers import autolog


device = "cuda" if torch.cuda.is_available() else "cpu"

if torch.cuda.is_available():
    torch.cuda.empty_cache()

pipeline = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
).to(device)

if torch.cuda.is_available():
    pipeline.enable_xformers_memory_efficient_attention()


def generate_image(
    wandb_project,
    wandb_api_key,
    prompt,
    negative_prompt,
    height,
    width,
    num_inference_steps,
    guidance_scale,
    seed,
    guidance_rescale,
):
    if not (wandb_api_key is None or wandb_api_key == ""):
        wandb.login(key=wandb_api_key, relogin=True)
        generator = torch.Generator(device="cuda").manual_seed(seed)
        autolog(init={"project": wandb_project})
        run_url = wandb.run.get_url()
        image = pipeline(
            prompt,
            negative_prompt=negative_prompt,
            generator=generator,
            height=height,
            width=width,
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
            guidance_rescale=guidance_rescale,
        ).images[0]
        wandb.finish()
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        shutil.rmtree("wandb")
        return image, f"**WandB Run:** [{run_url}]({run_url})"
    else:
        return (
            None,
            "A WandB API key is required to run this app! You can get one from [https://wandb.ai/authorize](https://wandb.ai/authorize)",
        )


gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="WandB Project Name", value="Stable-Diffusion-XL"),
        gr.Textbox(
            label="WandB API Key (You can get one from https://wandb.ai/authorize)",
            type="password",
            value=None,
        ),
        gr.Textbox(
            label="Prompt",
            lines=3,
            value="a woman in orange, extremely detailed digital painting, in the style of Fenghua Zhong and Ruan Jia and jeremy lipking and Peter Mohrbacher, mystical colors, rim light, beautiful Lighting, 8k, stunning scene, raytracing, octane, trending on artstation",
        ),
        gr.Textbox(
            label="Negative Prompt",
            lines=3,
            value="blurry, plastic, grainy, duplicate, deformed , disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limb, disconnected limb, mutated hands and fingers, text, name, signature, watermark, worst quality, jpeg artifacts, boring composition, uninteresting",
        ),
        gr.Slider(minimum=512, maximum=1024, value=1024, step=128, label="Height"),
        gr.Slider(minimum=512, maximum=1024, value=1024, step=128, label="Width"),
        gr.Slider(
            minimum=10,
            maximum=100,
            value=50,
            step=10,
            label="Number of Inference Steps",
        ),
        gr.Slider(
            minimum=1.0,
            maximum=15.0,
            value=5.0,
            step=0.25,
            label="Guidance Scale (How Closely the model follows the Prompt)",
        ),
        gr.Slider(
            minimum=0, step=1, maximum=999999999999999999, randomize=True, label="Seed"
        ),
        gr.Slider(
            minimum=0.0,
            maximum=1.0,
            value=0.0,
            step=0.1,
            label="Guidance Rescale Factor",
        ),
    ],
    outputs=["image", "markdown"],
    title="Reproducible Stable Diffusion-XL with WandB",
    description="""
    This space enables us to generate images using Stable Diffusion XL and track your experiments using Weights & Biases. In order to learn more able engineering better prompts for Stable Diffusion models and how to make your experiments reproducible using [Weights & Biases](https://wandb.ai/site), check out [this report](https://wandb.ai/geekyrakshit/diffusers-prompt-engineering/reports/A-Guide-to-Prompt-Engineering-for-Diffusion-Models--Vmlldzo1NzY4NzQ3).
    This app is powered by [🤗 Diffusers](https://huggingface.co/docs/diffusers) and [Weights & Biases](https://wandb.ai/site). A **Weights & Biases API key is necessary** to run this app, which you can find at **[https://wandb.ai/authorize](https://wandb.ai/authorize)**.
    """,
    examples=[
        [
            "Stable-Diffusion-XL",
            None,
            "a woman in orange, extremely detailed digital painting, in the style of Fenghua Zhong and Ruan Jia and jeremy lipking and Peter Mohrbacher, mystical colors, rim light, beautiful Lighting, 8k, stunning scene, raytracing, octane, trending on artstation",
            "blurry, plastic, grainy, duplicate, deformed , disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limb, disconnected limb, mutated hands and fingers, text, name, signature, watermark, worst quality, jpeg artifacts, boring composition, uninteresting",
            1024,
            1024,
            50,
            5.0,
            256174057,
            0.0,
        ],
        [
            "Stable-Diffusion-XL",
            None,
            "hyper realistic photograph, Kodak disposable photography of young queen sit on her throne, blonde hair, red lips, white skin, 8k",
            None,
            1024,
            1024,
            50,
            5.0,
            189708808,
            0.0,
        ],
        [
            "Stable-Diffusion-XL",
            None,
            "hyper realistic waist up portrait of a teen boy with messy dark blond hair, light brown eyes, smiling, dimples, textured skin, realistic features, imperfect skin, 85mm lens Sony Lens, bokeh, sharp focus, professional photography, warm lighting, soft lights, realistic sharp eyes, sharp focus, set a busy street in the evening as background",
            None,
            1024,
            1024,
            50,
            5.0,
            267243010,
            0.0,
        ],
        [
            "Stable-Diffusion-XL",
            None,
            "A Cinematic Film Still Food Photography of a Cocktail, shot on fujifilm xt4, Professional Advertisement Photography, Dynamic Effects, Foaming Gas, Epic, Beautiful Details, Mysterious micro effects, Highly Detailed",
            None,
            1024,
            1024,
            50,
            5.0,
            1073711987,
            0.0,
        ],
        [
            "Stable-Diffusion-XL",
            None,
            "hyper realistic photograph, photography of a children dressed like a gangster, 50 mm, film grain, Kodak portra 800",
            None,
            1024,
            1024,
            50,
            5.0,
            355517910,
            0.0,
        ],
        [
            "Stable-Diffusion-XL",
            None,
            "a girl as personification of chocolate cupcake",
            "deformed, disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limb, disconnected limb, mutated hands and fingers, blurry",
            1024,
            1024,
            50,
            5.0,
            595687707,
            0.0,
        ],
    ],
).launch(debug=True, max_threads=80)