CryptoCreeper commited on
Commit
dcfaebf
·
verified ·
1 Parent(s): e7f92b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -195
app.py CHANGED
@@ -1,228 +1,156 @@
1
  import gradio as gr
2
- import requests
3
- import traceback
4
-
5
- # -----------------------------
6
- # CONFIG
7
- # -----------------------------
8
- TEXT_MODEL = "HuggingFaceTB/SmolLM-135M-Instruct"
9
- Z_IMAGE_TURBO_API = "https://mrfakename-z-image-turbo.hf.space/gradio_api/mcp/"
10
- DEFAULT_IMAGE_MODEL = "runwayml/stable-diffusion-v1-5"
11
-
12
- # -----------------------------
13
- # PROMPT ENHANCER
14
- # -----------------------------
15
- def enhance_prompt(user_prompt):
16
- system_prompt = (
 
 
17
  "Please enhance this prompt so it is suitable for an image generator "
18
  "that requires clear instructions. Analyse the prompt, and output as "
19
- "much detail as possible about it.\n\n"
20
- f"Prompt to enhance: {user_prompt}"
 
21
  )
22
 
23
- from transformers import pipeline
24
-
25
- pipe = pipeline(
26
- "text-generation",
27
- model=TEXT_MODEL,
28
  max_new_tokens=500,
29
  temperature=0.6,
30
- do_sample=True
31
  )
32
 
33
- result = pipe(system_prompt)
34
- return result[0]["generated_text"].strip()
35
-
36
- # -----------------------------
37
- # DEFAULT CPU IMAGE GENERATOR
38
- # -----------------------------
39
- def generate_cpu_image(prompt, negative_prompt, resolution, steps):
40
- from diffusers import StableDiffusionPipeline
41
- import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- width, height = resolution
44
 
45
- pipe = StableDiffusionPipeline.from_pretrained(
46
- DEFAULT_IMAGE_MODEL,
47
- torch_dtype=torch.float32
48
  )
49
 
50
- pipe = pipe.to("cpu")
 
51
 
 
 
 
52
  image = pipe(
53
- prompt=prompt,
54
- negative_prompt=negative_prompt,
55
- width=width,
56
- height=height,
57
- num_inference_steps=steps
 
 
58
  ).images[0]
59
-
60
- return image
61
-
62
- # -----------------------------
63
- # ULTRA SPEED IMAGE GENERATOR
64
- # -----------------------------
65
- def generate_ultra_image(prompt, width, height, steps):
66
- payload = {
67
- "prompt": prompt,
68
- "width": width,
69
- "height": height,
70
- "num_inference_steps": steps,
71
- "seed": 0,
72
- "randomize_seed": True
73
- }
74
-
75
- try:
76
- response = requests.post(
77
- Z_IMAGE_TURBO_API + "Z_Image_Turbo_generate_image",
78
- json=payload,
79
- timeout=120
80
  )
 
81
 
82
- if response.status_code != 200:
83
- raise RuntimeError(
84
- f"HTTP {response.status_code}: {response.text}"
85
- )
86
-
87
- data = response.json()
88
-
89
- if "error" in data:
90
- raise RuntimeError(str(data["error"]))
91
-
92
- return data["image"]
93
-
94
- except Exception as e:
95
- error_message = (
96
- "❌ **Ultra Speed Error**\n\n"
97
- f"**Exception type:** `{type(e).__name__}`\n\n"
98
- "**Details:**\n"
99
- "```\n"
100
- f"{str(e)}\n"
101
- "```\n\n"
102
- "**Traceback:**\n"
103
- "```\n"
104
- f"{traceback.format_exc()}\n"
105
- "```"
106
- )
107
- return None, error_message
108
-
109
- # -----------------------------
110
- # MAIN PIPELINE
111
- # -----------------------------
112
- def generate(
113
- user_prompt,
114
- negative_prompt,
115
- resolution_choice,
116
- ultra_speed,
117
- steps_slider
118
- ):
119
- width_map = {
120
- "512": (512, 512),
121
- "768": (768, 768),
122
- "1024": (1024, 1024)
123
- }
124
-
125
- width, height = width_map[resolution_choice]
126
-
127
- # ULTRA SPEED PATH
128
- if ultra_speed:
129
- result = generate_ultra_image(
130
- prompt=user_prompt,
131
- width=width,
132
- height=height,
133
- steps=steps_slider
134
- )
135
-
136
- if isinstance(result, tuple):
137
- _, error = result
138
- return None, error
139
-
140
- return result, ""
141
-
142
- # NORMAL PATH (TEXT → IMAGE)
143
- enhanced_prompt = enhance_prompt(user_prompt)
144
-
145
- image = generate_cpu_image(
146
- enhanced_prompt,
147
- negative_prompt,
148
- (width, height),
149
- steps_slider
150
  )
151
 
152
- return image, ""
153
-
154
- # -----------------------------
155
  # UI
156
- # -----------------------------
157
- with gr.Blocks() as demo:
158
- gr.Markdown("## 👾 CREEPER AI IMAGE GENERATOR")
159
  gr.Markdown(
160
- "1: The higher the resolution & steps, the longer the image takes to make.\n"
161
- "2: The more detailed the prompt and negative prompt, the better the result."
162
  )
163
 
164
- prompt = gr.Textbox(label="Prompt")
165
- negative_prompt = gr.Textbox(label="Negative Prompt", value="")
 
 
166
 
167
- ultra_speed = gr.Checkbox(
168
- label="Ultra Speed (a few generations per day)",
169
- value=False
170
- )
171
 
172
- resolution = gr.Radio(
173
- ["512", "768", "1024"],
174
- value="512",
175
- label="Resolution"
176
- )
177
 
178
- steps_slider = gr.Slider(
179
- minimum=6,
180
- maximum=8,
181
- step=1,
182
- value=6,
183
- label="Steps"
184
- )
185
 
186
- generate_btn = gr.Button("Generate")
187
- output_image = gr.Image()
188
- error_box = gr.Markdown()
189
-
190
- # -------------------------
191
- # UI LOGIC
192
- # -------------------------
193
- def toggle_ultra(checked):
194
- # Update resolution choices and step range instantly
195
- if checked:
196
- return (
197
- gr.update(choices=["512", "1024"], value="512"),
198
- gr.update(minimum=6, maximum=20, value=6)
199
- )
200
- else:
201
- return (
202
- gr.update(choices=["512", "768", "1024"], value="512"),
203
- gr.update(minimum=6, maximum=8, value=6)
204
- )
205
-
206
- ultra_speed.change(
207
- toggle_ultra,
208
- inputs=ultra_speed,
209
- outputs=[resolution, steps_slider]
210
- )
211
 
212
  generate_btn.click(
213
  generate,
214
- inputs=[
215
- prompt,
216
- negative_prompt,
217
- resolution,
218
- ultra_speed,
219
- steps_slider
220
- ],
221
- outputs=[output_image, error_box]
222
  )
223
 
224
- # -----------------------------
225
- # ENTRYPOINT
226
- # -----------------------------
227
- if __name__ == "__main__":
228
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
+ import torch
3
+ import random
4
+ import time
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ from diffusers import DiffusionPipeline, LCMScheduler
7
+ from PIL import Image, ImageFilter
8
+
9
+ # ===============================
10
+ # TEXT MODEL (PROMPT ENHANCER)
11
+ # ===============================
12
+ TEXT_MODEL_ID = "HuggingFaceTB/SmolLM-135M-Instruct"
13
+
14
+ tokenizer = AutoTokenizer.from_pretrained(TEXT_MODEL_ID)
15
+ text_model = AutoModelForCausalLM.from_pretrained(TEXT_MODEL_ID)
16
+
17
+ def enhance_prompt(user_prompt: str) -> str:
18
+ instruction = (
19
  "Please enhance this prompt so it is suitable for an image generator "
20
  "that requires clear instructions. Analyse the prompt, and output as "
21
+ "much visual detail as possible about it.\n\n"
22
+ f"Prompt to enhance: {user_prompt}\n\n"
23
+ "Enhanced prompt:"
24
  )
25
 
26
+ inputs = tokenizer(instruction, return_tensors="pt")
27
+ outputs = text_model.generate(
28
+ **inputs,
 
 
29
  max_new_tokens=500,
30
  temperature=0.6,
31
+ do_sample=True,
32
  )
33
 
34
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
+
36
+ if "Enhanced prompt:" in decoded:
37
+ decoded = decoded.split("Enhanced prompt:")[-1]
38
+
39
+ return decoded.strip()
40
+
41
+ # ===============================
42
+ # IMAGE MODEL (CPU)
43
+ # ===============================
44
+ IMG_MODEL = "runwayml/stable-diffusion-v1-5"
45
+ LCM_LORA = "latent-consistency/lcm-lora-sdv1-5"
46
+
47
+ pipe = DiffusionPipeline.from_pretrained(
48
+ IMG_MODEL,
49
+ torch_dtype=torch.float32,
50
+ safety_checker=None
51
+ )
52
+
53
+ pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
54
+ pipe.load_lora_weights(LCM_LORA)
55
+ pipe.to("cpu")
56
+
57
+ pipe.enable_attention_slicing()
58
+ pipe.enable_vae_slicing()
59
+ pipe.set_progress_bar_config(disable=True)
60
+
61
+ # ===============================
62
+ # TIME ESTIMATION
63
+ # ===============================
64
+ def estimate_time(steps, res):
65
+ res = int(res)
66
+ steps = int(steps)
67
+ base = {512: 12, 768: 25, 1024: 45}[res]
68
+ total = steps * base + 5
69
+ return f"⏱️ Estimated: ~{total//60}m {total%60}s"
70
+
71
+ # ===============================
72
+ # GENERATION FUNCTION
73
+ # ===============================
74
+ def generate(prompt, negative, resolution, steps):
75
+ size = int(resolution)
76
+
77
+ # Placeholder while thinking
78
+ yield (
79
+ [Image.new("RGB", (size, size), "white")],
80
+ "🧠 Enhancing prompt..."
81
+ )
82
 
83
+ enhanced = enhance_prompt(prompt)
84
 
85
+ yield (
86
+ [Image.new("RGB", (size, size), "white")],
87
+ "🎨 Generating image..."
88
  )
89
 
90
+ seed = random.randint(0, 1_000_000_000)
91
+ generator = torch.Generator("cpu").manual_seed(seed)
92
 
93
+ pipe.scheduler.set_timesteps(int(steps))
94
+
95
+ start = time.time()
96
  image = pipe(
97
+ prompt=enhanced,
98
+ negative_prompt=negative,
99
+ num_inference_steps=int(steps),
100
+ guidance_scale=1.2,
101
+ width=size,
102
+ height=size,
103
+ generator=generator
104
  ).images[0]
105
+ elapsed = int(time.time() - start)
106
+
107
+ # Blur reveal
108
+ for i in range(10):
109
+ blur = image.filter(ImageFilter.GaussianBlur(radius=(10 - i)))
110
+ yield (
111
+ [blur],
112
+ f"👀 Revealing... ({i+1}/10)"
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  )
114
+ time.sleep(1)
115
 
116
+ yield (
117
+ [image],
118
+ f" Done in {elapsed}s | Seed {seed}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  )
120
 
121
+ # ===============================
 
 
122
  # UI
123
+ # ===============================
124
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
125
+ gr.Markdown("# 👾 Creeper AI Image Generator")
126
  gr.Markdown(
127
+ "1️⃣ The higher the resolution & steps, the longer the image takes.\n\n"
128
+ "2️⃣ The more detailed the prompt and negative prompt, the better the result."
129
  )
130
 
131
+ with gr.Row():
132
+ with gr.Column():
133
+ prompt = gr.Textbox(label="Prompt")
134
+ negative = gr.Textbox(label="Negative Prompt")
135
 
136
+ resolution = gr.Radio([512, 768, 1024], value=512, label="Resolution")
137
+ steps = gr.Slider(6, 8, value=6, step=1, label="Steps")
 
 
138
 
139
+ eta = gr.Markdown("⏱️ Estimated: ~1m")
140
+ generate_btn = gr.Button("Generate")
 
 
 
141
 
142
+ status = gr.Markdown("🟢 Ready")
 
 
 
 
 
 
143
 
144
+ with gr.Column():
145
+ gallery = gr.Gallery(columns=1)
146
+
147
+ resolution.change(estimate_time, [steps, resolution], eta)
148
+ steps.change(estimate_time, [steps, resolution], eta)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
  generate_btn.click(
151
  generate,
152
+ inputs=[prompt, negative, resolution, steps],
153
+ outputs=[gallery, status]
 
 
 
 
 
 
154
  )
155
 
156
+ demo.launch(share=False)