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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
from diffusers import DiffusionPipeline
|
| 5 |
+
from safetensors.torch import load_file
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
import torch
|
| 8 |
+
from typing import Tuple
|
| 9 |
+
|
| 10 |
+
style_list = [
|
| 11 |
+
{
|
| 12 |
+
"name": "(No style)",
|
| 13 |
+
"prompt": "{prompt}",
|
| 14 |
+
"negative_prompt": "",
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"name": "Cinematic",
|
| 18 |
+
"prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
| 19 |
+
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"name": "Photographic",
|
| 23 |
+
"prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
| 24 |
+
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"name": "Anime",
|
| 28 |
+
"prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
|
| 29 |
+
"negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"name": "Digital Art",
|
| 33 |
+
"prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
|
| 34 |
+
"negative_prompt": "photo, photorealistic, realism, ugly",
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"name": "Fantasy art",
|
| 38 |
+
"prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
|
| 39 |
+
"negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"name": "3D Model",
|
| 43 |
+
"prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
|
| 44 |
+
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
| 45 |
+
},
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
| 49 |
+
STYLE_NAMES = list(styles.keys())
|
| 50 |
+
DEFAULT_STYLE_NAME = "(No style)"
|
| 51 |
+
|
| 52 |
+
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
| 53 |
+
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 54 |
+
return p.replace("{prompt}", positive), n + negative
|
| 55 |
+
|
| 56 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
| 57 |
+
pipe.to("cuda")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 61 |
+
MAX_IMAGE_SIZE = 1024
|
| 62 |
+
|
| 63 |
+
def infer(prompt, negative_prompt, width, height, guidance_scale, style_name=None):
|
| 64 |
+
seed = random.randint(0,4294967295)
|
| 65 |
+
|
| 66 |
+
generator = torch.Generator().manual_seed(seed)
|
| 67 |
+
|
| 68 |
+
prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
|
| 69 |
+
|
| 70 |
+
image = [pipe(
|
| 71 |
+
prompt = prompt,
|
| 72 |
+
negative_prompt = negative_prompt,
|
| 73 |
+
guidance_scale = guidance_scale,
|
| 74 |
+
width = width,
|
| 75 |
+
height = height,
|
| 76 |
+
generator = generator
|
| 77 |
+
).images[0] for _ in range(4)]
|
| 78 |
+
|
| 79 |
+
return image
|
| 80 |
+
|
| 81 |
+
examples = [
|
| 82 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 83 |
+
"An astronaut riding a green horse",
|
| 84 |
+
"A delicious ceviche cheesecake slice",
|
| 85 |
+
"A serious capybara at work, wearing a suit",
|
| 86 |
+
'A Squirtle fine dining with a view to the London Eye',
|
| 87 |
+
'a graffiti of a robot serving meals to people',
|
| 88 |
+
'a beautiful cabin in Attersee, Austria, 3d animation style',
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
css="""
|
| 92 |
+
#col-container {
|
| 93 |
+
margin: 0 auto;
|
| 94 |
+
max-width: 1000px;
|
| 95 |
+
padding-top: 20px;
|
| 96 |
+
text-align: center;
|
| 97 |
+
}
|
| 98 |
+
.header {
|
| 99 |
+
margin: 10px auto 10px auto;
|
| 100 |
+
text-align: center;
|
| 101 |
+
max-width: 600px;
|
| 102 |
+
}
|
| 103 |
+
#example-container {
|
| 104 |
+
max-width: 1000px;
|
| 105 |
+
margin: 0 auto;
|
| 106 |
+
}
|
| 107 |
+
.footer {
|
| 108 |
+
margin: 25px auto 45px auto;
|
| 109 |
+
text-align: center;
|
| 110 |
+
max-width: 600px;
|
| 111 |
+
}
|
| 112 |
+
.footer>p {
|
| 113 |
+
font-size: .8rem;
|
| 114 |
+
display: inline-block;
|
| 115 |
+
padding: 0 10px;
|
| 116 |
+
transform: translateY(10px);
|
| 117 |
+
}
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
if torch.cuda.is_available():
|
| 121 |
+
power_device = "GPU"
|
| 122 |
+
else:
|
| 123 |
+
power_device = "CPU"
|
| 124 |
+
|
| 125 |
+
with gr.Blocks(css=css) as demo:
|
| 126 |
+
|
| 127 |
+
with gr.Column(elem_id="col-container"):
|
| 128 |
+
gr.HTML(
|
| 129 |
+
"""
|
| 130 |
+
<div class="header">
|
| 131 |
+
<h1>Welcome to Metamorph: Your Creative Gateway</h1>
|
| 132 |
+
<h4>
|
| 133 |
+
Transform your words into stunning visuals with our advanced AI-powered Text-to-Image generator
|
| 134 |
+
</h4>
|
| 135 |
+
</div>
|
| 136 |
+
""")
|
| 137 |
+
gr.Markdown(f"""
|
| 138 |
+
Currently running on {power_device}.
|
| 139 |
+
""")
|
| 140 |
+
with gr.Row(elem_id="col-container"):
|
| 141 |
+
# Left column
|
| 142 |
+
with gr.Column(scale=1,elem_id="left-container"):
|
| 143 |
+
with gr.Row():
|
| 144 |
+
prompt = gr.Text(
|
| 145 |
+
label="Prompt",
|
| 146 |
+
show_label=False,
|
| 147 |
+
max_lines=1,
|
| 148 |
+
placeholder="Enter your prompt",
|
| 149 |
+
container=False,
|
| 150 |
+
)
|
| 151 |
+
run_button = gr.Button("Generate", scale=0)
|
| 152 |
+
|
| 153 |
+
with gr.Accordion("Advanced Settings", open=True):
|
| 154 |
+
|
| 155 |
+
negative_prompt = gr.Textbox(
|
| 156 |
+
label="Negative prompt",
|
| 157 |
+
show_label=False,
|
| 158 |
+
max_lines=1,
|
| 159 |
+
placeholder="Enter a negative prompt",
|
| 160 |
+
elem_id="negative-prompt-text-input",
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
style_selection = gr.Radio(
|
| 164 |
+
show_label=True,
|
| 165 |
+
container=True,
|
| 166 |
+
interactive=True,
|
| 167 |
+
choices=STYLE_NAMES,
|
| 168 |
+
value=DEFAULT_STYLE_NAME,
|
| 169 |
+
label="Image Style",
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
with gr.Row():
|
| 173 |
+
width = gr.Slider(
|
| 174 |
+
label="Width",
|
| 175 |
+
minimum=256,
|
| 176 |
+
maximum=MAX_IMAGE_SIZE,
|
| 177 |
+
step=32,
|
| 178 |
+
value=1024,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
height = gr.Slider(
|
| 182 |
+
label="Height",
|
| 183 |
+
minimum=256,
|
| 184 |
+
maximum=MAX_IMAGE_SIZE,
|
| 185 |
+
step=32,
|
| 186 |
+
value=1024,
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
with gr.Row():
|
| 190 |
+
guidance_scale = gr.Slider(
|
| 191 |
+
label="Guidance scale",
|
| 192 |
+
minimum=0.0,
|
| 193 |
+
maximum=50.0,
|
| 194 |
+
step=0.1,
|
| 195 |
+
value=10,
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
# Right column
|
| 199 |
+
with gr.Column(scale=1, elem_id="right-container"):
|
| 200 |
+
result = gr.Gallery(label="Results", show_label=False, format="png", show_share_button=False, height=475)
|
| 201 |
+
|
| 202 |
+
gr.Examples(
|
| 203 |
+
elem_id="example-container",
|
| 204 |
+
examples = examples,
|
| 205 |
+
inputs = [prompt]
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
gr.HTML(
|
| 209 |
+
"""
|
| 210 |
+
<div class="footer">
|
| 211 |
+
<p>
|
| 212 |
+
This application harnesses the cutting-edge Stable Diffusion XL (SDXL) model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">StabilityAI</a>, offering unparalleled text-to-image generation, while acknowledging potential biases and content considerations outlined in the model card.</p>
|
| 213 |
+
</p>
|
| 214 |
+
</div>
|
| 215 |
+
"""
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
run_button.click(
|
| 219 |
+
fn = infer,
|
| 220 |
+
inputs = [prompt, negative_prompt, width, height, guidance_scale, style_selection],
|
| 221 |
+
outputs = [result]
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
demo.queue().launch()
|