CryptoCreeper commited on
Commit
83feb10
·
verified ·
1 Parent(s): 26c98e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -46
app.py CHANGED
@@ -4,7 +4,7 @@ import random
4
  from diffusers import DiffusionPipeline, LCMScheduler
5
  import time
6
 
7
- # Optimized Load
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 get_auto_res(prompt):
17
- # Logic: Analyze prompt for shape. Default 512.
18
  p = prompt.lower()
19
- if any(x in p for x in ["tall", "portrait", "person", "tree"]):
20
- return 448, 576 # Optimized portrait
21
- elif any(x in p for x in ["wide", "landscape", "panorama", "horizon"]):
22
- return 640, 384 # Optimized landscape
23
- return 480, 480 # Tight square
24
 
25
- def generate_process(prompt, neg_prompt, size, steps, is_random, manual_seed, bulk_count, auto_res, progress=gr.Progress()):
26
  start_total = time.time()
27
  all_images = []
28
 
29
- # Queue Detection Emulation
30
- progress(0, desc="⌛ Checking Queue Status...")
31
- time.sleep(0.5)
32
- progress(0, desc="🚀 Queue Clear: Initializing Generation...")
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
- step_start = time.time()
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=f"high quality, detailed, {prompt}", # Instruction enhancement
50
  negative_prompt=neg_prompt,
51
  num_inference_steps=int(steps),
52
- guidance_scale=1.5, # Slightly higher for better instruction following
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
- return all_images, f"✨ **Batch Complete:** {total_duration}s | **Res:** {w}x{h}"
61
 
62
  def update_estimate(steps, size, bulk, auto):
63
- res = 480 if auto else int(size)
64
- base = 13.5 * bulk
65
- factor = (res / 512) ** 2
66
- est = round(steps * base * factor, 1)
67
- return f"⚡ **Estimated Total Time:** ~{est} seconds"
 
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; font-weight: bold !important; }
73
- .side-panel { border: 1px solid #00ffcc !important; padding: 15px; border-radius: 10px; background: rgba(0,255,204,0.05); }
74
  """
75
 
76
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
77
- gr.Markdown("# 👾 CREEPER AI - V2 BULK ENGINE", elem_id="title")
78
 
79
  with gr.Row():
80
  with gr.Column(elem_classes="side-panel"):
81
- prompt = gr.Textbox(label="Instruction Prompt", placeholder="Describe precisely...", lines=3)
82
- neg_prompt = gr.Textbox(label="Negative Prompt", value="blurry, distorted, lowres")
83
 
84
- with gr.Accordion("⚙️ Advanced Engine", open=True):
85
- bulk_slider = gr.Slider(1, 3, value=1, step=1, label="Bulk Generation (Images)")
86
- auto_res = gr.Checkbox(label="Auto-Resolution (Speed Optimize)", value=False)
87
- size_choice = gr.Radio(choices=[512, 768], label="Fixed Resolution", value=512)
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 Total Time:** ~54.0 seconds")
95
- gen_btn = gr.Button("INITIALIZE CLUSTER", variant="primary", elem_classes="generate-btn")
96
 
97
  with gr.Column():
98
  status = gr.Markdown("🟢 **System Ready**")
99
- gallery = gr.Gallery(label="Output Buffer", columns=1, height="auto")
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