Spaces:
Paused
Paused
CryptoCreeper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,33 +25,25 @@ pipe.enable_vae_slicing()
|
|
| 25 |
pipe.set_progress_bar_config(disable=True)
|
| 26 |
|
| 27 |
# -------------------------------
|
| 28 |
-
#
|
| 29 |
# -------------------------------
|
| 30 |
-
def
|
| 31 |
"""
|
| 32 |
-
|
|
|
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
style = ""
|
| 39 |
-
if any(w in user_prompt.lower() for w in ["cute","adorable","kawaii"]):
|
| 40 |
-
style = ", cute, friendly, rounded body, big eyes, pastel colors, cartoon style"
|
| 41 |
-
elif any(w in user_prompt.lower() for w in ["realistic","photo","photograph"]):
|
| 42 |
-
style = ", ultra realistic, natural anatomy, professional photography"
|
| 43 |
-
else:
|
| 44 |
-
style = ", high quality, clean background"
|
| 45 |
-
|
| 46 |
-
prompt = f"a single {subject}, centered, isolated{style}"
|
| 47 |
-
negative = "multiple objects, duplicate, blurry, low quality, cropped, out of frame, horror, grotesque, aggressive, weird colors, artifacts"
|
| 48 |
return prompt, negative
|
| 49 |
|
| 50 |
# -------------------------------
|
| 51 |
-
#
|
| 52 |
# -------------------------------
|
| 53 |
def estimate_time(steps, resolution):
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
overhead = 2
|
| 56 |
est = overhead + steps * per_step
|
| 57 |
minutes = est // 60
|
|
@@ -59,42 +51,45 @@ def estimate_time(steps, resolution):
|
|
| 59 |
return f"⏱️ Estimated time: ~{int(minutes)}m {int(seconds)}s"
|
| 60 |
|
| 61 |
# -------------------------------
|
| 62 |
-
# IMAGE GENERATION WITH PROGRESSIVE BLUR
|
| 63 |
# -------------------------------
|
| 64 |
def generate(prompt, resolution, steps):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
blank_img = Image.new("RGB", (width, height), (255, 255, 255))
|
| 71 |
-
yield [blank_img], "" # always show gallery placeholder
|
| 72 |
|
| 73 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
seed = random.randint(0, 10**9)
|
| 75 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 76 |
-
pipe.scheduler.set_timesteps(
|
|
|
|
| 77 |
img = pipe(
|
| 78 |
prompt=refined_prompt,
|
| 79 |
negative_prompt=neg_prompt,
|
| 80 |
-
num_inference_steps=
|
| 81 |
guidance_scale=1.2,
|
| 82 |
-
width=
|
| 83 |
-
height=
|
| 84 |
generator=gen
|
| 85 |
).images[0]
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
max_blur = 20
|
| 89 |
steps_blur = 10
|
| 90 |
for i in range(steps_blur):
|
| 91 |
blur_percent = 100 - i*10
|
| 92 |
blurred_img = img.filter(ImageFilter.GaussianBlur(radius=max_blur * blur_percent / 100))
|
| 93 |
-
yield [blurred_img]
|
| 94 |
time.sleep(1) # 1 second per step
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
yield [img]
|
| 98 |
|
| 99 |
# -------------------------------
|
| 100 |
# GRADIO UI
|
|
@@ -105,14 +100,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column():
|
| 107 |
prompt_in = gr.Textbox(label="Prompt", placeholder="cute snake", lines=2)
|
| 108 |
-
|
| 109 |
resolution = gr.Radio([256, 512, 768, 1024], value=512, label="Resolution")
|
| 110 |
-
|
| 111 |
-
steps = gr.Slider(
|
| 112 |
-
|
| 113 |
eta = gr.Markdown("⏱️ Estimated time: ~1m 0s")
|
| 114 |
gen_btn = gr.Button("Generate")
|
| 115 |
-
|
| 116 |
with gr.Column():
|
| 117 |
gallery = gr.Gallery(columns=1)
|
| 118 |
|
|
@@ -124,7 +119,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 124 |
gen_btn.click(
|
| 125 |
generate,
|
| 126 |
inputs=[prompt_in, resolution, steps],
|
| 127 |
-
outputs=[gallery
|
| 128 |
)
|
| 129 |
|
| 130 |
demo.launch()
|
|
|
|
| 25 |
pipe.set_progress_bar_config(disable=True)
|
| 26 |
|
| 27 |
# -------------------------------
|
| 28 |
+
# PROMPT REFINEMENT
|
| 29 |
# -------------------------------
|
| 30 |
+
def refine_prompt(user_prompt: str):
|
| 31 |
"""
|
| 32 |
+
CPU-fast prompt enhancement.
|
| 33 |
+
Always focuses on a single centered object.
|
| 34 |
"""
|
| 35 |
+
subject = user_prompt.strip()
|
| 36 |
+
prompt = f"a single {subject}, centered, isolated, high quality, clean background"
|
| 37 |
+
negative = "multiple objects, duplicate, blurry, low quality, cropped, out of frame, horror, grotesque, weird colors, artifacts"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return prompt, negative
|
| 39 |
|
| 40 |
# -------------------------------
|
| 41 |
+
# ETA ESTIMATION
|
| 42 |
# -------------------------------
|
| 43 |
def estimate_time(steps, resolution):
|
| 44 |
+
steps = int(steps)
|
| 45 |
+
resolution = int(resolution)
|
| 46 |
+
per_step = {256:6, 512:12, 768:25, 1024:45}[resolution]
|
| 47 |
overhead = 2
|
| 48 |
est = overhead + steps * per_step
|
| 49 |
minutes = est // 60
|
|
|
|
| 51 |
return f"⏱️ Estimated time: ~{int(minutes)}m {int(seconds)}s"
|
| 52 |
|
| 53 |
# -------------------------------
|
| 54 |
+
# IMAGE GENERATION WITH PROGRESSIVE BLUR
|
| 55 |
# -------------------------------
|
| 56 |
def generate(prompt, resolution, steps):
|
| 57 |
+
steps = int(steps)
|
| 58 |
+
resolution = int(resolution)
|
| 59 |
|
| 60 |
+
# 1️⃣ Refine prompt
|
| 61 |
+
refined_prompt, neg_prompt = refine_prompt(prompt)
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# 2️⃣ Show blank gallery image while generating
|
| 64 |
+
blank_img = Image.new("RGB", (resolution, resolution), (255, 255, 255))
|
| 65 |
+
yield [blank_img]
|
| 66 |
+
|
| 67 |
+
# 3️⃣ CPU Image generation
|
| 68 |
seed = random.randint(0, 10**9)
|
| 69 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 70 |
+
pipe.scheduler.set_timesteps(steps)
|
| 71 |
+
|
| 72 |
img = pipe(
|
| 73 |
prompt=refined_prompt,
|
| 74 |
negative_prompt=neg_prompt,
|
| 75 |
+
num_inference_steps=steps,
|
| 76 |
guidance_scale=1.2,
|
| 77 |
+
width=resolution,
|
| 78 |
+
height=resolution,
|
| 79 |
generator=gen
|
| 80 |
).images[0]
|
| 81 |
|
| 82 |
+
# 4️⃣ Progressive blur reveal
|
| 83 |
+
max_blur = 20
|
| 84 |
steps_blur = 10
|
| 85 |
for i in range(steps_blur):
|
| 86 |
blur_percent = 100 - i*10
|
| 87 |
blurred_img = img.filter(ImageFilter.GaussianBlur(radius=max_blur * blur_percent / 100))
|
| 88 |
+
yield [blurred_img]
|
| 89 |
time.sleep(1) # 1 second per step
|
| 90 |
|
| 91 |
+
# 5️⃣ Fully revealed image
|
| 92 |
+
yield [img]
|
| 93 |
|
| 94 |
# -------------------------------
|
| 95 |
# GRADIO UI
|
|
|
|
| 100 |
with gr.Row():
|
| 101 |
with gr.Column():
|
| 102 |
prompt_in = gr.Textbox(label="Prompt", placeholder="cute snake", lines=2)
|
| 103 |
+
|
| 104 |
resolution = gr.Radio([256, 512, 768, 1024], value=512, label="Resolution")
|
| 105 |
+
|
| 106 |
+
steps = gr.Slider(4, 8, value=4, step=1, label="Steps") # minimum 4
|
| 107 |
+
|
| 108 |
eta = gr.Markdown("⏱️ Estimated time: ~1m 0s")
|
| 109 |
gen_btn = gr.Button("Generate")
|
| 110 |
+
|
| 111 |
with gr.Column():
|
| 112 |
gallery = gr.Gallery(columns=1)
|
| 113 |
|
|
|
|
| 119 |
gen_btn.click(
|
| 120 |
generate,
|
| 121 |
inputs=[prompt_in, resolution, steps],
|
| 122 |
+
outputs=[gallery]
|
| 123 |
)
|
| 124 |
|
| 125 |
demo.launch()
|