File size: 4,342 Bytes
ed2a56d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8da2b98
 
ed2a56d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import random
from diffusers import DiffusionPipeline
import torch

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

if torch.cuda.is_available():
    torch_dtype = torch.float16
else:
    torch_dtype = torch.float32

model_turbo_repo_id = "stabilityai/sdxl-turbo"
model_repo_id = "stabilityai/stable-diffusion-2"
pipe = DiffusionPipeline.from_pretrained(model_turbo_repo_id, torch_dtype=torch_dtype)
pipe = pipe.to(device)

# Recommended if your computer has < 64 GB of RAM
pipe.enable_attention_slicing()

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024


def create_generator(seed):
    return torch.Generator().manual_seed(seed)


def generate(prompt, negative_prompt, seed, is_random_seed, width, height, guidance_scale, inference_steps):
    if is_random_seed:
        seed = random.randint(0, np.iinfo(np.int32).max)

    generator = create_generator(seed)

    return pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        guidance_scale=guidance_scale,
        num_inference_steps=inference_steps,
        width=width,
        height=height,
        generator=generator
    ).images[0]


with gr.Blocks() as imageGenerator:
    with gr.Column():
        gr.Markdown(f"""
        # Zatsit Image Generator
        ## Générateur d'image basé sur des modèles de stable diffusion
        Vous cherchez de l'inspiration pour vos prompts ? 
        [lien](https://stablediffusion.fr/prompts)
        """)

        with gr.Row():
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=4,
                placeholder="Votre prompt",
                container=False,
            )

            generate_btn = gr.Button("Générer", scale=0)

        result = gr.Image(label="Image générée", show_label=False)

        with gr.Accordion("Paramètres", open=True):
            negative_prompt = gr.Text(
                label="Negative prompt",
                max_lines=4,
                placeholder="Votre prompt négatif",
            )

            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=70537634,
            )

            is_random_seed = gr.Checkbox(label="Seed en mode random", value=False)

            width = gr.Slider(
                label="Width",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=10,
                value=1024,
            )

            height = gr.Slider(
                label="Height",
                minimum=256,
                maximum=MAX_IMAGE_SIZE,
                step=10,
                value=1024,
            )

            guidance_scale = gr.Slider(
                label="Guildance scale",
                minimum=0,
                maximum=10.0,
                step=0.1,
                value=0.0,
            )

            inference_steps = gr.Slider(
                label="Nombre d'inférences",
                minimum=0,
                maximum=100.0,
                step=1,
                value=2,
            )

        gr.Examples(
            examples=[
                ["pikachu eating spagetti, Antonio J. Manzanedo", 3384976558, 7, 20],
                ["Gandalf from Lord of the Rings, diffuse lighting, fantasy, intricate elegant highly detailed "
                 "lifelike photorealistic digital painting, artstation", 248215544, 7, 42],
                ["Ethereal gardens of marble built in a shining teal river in future city, gorgeous ornate "
                 "multi-tiered fountain, futuristic, intricate elegant highly detailed lifelike photorealistic "
                 "realistic  painting, long shot, studio lighting, octane render, by Dorian Cleavenger", 3868142022,
                 7, 20],
                ["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", 70537634, 1, 2]
            ],
            inputs=[prompt, seed, guidance_scale, inference_steps]
        )

    gr.on(
        triggers=[generate_btn.click, prompt.submit],
        fn=generate,
        inputs=[prompt, negative_prompt, seed, is_random_seed, width, height, guidance_scale, inference_steps],
        outputs=[result]
    )

imageGenerator.queue().launch()