Spaces:
Paused
Paused
CryptoCreeper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import random
|
|
| 4 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
#
|
| 8 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 9 |
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
|
| 10 |
|
|
@@ -13,94 +13,89 @@ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
|
| 13 |
pipe.load_lora_weights(adapter_id)
|
| 14 |
pipe.to("cpu")
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
#
|
| 18 |
p = prompt.lower()
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return
|
| 24 |
|
| 25 |
-
def generate_process(prompt, neg_prompt, size, steps, is_random, manual_seed, bulk_count, auto_res
|
| 26 |
start_total = time.time()
|
| 27 |
all_images = []
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Resolution Logic
|
| 35 |
-
if auto_res:
|
| 36 |
-
w, h = get_auto_res(prompt)
|
| 37 |
-
else:
|
| 38 |
-
w, h = int(size), int(size)
|
| 39 |
|
| 40 |
for i in range(int(bulk_count)):
|
| 41 |
-
|
| 42 |
-
progress((i/bulk_count), desc=f"🎨 Generating Image {i+1} of {bulk_count}...")
|
| 43 |
|
| 44 |
-
# Seed Logic: If bulk > 1, always randomize to prevent duplicates
|
| 45 |
current_seed = random.randint(0, 10**6) if (is_random or bulk_count > 1) else int(manual_seed)
|
| 46 |
gen = torch.Generator(device="cpu").manual_seed(current_seed)
|
| 47 |
|
|
|
|
| 48 |
img = pipe(
|
| 49 |
-
prompt=
|
| 50 |
negative_prompt=neg_prompt,
|
| 51 |
num_inference_steps=int(steps),
|
| 52 |
-
guidance_scale=
|
| 53 |
width=w, height=h,
|
| 54 |
generator=gen
|
| 55 |
).images[0]
|
| 56 |
|
| 57 |
all_images.append(img)
|
|
|
|
| 58 |
|
| 59 |
total_duration = round(time.time() - start_total, 2)
|
| 60 |
-
|
| 61 |
|
| 62 |
def update_estimate(steps, size, bulk, auto):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
# CSS for "Creeper" aesthetic
|
| 70 |
css = """
|
| 71 |
.gradio-container { background-color: #0b0f19 !important; color: #00ffcc !important; }
|
| 72 |
-
.generate-btn { background: linear-gradient(90deg, #00ffcc, #0077ff) !important;
|
| 73 |
-
.side-panel { border: 1px solid #00ffcc
|
| 74 |
"""
|
| 75 |
|
| 76 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 77 |
-
gr.Markdown("# 👾 CREEPER AI - V2
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
with gr.Column(elem_classes="side-panel"):
|
| 81 |
-
prompt = gr.Textbox(label="Instruction
|
| 82 |
-
neg_prompt = gr.Textbox(label="Negative Prompt",
|
| 83 |
|
| 84 |
-
with gr.Accordion("⚙️
|
| 85 |
-
bulk_slider = gr.Slider(1, 3, value=1, step=1, label="Bulk
|
| 86 |
-
auto_res = gr.Checkbox(label="Auto-
|
| 87 |
-
size_choice = gr.Radio(choices=
|
| 88 |
-
step_slider = gr.Slider(1, 6, value=4, step=1, label="Steps")
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
random_seed = gr.Checkbox(label="Random Seed", value=True)
|
| 92 |
-
seed_val = gr.Number(label="Seed", visible=False, value=42)
|
| 93 |
|
| 94 |
-
est_box = gr.Markdown("⚡ **Estimated
|
| 95 |
-
gen_btn = gr.Button("INITIALIZE
|
| 96 |
|
| 97 |
with gr.Column():
|
| 98 |
status = gr.Markdown("🟢 **System Ready**")
|
| 99 |
-
gallery = gr.Gallery(label="Output
|
| 100 |
|
| 101 |
-
# Event Logic
|
| 102 |
random_seed.change(lambda x: gr.update(visible=not x), random_seed, seed_val)
|
| 103 |
|
|
|
|
| 104 |
for ctrl in [bulk_slider, auto_res, size_choice, step_slider]:
|
| 105 |
ctrl.change(update_estimate, [step_slider, size_choice, bulk_slider, auto_res], est_box)
|
| 106 |
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# Load Model
|
| 8 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 9 |
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
|
| 10 |
|
|
|
|
| 13 |
pipe.load_lora_weights(adapter_id)
|
| 14 |
pipe.to("cpu")
|
| 15 |
|
| 16 |
+
def analyze_auto_res(prompt):
|
| 17 |
+
# If the prompt implies a "scene" or "background", use full 512
|
| 18 |
p = prompt.lower()
|
| 19 |
+
scene_keywords = ["room", "forest", "city", "street", "landscape", "background", "many", "all"]
|
| 20 |
+
if any(k in p for k in scene_keywords):
|
| 21 |
+
return 512, 512
|
| 22 |
+
# If it's a single object, tighten the frame to 448 to save CPU time
|
| 23 |
+
return 448, 448
|
| 24 |
|
| 25 |
+
def generate_process(prompt, neg_prompt, size, steps, is_random, manual_seed, bulk_count, auto_res):
|
| 26 |
start_total = time.time()
|
| 27 |
all_images = []
|
| 28 |
|
| 29 |
+
# Smarter Instruction Following: forcing the model to focus on prompt counts
|
| 30 |
+
enhanced_prompt = f"((centered single object)), {prompt}, high quality, sharp focus"
|
| 31 |
+
|
| 32 |
+
# Auto-Res logic
|
| 33 |
+
w, h = analyze_auto_res(prompt) if auto_res else (int(size), int(size))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
for i in range(int(bulk_count)):
|
| 36 |
+
yield None, f"🤔 **Thinking...** (Processing Image {i+1})"
|
|
|
|
| 37 |
|
|
|
|
| 38 |
current_seed = random.randint(0, 10**6) if (is_random or bulk_count > 1) else int(manual_seed)
|
| 39 |
gen = torch.Generator(device="cpu").manual_seed(current_seed)
|
| 40 |
|
| 41 |
+
# Guidance Scale 2.0 helps prevent "object duplication" (the 10 apples issue)
|
| 42 |
img = pipe(
|
| 43 |
+
prompt=enhanced_prompt,
|
| 44 |
negative_prompt=neg_prompt,
|
| 45 |
num_inference_steps=int(steps),
|
| 46 |
+
guidance_scale=2.0,
|
| 47 |
width=w, height=h,
|
| 48 |
generator=gen
|
| 49 |
).images[0]
|
| 50 |
|
| 51 |
all_images.append(img)
|
| 52 |
+
yield all_images, f"🎨 **Generating...** ({i+1}/{bulk_count})"
|
| 53 |
|
| 54 |
total_duration = round(time.time() - start_total, 2)
|
| 55 |
+
yield all_images, f"✅ **Finished:** {total_duration}s | **Seed used:** {current_seed}"
|
| 56 |
|
| 57 |
def update_estimate(steps, size, bulk, auto):
|
| 58 |
+
# 2-vCPU actual: ~14.2s per step for 512px.
|
| 59 |
+
# Auto-res (448px) is ~25% faster.
|
| 60 |
+
res_val = 448 if auto else int(size)
|
| 61 |
+
time_per_step = 14.2 * ((res_val / 512) ** 2)
|
| 62 |
+
est = round(steps * time_per_step * bulk, 1)
|
| 63 |
+
return f"⚡ **Estimated Wait:** ~{est} seconds"
|
| 64 |
|
|
|
|
| 65 |
css = """
|
| 66 |
.gradio-container { background-color: #0b0f19 !important; color: #00ffcc !important; }
|
| 67 |
+
.generate-btn { background: linear-gradient(90deg, #00ffcc, #0077ff) !important; color: white !important; }
|
| 68 |
+
.side-panel { border: 1px solid #00ffcc; padding: 15px; border-radius: 12px; background: rgba(0,255,204,0.05); }
|
| 69 |
"""
|
| 70 |
|
| 71 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 72 |
+
gr.Markdown("# 👾 CREEPER AI - V2.1 CORE", elem_id="title")
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column(elem_classes="side-panel"):
|
| 76 |
+
prompt = gr.Textbox(label="Instruction", placeholder="Example: One red apple on a table", lines=3)
|
| 77 |
+
neg_prompt = gr.Textbox(label="Negative Prompt", placeholder="Avoid: blurry, many objects, distorted...")
|
| 78 |
|
| 79 |
+
with gr.Accordion("⚙️ Engine Parameters", open=True):
|
| 80 |
+
bulk_slider = gr.Slider(1, 3, value=1, step=1, label="Bulk Count")
|
| 81 |
+
auto_res = gr.Checkbox(label="Auto-Optimize Resolution", value=False)
|
| 82 |
+
size_choice = gr.Radio(choices=, label="Fixed Resolution", value=512)
|
| 83 |
+
step_slider = gr.Slider(1, 6, value=4, step=1, label="Iteration Steps")
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
random_seed = gr.Checkbox(label="Random Seed", value=True)
|
| 87 |
+
seed_val = gr.Number(label="Manual Seed", visible=False, value=42)
|
| 88 |
|
| 89 |
+
est_box = gr.Markdown("⚡ **Estimated Wait:** ~56.8 seconds")
|
| 90 |
+
gen_btn = gr.Button("INITIALIZE GENERATION", variant="primary", elem_classes="generate-btn")
|
| 91 |
|
| 92 |
with gr.Column():
|
| 93 |
status = gr.Markdown("🟢 **System Ready**")
|
| 94 |
+
gallery = gr.Gallery(label="Output", columns=1, height="auto", preview=True)
|
| 95 |
|
|
|
|
| 96 |
random_seed.change(lambda x: gr.update(visible=not x), random_seed, seed_val)
|
| 97 |
|
| 98 |
+
# Update timer live
|
| 99 |
for ctrl in [bulk_slider, auto_res, size_choice, step_slider]:
|
| 100 |
ctrl.change(update_estimate, [step_slider, size_choice, bulk_slider, auto_res], est_box)
|
| 101 |
|