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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
import uuid
|
5 |
+
import gradio as gr
|
6 |
+
import spaces
|
7 |
+
import numpy as np
|
8 |
+
from diffusers import PixArtAlphaPipeline, LCMScheduler
|
9 |
+
import torch
|
10 |
+
from typing import Tuple
|
11 |
+
from datetime import datetime
|
12 |
+
|
13 |
+
# Description for the app
|
14 |
+
DESCRIPTION = """
|
15 |
+
# Instant Image
|
16 |
+
### Super fast text to Image Generator.
|
17 |
+
### <span style='color: red;'>You may change the steps from 4 to 8, if you didn't get satisfied results.
|
18 |
+
### First Image processing takes time then images generate faster.
|
19 |
+
### Must Try -> Instant Video https://huggingface.co/spaces/KingNish/Instant-Video
|
20 |
+
"""
|
21 |
+
if not torch.cuda.is_available():
|
22 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
|
23 |
+
|
24 |
+
# Configuration and constants
|
25 |
+
MAX_SEED = np.iinfo(np.int32).max
|
26 |
+
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
|
27 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4192"))
|
28 |
+
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
29 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
30 |
+
PORT = int(os.getenv("DEMO_PORT", "15432"))
|
31 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
32 |
+
|
33 |
+
# Define color-based attributes
|
34 |
+
color_attributes = {
|
35 |
+
"Purple": {"verbs": ["assist", "befriend", "care", "collaborate", "connect", "embrace", "empower", "encourage", "foster", "give", "help", "nourish", "nurture", "promote", "protect", "provide", "serve", "share", "shepherd", "steward", "tend", "uplift", "value", "welcome"], "adjectives": ["caring", "encouraging", "attentive", "compassionate", "empathetic", "generous", "hospitable", "nurturing", "protective", "selfless", "supportive", "welcoming"]},
|
36 |
+
"Green": {"verbs": ["analyze", "discover", "examine", "expand", "explore", "extend", "inquire", "journey", "launch", "move", "pioneer", "pursue", "question", "reach", "search", "uncover", "venture", "wonder"], "adjectives": ["adventurous", "curious", "discerning", "examining", "experiential", "exploratory", "inquisitive", "investigative", "intrepid", "philosophical"]},
|
37 |
+
"Maroon": {"verbs": ["accomplish", "achieve", "build", "challenge", "commit", "compete", "contend", "dedicate", "defend", "devote", "drive", "endeavor", "entrust", "endure", "fight", "grapple", "grow", "improve", "increase", "overcome", "persevere", "persist", "press on", "pursue", "resolve"], "adjectives": ["competitive", "determined", "gritty", "industrious", "persevering", "relentless", "resilient", "tenacious", "tough", "unwavering"]},
|
38 |
+
"Orange": {"verbs": ["compose", "conceptualize", "conceive", "craft", "create", "design", "dream", "envision", "express", "fashion", "form", "imagine", "interpret", "make", "originate", "paint", "perform", "portray", "realize", "shape"], "adjectives": ["artistic", "conceptual", "creative", "eclectic", "expressive", "imaginative", "interpretive", "novel", "original", "whimsical"]},
|
39 |
+
"Yellow": {"verbs": ["accelerate", "advance", "change", "conceive", "create", "engineer", "envision", "experiment", "dream", "ignite", "illuminate", "imagine", "innovate", "inspire", "invent", "pioneer", "progress", "shape", "spark", "solve", "transform", "unleash", "unlock"], "adjectives": ["advanced", "analytical", "brilliant", "experimental", "forward-thinking", "innovative", "intelligent", "inventive", "leading-edge", "visionary"]},
|
40 |
+
"Red": {"verbs": ["animate", "amuse", "captivate", "cheer", "delight", "encourage", "energize", "engage", "enjoy", "enliven", "entertain", "excite", "express", "inspire", "joke", "motivate", "play", "stir", "uplift"], "adjectives": ["dynamic", "energetic", "engaging", "entertaining", "enthusiastic", "exciting", "fun", "lively", "magnetic", "playful", "humorous"]},
|
41 |
+
"Blue": {"verbs": ["accomplish", "achieve", "affect", "assert", "cause", "command", "determine", "direct", "dominate", "drive", "empower", "establish", "guide", "impact", "impress", "influence", "inspire", "lead", "outpace", "outshine", "realize", "shape", "succeed", "transform", "win"], "adjectives": ["accomplished", "assertive", "confident", "decisive", "elite", "influential", "powerful", "prominent", "proven", "strong"]},
|
42 |
+
"Pink": {"verbs": ["arise", "aspire", "detail", "dream", "elevate", "enchant", "enrich", "envision", "exceed", "excel", "experience", "improve", "idealize", "imagine", "inspire", "perfect", "poise", "polish", "prepare", "refine", "uplift"], "adjectives": ["aesthetic", "charming", "classic", "dignified", "idealistic", "meticulous", "poised", "polished", "refined", "sophisticated", "elegant"]},
|
43 |
+
"Silver": {"verbs": ["activate", "campaign", "challenge", "commit", "confront", "dare", "defy", "disrupt", "drive", "excite", "face", "ignite", "incite", "influence", "inspire", "inspirit", "motivate", "move", "push", "rebel", "reimagine", "revolutionize", "rise", "spark", "stir", "fight", "free"], "adjectives": ["bold", "daring", "fearless", "independent", "non-conformist", "radical", "rebellious", "resolute", "unconventional", "valiant"]},
|
44 |
+
"Beige": {"verbs": ["dedicate", "humble", "collaborate", "empower", "inspire", "empassion", "transform"], "adjectives": ["dedicated", "collaborative", "consistent", "empowering", "enterprising", "humble", "inspiring", "passionate", "proud", "traditional", "transformative"]},
|
45 |
+
}
|
46 |
+
|
47 |
+
# Image styles for Gradio interface
|
48 |
+
style_list = [
|
49 |
+
{"name": "(No style)", "prompt": "{prompt}", "negative_prompt": ""},
|
50 |
+
{"name": "Cinematic", "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy", "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured"},
|
51 |
+
{"name": "Realistic", "prompt": "Photorealistic {prompt} . Ulta-realistic, professional, 4k, highly detailed", "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly, disfigured"},
|
52 |
+
{"name": "Anime", "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed", "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast"},
|
53 |
+
{"name": "Digital Art", "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed", "negative_prompt": "photo, photorealistic, realism, ugly"},
|
54 |
+
{"name": "Pixel art", "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics", "negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic"},
|
55 |
+
{"name": "Fantasy art", "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy", "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"},
|
56 |
+
{"name": "3D Model", "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting", "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting"},
|
57 |
+
]
|
58 |
+
|
59 |
+
# Create dictionary of styles
|
60 |
+
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
61 |
+
STYLE_NAMES = list(styles.keys())
|
62 |
+
DEFAULT_STYLE_NAME = "(No style)"
|
63 |
+
NUM_IMAGES_PER_PROMPT = 1
|
64 |
+
|
65 |
+
# Function to apply style and modify prompt based on selected colors
|
66 |
+
def apply_style(style_name: str, positive: str, color_selections: dict) -> Tuple[str, str]:
|
67 |
+
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
68 |
+
color_prompt = ""
|
69 |
+
|
70 |
+
# Aggregate verbs and adjectives from selected colors based on their ratios
|
71 |
+
for color, attributes in color_selections.items():
|
72 |
+
if attributes["selected"]:
|
73 |
+
verbs = random.sample(color_attributes[color]["verbs"], min(3, len(color_attributes[color]["verbs"])))
|
74 |
+
adjectives = random.sample(color_attributes[color]["adjectives"], min(3, len(color_attributes[color]["adjectives"])))
|
75 |
+
color_prompt += " ".join(verbs) + " " + " ".join(adjectives) + " "
|
76 |
+
|
77 |
+
# Form the final prompt
|
78 |
+
final_prompt = p.replace("{prompt}", positive + " " + color_prompt.strip())
|
79 |
+
return final_prompt, n
|
80 |
+
|
81 |
+
# Check if CUDA is available and set up the pipeline
|
82 |
+
if torch.cuda.is_available():
|
83 |
+
pipe = PixArtAlphaPipeline.from_pretrained(
|
84 |
+
"PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
|
85 |
+
torch_dtype=torch.float16,
|
86 |
+
use_safetensors=True,
|
87 |
+
)
|
88 |
+
if os.getenv('CONSISTENCY_DECODER', False):
|
89 |
+
print("Using DALL-E 3 Consistency Decoder")
|
90 |
+
pipe.vae = ConsistencyDecoderVAE.from_pretrained("openai/consistency-decoder", torch_dtype=torch.float16)
|
91 |
+
if ENABLE_CPU_OFFLOAD:
|
92 |
+
pipe.enable_model_cpu_offload()
|
93 |
+
else:
|
94 |
+
pipe.to(device)
|
95 |
+
print("Loaded on Device!")
|
96 |
+
if USE_TORCH_COMPILE:
|
97 |
+
pipe.transformer = torch.compile(pipe.transformer, mode="reduce-overhead", fullgraph=True)
|
98 |
+
print("Model Compiled!")
|
99 |
+
|
100 |
+
# Function to save image
|
101 |
+
def save_image(img):
|
102 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
103 |
+
img.save(unique_name)
|
104 |
+
return unique_name
|
105 |
+
|
106 |
+
# Function to randomize seed if needed
|
107 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
108 |
+
if randomize_seed:
|
109 |
+
seed = random.randint(0, MAX_SEED)
|
110 |
+
return seed
|
111 |
+
|
112 |
+
# Main function to generate images based on user inputs
|
113 |
+
@spaces.GPU(duration=30)
|
114 |
+
def generate(
|
115 |
+
prompt: str,
|
116 |
+
negative_prompt: str = "",
|
117 |
+
style: str = DEFAULT_STYLE_NAME,
|
118 |
+
use_negative_prompt: bool = False,
|
119 |
+
seed: int = 0,
|
120 |
+
width: int = 1024,
|
121 |
+
height: int = 1024,
|
122 |
+
inference_steps: int = 4,
|
123 |
+
randomize_seed: bool = False,
|
124 |
+
use_resolution_binning: bool = True,
|
125 |
+
**color_ratios # Collect color ratios dynamically
|
126 |
+
):
|
127 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
128 |
+
generator = torch.Generator().manual_seed(seed)
|
129 |
+
|
130 |
+
if not use_negative_prompt:
|
131 |
+
negative_prompt = None # type: ignore
|
132 |
+
|
133 |
+
# Process color selections and their ratios
|
134 |
+
color_selections = {color: {"selected": color_ratios.get(f"{color.lower()}_selected", False), "ratio": color_ratios.get(f"{color.lower()}_ratio", 0)} for color in color_attributes}
|
135 |
+
|
136 |
+
# Apply style and modify prompt based on color selections
|
137 |
+
prompt, negative_prompt = apply_style(style, prompt, color_selections)
|
138 |
+
|
139 |
+
# Generate images
|
140 |
+
try:
|
141 |
+
images = pipe(
|
142 |
+
prompt=prompt,
|
143 |
+
negative_prompt=negative_prompt,
|
144 |
+
width=width,
|
145 |
+
height=height,
|
146 |
+
guidance_scale=0,
|
147 |
+
num_inference_steps=inference_steps,
|
148 |
+
generator=generator,
|
149 |
+
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
150 |
+
use_resolution_binning=use_resolution_binning,
|
151 |
+
output_type="pil",
|
152 |
+
).images
|
153 |
+
except Exception as e:
|
154 |
+
print(f"Error during image generation: {e}")
|
155 |
+
return [], seed
|
156 |
+
|
157 |
+
image_paths = [save_image(img) for img in images]
|
158 |
+
print(image_paths)
|
159 |
+
return image_paths, seed
|
160 |
+
|
161 |
+
# Example prompts
|
162 |
+
examples = [
|
163 |
+
"A Monkey with a happy face in the Sahara desert.",
|
164 |
+
"Eiffel Tower was Made up of ICE.",
|
165 |
+
"Color photo of a corgi made of transparent glass, standing on the riverside in Yosemite National Park.",
|
166 |
+
"A close-up photo of a woman. She wore a blue coat with a gray dress underneath and has blue eyes.",
|
167 |
+
"A litter of golden retriever puppies playing in the snow. Their heads pop out of the snow, covered in.",
|
168 |
+
"an astronaut sitting in a diner, eating fries, cinematic, analog film",
|
169 |
+
]
|
170 |
+
|
171 |
+
# Set up the Gradio interface
|
172 |
+
with gr.Blocks() as demo:
|
173 |
+
gr.Markdown(DESCRIPTION)
|
174 |
+
with gr.Row(equal_height=False):
|
175 |
+
with gr.Group():
|
176 |
+
with gr.Row():
|
177 |
+
prompt = gr.Text(
|
178 |
+
label="Prompt",
|
179 |
+
show_label=False,
|
180 |
+
max_lines=1,
|
181 |
+
placeholder="Enter your prompt",
|
182 |
+
container=False,
|
183 |
+
)
|
184 |
+
run_button = gr.Button("Run", scale=0)
|
185 |
+
result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT, show_label=False)
|
186 |
+
|
187 |
+
# Color selection and ratio configuration in the UI
|
188 |
+
with gr.Accordion("Color Influences", open=False):
|
189 |
+
with gr.Group():
|
190 |
+
color_checkboxes = {}
|
191 |
+
color_sliders = {}
|
192 |
+
for color in color_attributes:
|
193 |
+
with gr.Row():
|
194 |
+
color_checkboxes[color] = gr.Checkbox(label=f"{color} Selected", value=False)
|
195 |
+
color_sliders[color] = gr.Slider(label=f"{color} Influence Ratio", minimum=0, maximum=1, step=0.01, value=0.0)
|
196 |
+
|
197 |
+
with gr.Accordion("Advanced options", open=False):
|
198 |
+
with gr.Group():
|
199 |
+
with gr.Row():
|
200 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False, visible=True)
|
201 |
+
negative_prompt = gr.Text(
|
202 |
+
label="Negative prompt",
|
203 |
+
max_lines=1,
|
204 |
+
placeholder="Enter a negative prompt",
|
205 |
+
visible=True,
|
206 |
+
)
|
207 |
+
style_selection = gr.Radio(
|
208 |
+
choices=STYLE_NAMES,
|
209 |
+
value=DEFAULT_STYLE_NAME,
|
210 |
+
label="Image Style",
|
211 |
+
show_label=True,
|
212 |
+
container=True,
|
213 |
+
interactive=True,
|
214 |
+
)
|
215 |
+
seed = gr.Slider(
|
216 |
+
label="Seed",
|
217 |
+
minimum=0,
|
218 |
+
maximum=MAX_SEED,
|
219 |
+
step=1,
|
220 |
+
value=0,
|
221 |
+
)
|
222 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
223 |
+
with gr.Row(visible=True):
|
224 |
+
width = gr.Slider(
|
225 |
+
label="Width",
|
226 |
+
minimum=256,
|
227 |
+
maximum=MAX_IMAGE_SIZE,
|
228 |
+
step=32,
|
229 |
+
value=1024,
|
230 |
+
)
|
231 |
+
height = gr.Slider(
|
232 |
+
label="Height",
|
233 |
+
minimum=256,
|
234 |
+
maximum=MAX_IMAGE_SIZE,
|
235 |
+
step=32,
|
236 |
+
value=1024,
|
237 |
+
)
|
238 |
+
with gr.Row():
|
239 |
+
inference_steps = gr.Slider(
|
240 |
+
label="Steps",
|
241 |
+
minimum=4,
|
242 |
+
maximum=20,
|
243 |
+
step=1,
|
244 |
+
value=4,
|
245 |
+
)
|
246 |
+
|
247 |
+
gr.Examples(
|
248 |
+
examples=examples,
|
249 |
+
inputs=prompt,
|
250 |
+
outputs=[result, seed],
|
251 |
+
fn=generate,
|
252 |
+
cache_examples=CACHE_EXAMPLES,
|
253 |
+
)
|
254 |
+
|
255 |
+
# Dynamic updates based on user interactions
|
256 |
+
use_negative_prompt.change(
|
257 |
+
fn=lambda x: gr.update(visible=x),
|
258 |
+
inputs=use_negative_prompt,
|
259 |
+
outputs=negative_prompt,
|
260 |
+
api_name=False,
|
261 |
+
)
|
262 |
+
|
263 |
+
gr.on(
|
264 |
+
triggers=[
|
265 |
+
prompt.submit,
|
266 |
+
negative_prompt.submit,
|
267 |
+
run_button.click,
|
268 |
+
],
|
269 |
+
fn=generate,
|
270 |
+
inputs=[
|
271 |
+
prompt,
|
272 |
+
negative_prompt,
|
273 |
+
style_selection,
|
274 |
+
use_negative_prompt,
|
275 |
+
seed,
|
276 |
+
width,
|
277 |
+
height,
|
278 |
+
inference_steps,
|
279 |
+
randomize_seed,
|
280 |
+
*[color_checkboxes[color] for color in color_attributes],
|
281 |
+
*[color_sliders[color] for color in color_attributes]
|
282 |
+
],
|
283 |
+
outputs=[result, seed],
|
284 |
+
api_name="run",
|
285 |
+
)
|
286 |
+
|
287 |
+
# Launch the Gradio app
|
288 |
+
if __name__ == "__main__":
|
289 |
+
demo.queue(max_size=20).launch()
|
290 |
+
# Uncomment the next line to launch the server with specific options
|
291 |
+
# demo.queue(max_size=20).launch(server_name="0.0.0.0", server_port=11900, debug=True)
|