CryptoCreeper commited on
Commit
6194646
·
verified ·
1 Parent(s): ff9e34a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -92
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-ONLY, FAST)
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
- # FAST PROMPT ANALYZER (NO DELAYS)
30
  # -------------------------------------------------
31
- def analyze_prompt(prompt: str):
32
- p = prompt.lower()
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 scene
38
- is_scene = any(k in p for k in [
39
- "room", "street", "city", "forest", "landscape", "background"
40
- ])
41
 
42
- if single_object and not is_scene:
43
- optimized = (
44
- f"single centered {prompt}, isolated object, plain background, "
45
- f"sharp focus, product photo"
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
- return optimized, neg, is_scene
55
 
56
- # -------------------------------------------------
57
- # SMART AUTO-CROP (FAST, NO ML)
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
- if not mask.any():
64
- return img # safety fallback
 
 
 
 
 
 
 
 
 
 
65
 
66
- coords = np.argwhere(mask)
67
- y0, x0 = coords.min(axis=0)
68
- y1, x1 = coords.max(axis=0)
69
 
70
- h, w = gray.shape
71
- x0 = max(0, x0 - padding)
72
- y0 = max(0, y0 - padding)
73
- x1 = min(w, x1 + padding)
74
- y1 = min(h, y1 + padding)
75
 
76
- return img.crop((x0, y0, x1, y1))
77
 
78
  # -------------------------------------------------
79
- # GENERATION CORE (FAST <10s)
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
- optimized_prompt, neg_prompt, is_scene = analyze_prompt(prompt)
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, 999999) if random_seed else int(seed_val)
100
  gen = torch.Generator("cpu").manual_seed(seed)
101
 
102
- image = pipe(
103
- prompt=optimized_prompt,
104
  negative_prompt=neg_prompt,
105
  num_inference_steps=int(steps),
106
- guidance_scale=1.2, # LCM sweet spot
107
- width=width,
108
- height=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"✅ Done in {duration}s | Seed: {seed}"
118
 
119
- return [image], status
120
 
121
  # -------------------------------------------------
122
- # ETA (CPU-REALISTIC)
123
  # -------------------------------------------------
124
- def estimate_time(steps, size, auto):
125
- base = 0.9 # model overhead
126
- res = 448 if auto else int(size)
127
- step_cost = 0.22 * (res / 512) ** 2
128
- est = base + steps * step_cost
129
- return f"⚡ Estimated: ~{round(est, 1)}s"
 
130
 
131
  # -------------------------------------------------
132
  # UI
133
  # -------------------------------------------------
134
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
135
- gr.Markdown("# 👾 CREEPER AI — FAST SMART IMAGE CORE")
136
 
137
  with gr.Row():
138
  with gr.Column():
139
- prompt_in = gr.Textbox(label="Prompt", placeholder="apple")
140
- auto_res = gr.Checkbox(label="Smart Auto-Resolution", value=True)
141
- size = gr.Radio([512, 768], value=512, label="Base Resolution")
142
- steps = gr.Slider(2, 6, value=4, step=1, label="Steps")
143
-
144
- random_seed = gr.Checkbox(label="Random Seed", value=True)
145
- seed_val = gr.Number(label="Manual Seed", value=42, visible=False)
146
-
147
- eta = gr.Markdown("⚡ Estimated: ~2.0s")
 
 
 
 
 
 
 
 
 
 
 
 
148
  btn = gr.Button("Generate")
149
 
150
  with gr.Column():
151
  status = gr.Markdown("🟢 Ready")
152
  gallery = gr.Gallery(columns=1)
153
 
154
- random_seed.change(lambda x: gr.update(visible=not x), random_seed, seed_val)
155
- for ctrl in [steps, size, auto_res]:
156
- ctrl.change(estimate_time, [steps, size, auto_res], eta)
157
 
158
  btn.click(
159
  generate,
160
- inputs=[prompt_in, size, steps, random_seed, seed_val, auto_res],
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