Spaces:
Paused
Paused
CryptoCreeper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import random
|
| 4 |
import time
|
|
|
|
| 5 |
import re
|
| 6 |
-
import numpy as np
|
| 7 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 8 |
-
from PIL import Image
|
| 9 |
|
| 10 |
# -------------------------------------------------
|
| 11 |
-
# MODEL SETUP (CPU
|
| 12 |
# -------------------------------------------------
|
| 13 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 14 |
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
|
|
@@ -24,140 +22,129 @@ pipe.to("cpu")
|
|
| 24 |
|
| 25 |
pipe.enable_attention_slicing()
|
| 26 |
pipe.enable_vae_slicing()
|
|
|
|
| 27 |
|
| 28 |
# -------------------------------------------------
|
| 29 |
-
#
|
| 30 |
# -------------------------------------------------
|
| 31 |
-
def
|
| 32 |
-
p =
|
| 33 |
-
|
| 34 |
-
# Detect single-object intent
|
| 35 |
-
single_object = not re.search(r"\b(two|three|multiple|many|group|pile)\b", p)
|
| 36 |
|
| 37 |
-
# Detect
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
])
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
-
neg = (
|
| 48 |
-
"multiple objects, duplicates, crowd, background objects, clutter"
|
| 49 |
-
)
|
| 50 |
-
else:
|
| 51 |
-
optimized = f"{prompt}, high quality"
|
| 52 |
-
neg = "blurry, low quality"
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
# -------------------------------------------------
|
| 59 |
-
def smart_crop(img: Image.Image, padding=5):
|
| 60 |
-
gray = np.array(img.convert("L"))
|
| 61 |
-
mask = gray > 20 # detect non-black-ish pixels
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
y0, x0 = coords.min(axis=0)
|
| 68 |
-
y1, x1 = coords.max(axis=0)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
y1 = min(h, y1 + padding)
|
| 75 |
|
| 76 |
-
return
|
| 77 |
|
| 78 |
# -------------------------------------------------
|
| 79 |
-
# GENERATION
|
| 80 |
# -------------------------------------------------
|
| 81 |
-
def generate(
|
| 82 |
-
prompt,
|
| 83 |
-
size,
|
| 84 |
-
steps,
|
| 85 |
-
random_seed,
|
| 86 |
-
seed_val,
|
| 87 |
-
auto_res
|
| 88 |
-
):
|
| 89 |
start = time.time()
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# Resolution logic
|
| 94 |
-
if auto_res and not is_scene:
|
| 95 |
-
width = height = 448
|
| 96 |
-
else:
|
| 97 |
-
width = height = int(size)
|
| 98 |
|
| 99 |
-
seed = random.randint(0,
|
| 100 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
prompt=
|
| 104 |
negative_prompt=neg_prompt,
|
| 105 |
num_inference_steps=int(steps),
|
| 106 |
-
guidance_scale=1.2,
|
| 107 |
-
width=
|
| 108 |
-
height=
|
| 109 |
generator=gen
|
| 110 |
).images[0]
|
| 111 |
|
| 112 |
-
# Auto-crop ONLY for single objects
|
| 113 |
-
if auto_res and not is_scene:
|
| 114 |
-
image = smart_crop(image, padding=5)
|
| 115 |
-
|
| 116 |
duration = round(time.time() - start, 2)
|
| 117 |
-
status = f"✅
|
| 118 |
|
| 119 |
-
return [
|
| 120 |
|
| 121 |
# -------------------------------------------------
|
| 122 |
-
# ETA (
|
| 123 |
# -------------------------------------------------
|
| 124 |
-
def estimate_time(steps,
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
step_cost = 0.
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
# -------------------------------------------------
|
| 132 |
# UI
|
| 133 |
# -------------------------------------------------
|
| 134 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 135 |
-
gr.Markdown("# 👾 CREEPER AI —
|
| 136 |
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column():
|
| 139 |
-
prompt_in = gr.Textbox(
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
btn = gr.Button("Generate")
|
| 149 |
|
| 150 |
with gr.Column():
|
| 151 |
status = gr.Markdown("🟢 Ready")
|
| 152 |
gallery = gr.Gallery(columns=1)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
ctrl.change(estimate_time, [steps, size, auto_res], eta)
|
| 157 |
|
| 158 |
btn.click(
|
| 159 |
generate,
|
| 160 |
-
inputs=[prompt_in,
|
| 161 |
outputs=[gallery, status]
|
| 162 |
)
|
| 163 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
import time
|
| 4 |
+
import random
|
| 5 |
import re
|
|
|
|
| 6 |
from diffusers import DiffusionPipeline, LCMScheduler
|
|
|
|
| 7 |
|
| 8 |
# -------------------------------------------------
|
| 9 |
+
# MODEL SETUP (CPU SAFE)
|
| 10 |
# -------------------------------------------------
|
| 11 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 12 |
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
|
|
|
|
| 22 |
|
| 23 |
pipe.enable_attention_slicing()
|
| 24 |
pipe.enable_vae_slicing()
|
| 25 |
+
pipe.set_progress_bar_config(disable=True)
|
| 26 |
|
| 27 |
# -------------------------------------------------
|
| 28 |
+
# EXCELLENT UNDERSTANDING ENGINE (FAST, NO ML)
|
| 29 |
# -------------------------------------------------
|
| 30 |
+
def refine_prompt(user_prompt: str):
|
| 31 |
+
p = user_prompt.lower()
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Detect style intent
|
| 34 |
+
is_cute = any(k in p for k in ["cute", "adorable", "kawaii"])
|
| 35 |
+
is_realistic = any(k in p for k in ["realistic", "photo", "photograph"])
|
| 36 |
+
is_cartoon = any(k in p for k in ["cartoon", "anime", "illustration"])
|
| 37 |
|
| 38 |
+
# Detect animal
|
| 39 |
+
animal_match = re.search(
|
| 40 |
+
r"(snake|cat|dog|dragon|bird|fox|rabbit|lion|tiger)", p
|
| 41 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
subject = animal_match.group(1) if animal_match else user_prompt
|
| 44 |
|
| 45 |
+
# Base object enforcement
|
| 46 |
+
prompt = f"a single {subject}, centered, isolated"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Style refinement
|
| 49 |
+
if is_cute:
|
| 50 |
+
prompt += (
|
| 51 |
+
", cute, friendly, rounded shapes, big expressive eyes, "
|
| 52 |
+
"soft lighting, smooth colors"
|
| 53 |
+
)
|
| 54 |
+
elif is_cartoon:
|
| 55 |
+
prompt += ", cartoon style, clean lines, vibrant colors"
|
| 56 |
+
elif is_realistic:
|
| 57 |
+
prompt += ", ultra realistic, sharp focus, professional photography"
|
| 58 |
+
else:
|
| 59 |
+
prompt += ", high quality, detailed"
|
| 60 |
|
| 61 |
+
prompt += ", simple background"
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
negative = (
|
| 64 |
+
"multiple subjects, duplicate, horror, scary, grotesque, "
|
| 65 |
+
"deformed, blurry, low quality, background clutter"
|
| 66 |
+
)
|
|
|
|
| 67 |
|
| 68 |
+
return prompt, negative
|
| 69 |
|
| 70 |
# -------------------------------------------------
|
| 71 |
+
# GENERATION
|
| 72 |
# -------------------------------------------------
|
| 73 |
+
def generate(prompt, resolution, steps):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
start = time.time()
|
| 75 |
|
| 76 |
+
refined_prompt, neg_prompt = refine_prompt(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
seed = random.randint(0, 10**9)
|
| 79 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 80 |
|
| 81 |
+
img = pipe(
|
| 82 |
+
prompt=refined_prompt,
|
| 83 |
negative_prompt=neg_prompt,
|
| 84 |
num_inference_steps=int(steps),
|
| 85 |
+
guidance_scale=1.2,
|
| 86 |
+
width=int(resolution),
|
| 87 |
+
height=int(resolution),
|
| 88 |
generator=gen
|
| 89 |
).images[0]
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
duration = round(time.time() - start, 2)
|
| 92 |
+
status = f"✅ Generated in {duration}s | Seed: {seed}"
|
| 93 |
|
| 94 |
+
return [img], status
|
| 95 |
|
| 96 |
# -------------------------------------------------
|
| 97 |
+
# FAST & REALISTIC ETA ( < 10 ms )
|
| 98 |
# -------------------------------------------------
|
| 99 |
+
def estimate_time(steps, resolution):
|
| 100 |
+
base_overhead = 1.2
|
| 101 |
+
res_factor = (int(resolution) / 512) ** 2
|
| 102 |
+
step_cost = 0.35
|
| 103 |
+
|
| 104 |
+
est = base_overhead + (steps * step_cost * res_factor)
|
| 105 |
+
return f"⚡ Estimated time: ~{round(est, 1)}s"
|
| 106 |
|
| 107 |
# -------------------------------------------------
|
| 108 |
# UI
|
| 109 |
# -------------------------------------------------
|
| 110 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 111 |
+
gr.Markdown("# 👾 CREEPER AI — IMAGE (SMART CORE)")
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
with gr.Column():
|
| 115 |
+
prompt_in = gr.Textbox(
|
| 116 |
+
label="Prompt",
|
| 117 |
+
placeholder="cute snake",
|
| 118 |
+
lines=2
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
resolution = gr.Radio(
|
| 122 |
+
[256, 512, 768, 1024],
|
| 123 |
+
value=512,
|
| 124 |
+
label="Resolution"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
steps = gr.Slider(
|
| 128 |
+
minimum=2,
|
| 129 |
+
maximum=10,
|
| 130 |
+
value=4,
|
| 131 |
+
step=1,
|
| 132 |
+
label="Steps"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
eta = gr.Markdown("⚡ Estimated time: ~2.5s")
|
| 136 |
btn = gr.Button("Generate")
|
| 137 |
|
| 138 |
with gr.Column():
|
| 139 |
status = gr.Markdown("🟢 Ready")
|
| 140 |
gallery = gr.Gallery(columns=1)
|
| 141 |
|
| 142 |
+
for ctrl in [steps, resolution]:
|
| 143 |
+
ctrl.change(estimate_time, [steps, resolution], eta)
|
|
|
|
| 144 |
|
| 145 |
btn.click(
|
| 146 |
generate,
|
| 147 |
+
inputs=[prompt_in, resolution, steps],
|
| 148 |
outputs=[gallery, status]
|
| 149 |
)
|
| 150 |
|