Spaces:
Paused
Paused
CryptoCreeper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,104 @@
|
|
| 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
|
| 10 |
-
# -------------------------------
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
pipe = DiffusionPipeline.from_pretrained(
|
| 15 |
-
|
| 16 |
torch_dtype=torch.float32,
|
| 17 |
safety_checker=None
|
| 18 |
)
|
| 19 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 20 |
-
pipe.load_lora_weights(
|
| 21 |
pipe.to("cpu")
|
| 22 |
|
| 23 |
pipe.enable_attention_slicing()
|
| 24 |
pipe.enable_vae_slicing()
|
| 25 |
pipe.set_progress_bar_config(disable=True)
|
| 26 |
|
| 27 |
-
# -------------------------------
|
| 28 |
-
#
|
| 29 |
-
# -------------------------------
|
| 30 |
def refine_prompt(user_prompt: str):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
if is_cute:
|
| 48 |
-
prompt +=
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
"smooth cartoon style, pastel colors"
|
| 52 |
-
)
|
| 53 |
elif is_realistic:
|
| 54 |
-
prompt +=
|
| 55 |
-
", ultra realistic, natural anatomy, "
|
| 56 |
-
"professional wildlife photography"
|
| 57 |
-
)
|
| 58 |
else:
|
| 59 |
-
prompt += ",
|
| 60 |
-
|
| 61 |
-
prompt += ", simple plain background"
|
| 62 |
|
|
|
|
| 63 |
negative = (
|
| 64 |
-
"multiple
|
| 65 |
-
"
|
| 66 |
-
"blurry, low quality, cropped, out of frame"
|
| 67 |
)
|
| 68 |
|
| 69 |
return prompt, negative
|
| 70 |
|
| 71 |
-
# -------------------------------
|
| 72 |
-
#
|
| 73 |
-
# -------------------------------
|
| 74 |
def estimate_time(steps, resolution):
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def generate(prompt, resolution, steps):
|
| 88 |
-
|
| 89 |
yield None, "🧠 Understanding your prompt..."
|
| 90 |
-
|
| 91 |
refined_prompt, neg_prompt = refine_prompt(prompt)
|
| 92 |
-
|
| 93 |
-
yield None, "🎨 Generating image (CPU,
|
| 94 |
-
|
| 95 |
seed = random.randint(0, 10**9)
|
| 96 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 97 |
|
|
|
|
|
|
|
| 98 |
img = pipe(
|
| 99 |
prompt=refined_prompt,
|
| 100 |
negative_prompt=neg_prompt,
|
|
@@ -104,45 +108,36 @@ def generate(prompt, resolution, steps):
|
|
| 104 |
height=int(resolution),
|
| 105 |
generator=gen
|
| 106 |
).images[0]
|
| 107 |
-
|
| 108 |
-
duration = int(time.time() -
|
| 109 |
yield [img], f"✅ Finished in {duration}s | Seed: {seed}"
|
| 110 |
|
| 111 |
-
# -------------------------------
|
| 112 |
-
# UI
|
| 113 |
-
# -------------------------------
|
| 114 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 115 |
-
gr.Markdown("# 👾 CREEPER AI — CPU
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
with gr.Column():
|
| 119 |
-
prompt_in = gr.Textbox(
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
label="Resolution"
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
steps = gr.Slider(
|
| 132 |
-
2, 10, value=4, step=1, label="Steps"
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
eta = gr.Markdown("⏱️ Estimated time: ~90 seconds")
|
| 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 |
-
|
| 146 |
generate,
|
| 147 |
inputs=[prompt_in, resolution, steps],
|
| 148 |
outputs=[gallery, status]
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
import random
|
| 4 |
+
import time
|
| 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"
|
| 13 |
|
| 14 |
pipe = DiffusionPipeline.from_pretrained(
|
| 15 |
+
MODEL_ID,
|
| 16 |
torch_dtype=torch.float32,
|
| 17 |
safety_checker=None
|
| 18 |
)
|
| 19 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 20 |
+
pipe.load_lora_weights(ADAPTER_ID)
|
| 21 |
pipe.to("cpu")
|
| 22 |
|
| 23 |
pipe.enable_attention_slicing()
|
| 24 |
pipe.enable_vae_slicing()
|
| 25 |
pipe.set_progress_bar_config(disable=True)
|
| 26 |
|
| 27 |
+
# -------------------------------
|
| 28 |
+
# PROMPT UNDERSTANDING ENGINE
|
| 29 |
+
# -------------------------------
|
| 30 |
def refine_prompt(user_prompt: str):
|
| 31 |
+
"""
|
| 32 |
+
Deterministically converts user input to a structured prompt and negative prompt.
|
| 33 |
+
Ensures SD generates exactly the object the user wants.
|
| 34 |
+
"""
|
| 35 |
+
# Lowercase and strip
|
| 36 |
+
p = user_prompt.lower().strip()
|
| 37 |
+
|
| 38 |
+
# Attempt to extract a single object from known list
|
| 39 |
+
known_objects = ["apple","banana","snake","cat","dog","fox","rabbit","dragon","bird","frog","hamster"]
|
| 40 |
+
obj_match = next((w for w in known_objects if w in p), None)
|
| 41 |
+
subject = obj_match if obj_match else p
|
| 42 |
+
|
| 43 |
+
# Style detection
|
| 44 |
+
is_cute = any(w in p for w in ["cute","adorable","kawaii"])
|
| 45 |
+
is_realistic = any(w in p for w in ["realistic","photo","photograph"])
|
| 46 |
+
is_cartoon = any(w in p for w in ["cartoon","anime","illustration"])
|
| 47 |
+
|
| 48 |
+
# Base prompt template
|
| 49 |
+
prompt = f"a single {subject}, centered, isolated"
|
| 50 |
|
| 51 |
if is_cute:
|
| 52 |
+
prompt += ", cute, friendly, rounded body, big expressive eyes, soft lighting, smooth cartoon style, pastel colors"
|
| 53 |
+
elif is_cartoon:
|
| 54 |
+
prompt += ", cartoon style, clean lines, vibrant colors, simple background"
|
|
|
|
|
|
|
| 55 |
elif is_realistic:
|
| 56 |
+
prompt += ", ultra realistic, natural anatomy, professional photography"
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
+
prompt += ", high quality, detailed, clean background"
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Strong negative prompt to prevent hallucinations
|
| 61 |
negative = (
|
| 62 |
+
"multiple objects, duplicate, blurry, low quality, cropped, out of frame, "
|
| 63 |
+
"horror, grotesque, aggressive, scary, weird colors, artifacts"
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
return prompt, negative
|
| 67 |
|
| 68 |
+
# -------------------------------
|
| 69 |
+
# ETA CALCULATION
|
| 70 |
+
# -------------------------------
|
| 71 |
def estimate_time(steps, resolution):
|
| 72 |
+
# CPU empirical timing (seconds per step)
|
| 73 |
+
per_step = {
|
| 74 |
+
256: 6,
|
| 75 |
+
512: 12,
|
| 76 |
+
768: 25,
|
| 77 |
+
1024: 45
|
| 78 |
+
}[int(resolution)]
|
| 79 |
+
|
| 80 |
+
overhead = 10 # initial model load / conditioning
|
| 81 |
+
est = overhead + steps * per_step
|
| 82 |
+
minutes = est // 60
|
| 83 |
+
seconds = est % 60
|
| 84 |
+
return f"⏱️ Estimated time: ~{int(minutes)}m {int(seconds)}s"
|
| 85 |
+
|
| 86 |
+
# -------------------------------
|
| 87 |
+
# GENERATION FUNCTION (with live status)
|
| 88 |
+
# -------------------------------
|
| 89 |
def generate(prompt, resolution, steps):
|
| 90 |
+
start_time = time.time()
|
| 91 |
yield None, "🧠 Understanding your prompt..."
|
| 92 |
+
|
| 93 |
refined_prompt, neg_prompt = refine_prompt(prompt)
|
| 94 |
+
|
| 95 |
+
yield None, "🎨 Generating image (CPU, please wait)..."
|
| 96 |
+
|
| 97 |
seed = random.randint(0, 10**9)
|
| 98 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 99 |
|
| 100 |
+
pipe.scheduler.set_timesteps(int(steps)) # ensure LCM fast path
|
| 101 |
+
|
| 102 |
img = pipe(
|
| 103 |
prompt=refined_prompt,
|
| 104 |
negative_prompt=neg_prompt,
|
|
|
|
| 108 |
height=int(resolution),
|
| 109 |
generator=gen
|
| 110 |
).images[0]
|
| 111 |
+
|
| 112 |
+
duration = int(time.time() - start_time)
|
| 113 |
yield [img], f"✅ Finished in {duration}s | Seed: {seed}"
|
| 114 |
|
| 115 |
+
# -------------------------------
|
| 116 |
+
# GRADIO UI
|
| 117 |
+
# -------------------------------
|
| 118 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 119 |
+
gr.Markdown("# 👾 CREEPER AI — CPU SMART IMAGE GENERATION")
|
| 120 |
+
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column():
|
| 123 |
+
prompt_in = gr.Textbox(label="Prompt", placeholder="cute snake", lines=2)
|
| 124 |
+
|
| 125 |
+
resolution = gr.Radio([256, 512, 768, 1024], value=512, label="Resolution")
|
| 126 |
+
|
| 127 |
+
steps = gr.Slider(2, 8, value=4, step=1, label="Steps")
|
| 128 |
+
|
| 129 |
+
eta = gr.Markdown("⏱️ Estimated time: ~1m 0s")
|
| 130 |
+
gen_btn = gr.Button("Generate")
|
| 131 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
with gr.Column():
|
| 133 |
status = gr.Markdown("🟢 Ready")
|
| 134 |
gallery = gr.Gallery(columns=1)
|
| 135 |
+
|
| 136 |
+
# Update ETA dynamically
|
| 137 |
for ctrl in [steps, resolution]:
|
| 138 |
ctrl.change(estimate_time, [steps, resolution], eta)
|
| 139 |
+
|
| 140 |
+
gen_btn.click(
|
| 141 |
generate,
|
| 142 |
inputs=[prompt_in, resolution, steps],
|
| 143 |
outputs=[gallery, status]
|