CryptoCreeper commited on
Commit
551486d
·
verified ·
1 Parent(s): d820615

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -105
app.py CHANGED
@@ -6,157 +6,178 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
6
  from diffusers import DiffusionPipeline, LCMScheduler
7
  from PIL import Image, ImageFilter
8
 
9
- # -------------------------------
10
  # PASSWORD
11
- # -------------------------------
12
  PASSWORD = "CREEPERIMG"
13
 
14
- # -------------------------------
15
- # PROMPT ENHANCER SETUP
16
- # -------------------------------
17
- ENHANCER_MODEL = "HuggingFaceTB/SmolLM-135M-Instruct"
18
- tokenizer_enhancer = AutoTokenizer.from_pretrained(ENHANCER_MODEL)
19
- model_enhancer = AutoModelForCausalLM.from_pretrained(ENHANCER_MODEL)
20
-
21
- def enhance_text(user_prompt, negative=False):
22
- """Enhance user prompt or negative prompt using small LLM with strict instructions"""
23
- if not user_prompt.strip():
24
- return ""
25
-
26
- if negative:
27
- instruction = f"""
28
- You are an assistant for Stable Diffusion / image generation.
29
- Rewrite the user negative prompt to make it clear what to avoid visually.
30
- Do NOT add unrelated commentary.
31
-
32
- User input: {user_prompt}
33
- Enhanced negative prompt (visual only):
34
- """
35
- else:
36
- instruction = f"""
37
- You are an assistant for Stable Diffusion / image generation.
38
- Your task is to **only rewrite the user prompt** to add details for generating a clear, visual image.
39
- Do NOT include unrelated topics, commentary, or references to text content.
40
- Focus solely on visual aspects.
41
-
42
- User input: {user_prompt}
43
- Enhanced prompt (visual only):
44
- """
45
-
46
- inputs = tokenizer_enhancer(instruction, return_tensors="pt")
47
- outputs = model_enhancer.generate(
48
  **inputs,
49
- max_new_tokens=50,
50
- temperature=0.7,
51
- do_sample=True
52
  )
53
- return tokenizer_enhancer.decode(outputs[0], skip_special_tokens=True).strip()
54
 
55
- # -------------------------------
56
- # IMAGE MODEL SETUP (CPU)
57
- # -------------------------------
58
- IMG_MODEL_ID = "runwayml/stable-diffusion-v1-5"
59
- IMG_ADAPTER_ID = "latent-consistency/lcm-lora-sdv1-5"
 
 
 
 
 
 
 
 
60
 
61
  pipe = DiffusionPipeline.from_pretrained(
62
- IMG_MODEL_ID,
63
  torch_dtype=torch.float32,
64
  safety_checker=None
65
  )
 
66
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
67
- pipe.load_lora_weights(IMG_ADAPTER_ID)
68
  pipe.to("cpu")
 
69
  pipe.enable_attention_slicing()
70
  pipe.enable_vae_slicing()
71
  pipe.set_progress_bar_config(disable=True)
72
 
73
- # -------------------------------
74
- # ETA
75
- # -------------------------------
76
- def estimate_time(steps, resolution):
 
77
  steps = int(steps)
78
- resolution = int(resolution)
79
- per_step = {512:12, 768:25, 1024:45}[resolution]
80
- overhead = 2
81
- est = overhead + steps * per_step
82
- mins = est // 60
83
- secs = est % 60
84
- return f"⏱️ Estimated: ~{mins}m {secs}s"
85
-
86
- # -------------------------------
87
- # GENERATE IMAGE WITH BLUR REVEAL
88
- # -------------------------------
89
- def generate(password, prompt, neg_prompt, resolution, steps):
90
  if password != PASSWORD:
91
- return [Image.new("RGB", (int(resolution), int(resolution)), (255,255,255))], "❌ Wrong password", ""
92
-
93
- # 1️⃣ Enhance prompts
94
- enhanced_prompt = enhance_text(prompt, negative=False)
95
- enhanced_negative = enhance_text(neg_prompt, negative=True)
96
-
97
- # Show enhanced prompt for debugging
98
- yield [Image.new("RGB", (int(resolution), int(resolution)), (255,255,255))], "🟡 Generating...", enhanced_prompt
99
-
100
- # 2️⃣ Generate the image
101
- seed = random.randint(0, 10**9)
102
- gen = torch.Generator("cpu").manual_seed(seed)
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  pipe.scheduler.set_timesteps(int(steps))
104
 
105
- img = pipe(
106
- prompt=enhanced_prompt,
107
- negative_prompt=enhanced_negative,
 
108
  num_inference_steps=int(steps),
109
  guidance_scale=1.2,
110
- width=int(resolution),
111
- height=int(resolution),
112
- generator=gen
113
  ).images[0]
 
114
 
115
- # 3️⃣ Progressive blur reveal
116
- max_blur = 20
117
  for i in range(10):
118
- blur_pct = 100 - i*10
119
- blurred = img.filter(ImageFilter.GaussianBlur(radius=max_blur * blur_pct/100))
120
- yield [blurred], "🟢 Revealing...", enhanced_prompt
 
 
 
121
  time.sleep(1)
122
 
123
- # 4️⃣ Final image
124
- yield [img], f"✅ Done | Seed: {seed}", enhanced_prompt
 
 
 
125
 
126
- # -------------------------------
127
- # GRADIO UI
128
- # -------------------------------
129
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
130
- gr.Markdown("# 👾 CREEPER AI — SMART IMAGE GENERATOR")
131
  gr.Markdown(
132
- "1: The higher the resolution & steps, the longer the image takes to make.\n"
133
- "2: The more detailed the prompt and negative prompt, the better the result."
134
  )
135
 
136
  with gr.Row():
137
  with gr.Column():
138
- password_in = gr.Textbox(label="Password", placeholder="Enter password to enable generation")
139
- prompt_in = gr.Textbox(label="Prompt")
140
- neg_in = gr.Textbox(label="Negative Prompt")
141
 
142
  resolution = gr.Radio([512, 768, 1024], value=512, label="Resolution")
143
- steps = gr.Slider(6,8,value=6,step=1,label="Steps")
144
 
145
- eta = gr.Markdown("⏱️ Estimated: ~1m 0s")
146
- gen_btn = gr.Button("Generate")
147
 
148
  status = gr.Markdown("🟢 Ready")
149
- enhanced_box = gr.Textbox(label="Enhanced Prompt (sent to image model)", interactive=False)
 
 
 
150
 
151
  with gr.Column():
152
  gallery = gr.Gallery(columns=1)
153
 
154
- for ctrl in [steps, resolution]:
155
- ctrl.change(estimate_time, [steps, resolution], eta)
156
 
157
- gen_btn.click(
158
  generate,
159
- inputs=[password_in, prompt_in, neg_in, resolution, steps],
160
  outputs=[gallery, status, enhanced_box]
161
  )
162
 
 
6
  from diffusers import DiffusionPipeline, LCMScheduler
7
  from PIL import Image, ImageFilter
8
 
9
+ # ===============================
10
  # PASSWORD
11
+ # ===============================
12
  PASSWORD = "CREEPERIMG"
13
 
14
+ # ===============================
15
+ # TEXT MODEL (PROMPT ENHANCER)
16
+ # ===============================
17
+ TEXT_MODEL_ID = "HuggingFaceTB/SmolLM-135M-Instruct"
18
+
19
+ tokenizer = AutoTokenizer.from_pretrained(TEXT_MODEL_ID)
20
+ text_model = AutoModelForCausalLM.from_pretrained(TEXT_MODEL_ID)
21
+
22
+ def enhance_prompt(user_prompt: str) -> str:
23
+ """
24
+ Enhances the user prompt for image generation.
25
+ Returns ONLY the enhanced prompt text.
26
+ """
27
+ instruction = (
28
+ "Please enhance this prompt so it is suitable for an image generator "
29
+ "that requires clear instructions. Analyse the prompt, and output as "
30
+ "much visual detail as possible about it.\n\n"
31
+ f"Prompt to enhance: {user_prompt}\n\n"
32
+ "Enhanced prompt:"
33
+ )
34
+
35
+ inputs = tokenizer(instruction, return_tensors="pt")
36
+ outputs = text_model.generate(
 
 
 
 
 
 
 
 
 
 
 
37
  **inputs,
38
+ max_new_tokens=60,
39
+ temperature=0.6,
40
+ do_sample=True,
41
  )
 
42
 
43
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
+
45
+ # ⛔️ CRITICAL: extract only what comes AFTER "Enhanced prompt:"
46
+ if "Enhanced prompt:" in decoded:
47
+ decoded = decoded.split("Enhanced prompt:")[-1]
48
+
49
+ return decoded.strip()
50
+
51
+ # ===============================
52
+ # IMAGE MODEL (CPU)
53
+ # ===============================
54
+ IMG_MODEL = "runwayml/stable-diffusion-v1-5"
55
+ LCM_LORA = "latent-consistency/lcm-lora-sdv1-5"
56
 
57
  pipe = DiffusionPipeline.from_pretrained(
58
+ IMG_MODEL,
59
  torch_dtype=torch.float32,
60
  safety_checker=None
61
  )
62
+
63
  pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
64
+ pipe.load_lora_weights(LCM_LORA)
65
  pipe.to("cpu")
66
+
67
  pipe.enable_attention_slicing()
68
  pipe.enable_vae_slicing()
69
  pipe.set_progress_bar_config(disable=True)
70
 
71
+ # ===============================
72
+ # TIME ESTIMATION
73
+ # ===============================
74
+ def estimate_time(steps, res):
75
+ res = int(res)
76
  steps = int(steps)
77
+ base = {512: 12, 768: 25, 1024: 45}[res]
78
+ total = steps * base + 5
79
+ return f"⏱️ Estimated: ~{total//60}m {total%60}s"
80
+
81
+ # ===============================
82
+ # GENERATION FUNCTION
83
+ # ===============================
84
+ def generate(password, prompt, negative, resolution, steps):
85
+ size = int(resolution)
86
+
87
+ # Password gate
 
88
  if password != PASSWORD:
89
+ return (
90
+ [Image.new("RGB", (size, size), "white")],
91
+ "❌ Wrong password",
92
+ ""
93
+ )
94
+
95
+ # Placeholder while thinking
96
+ yield (
97
+ [Image.new("RGB", (size, size), "white")],
98
+ "🧠 Enhancing prompt...",
99
+ ""
100
+ )
101
+
102
+ enhanced = enhance_prompt(prompt)
103
+
104
+ # Show enhanced prompt immediately
105
+ yield (
106
+ [Image.new("RGB", (size, size), "white")],
107
+ "🎨 Generating image...",
108
+ enhanced
109
+ )
110
+
111
+ seed = random.randint(0, 1_000_000_000)
112
+ generator = torch.Generator("cpu").manual_seed(seed)
113
+
114
  pipe.scheduler.set_timesteps(int(steps))
115
 
116
+ start = time.time()
117
+ image = pipe(
118
+ prompt=enhanced,
119
+ negative_prompt=negative,
120
  num_inference_steps=int(steps),
121
  guidance_scale=1.2,
122
+ width=size,
123
+ height=size,
124
+ generator=generator
125
  ).images[0]
126
+ elapsed = int(time.time() - start)
127
 
128
+ # Blur reveal (UI-only)
 
129
  for i in range(10):
130
+ blur = image.filter(ImageFilter.GaussianBlur(radius=(10 - i)))
131
+ yield (
132
+ [blur],
133
+ f"👀 Revealing... ({i+1}/10)",
134
+ enhanced
135
+ )
136
  time.sleep(1)
137
 
138
+ yield (
139
+ [image],
140
+ f"✅ Done in {elapsed}s | Seed {seed}",
141
+ enhanced
142
+ )
143
 
144
+ # ===============================
145
+ # UI
146
+ # ===============================
147
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
148
+ gr.Markdown("# 👾 Creeper AI — Image Generator")
149
  gr.Markdown(
150
+ "1️⃣ The higher the resolution & steps, the longer the image takes.\n\n"
151
+ "2️⃣ More detailed prompts = better results."
152
  )
153
 
154
  with gr.Row():
155
  with gr.Column():
156
+ password = gr.Textbox(label="Password", type="password")
157
+ prompt = gr.Textbox(label="Prompt")
158
+ negative = gr.Textbox(label="Negative Prompt")
159
 
160
  resolution = gr.Radio([512, 768, 1024], value=512, label="Resolution")
161
+ steps = gr.Slider(6, 8, value=6, step=1, label="Steps")
162
 
163
+ eta = gr.Markdown("⏱️ Estimated: ~1m")
164
+ generate_btn = gr.Button("Generate")
165
 
166
  status = gr.Markdown("🟢 Ready")
167
+ enhanced_box = gr.Textbox(
168
+ label="Enhanced Prompt (exact output from text AI)",
169
+ interactive=False
170
+ )
171
 
172
  with gr.Column():
173
  gallery = gr.Gallery(columns=1)
174
 
175
+ resolution.change(estimate_time, [steps, resolution], eta)
176
+ steps.change(estimate_time, [steps, resolution], eta)
177
 
178
+ generate_btn.click(
179
  generate,
180
+ inputs=[password, prompt, negative, resolution, steps],
181
  outputs=[gallery, status, enhanced_box]
182
  )
183