Spaces:
Paused
Paused
CryptoCreeper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,32 +7,32 @@ from diffusers import DiffusionPipeline, LCMScheduler
|
|
| 7 |
from PIL import Image, ImageFilter
|
| 8 |
|
| 9 |
# -------------------------------
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# -------------------------------
|
| 12 |
ENHANCER_MODEL = "HuggingFaceTB/SmolLM-135M-Instruct"
|
| 13 |
tokenizer_enhancer = AutoTokenizer.from_pretrained(ENHANCER_MODEL)
|
| 14 |
model_enhancer = AutoModelForCausalLM.from_pretrained(ENHANCER_MODEL)
|
| 15 |
|
| 16 |
-
def enhance_text(user_prompt
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if prefix:
|
| 22 |
-
instruction = f"{prefix} {user_prompt}"
|
| 23 |
-
|
| 24 |
-
inputs = tokenizer_enhancer(instruction, return_tensors="pt")
|
| 25 |
outputs = model_enhancer.generate(
|
| 26 |
**inputs,
|
| 27 |
max_new_tokens=50,
|
| 28 |
temperature=0.7,
|
| 29 |
do_sample=True
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
return text.strip()
|
| 33 |
|
| 34 |
# -------------------------------
|
| 35 |
-
# IMAGE MODEL SETUP (CPU
|
| 36 |
# -------------------------------
|
| 37 |
IMG_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 38 |
IMG_ADAPTER_ID = "latent-consistency/lcm-lora-sdv1-5"
|
|
@@ -63,18 +63,20 @@ def estimate_time(steps, resolution):
|
|
| 63 |
return f"⏱️ Estimated: ~{mins}m {secs}s"
|
| 64 |
|
| 65 |
# -------------------------------
|
| 66 |
-
# GENERATE WITH
|
| 67 |
# -------------------------------
|
| 68 |
-
def generate(prompt, neg_prompt, resolution, steps):
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
seed = random.randint(0, 10**9)
|
| 79 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 80 |
pipe.scheduler.set_timesteps(int(steps))
|
|
@@ -89,26 +91,30 @@ def generate(prompt, neg_prompt, resolution, steps):
|
|
| 89 |
generator=gen
|
| 90 |
).images[0]
|
| 91 |
|
| 92 |
-
#
|
| 93 |
max_blur = 20
|
| 94 |
for i in range(10):
|
| 95 |
blur_pct = 100 - i*10
|
| 96 |
blurred = img.filter(ImageFilter.GaussianBlur(radius=max_blur * blur_pct/100))
|
| 97 |
-
yield [blurred], "🟢 Revealing..."
|
| 98 |
time.sleep(1)
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
yield [img], f"✅ Done | Seed: {seed}"
|
| 102 |
|
| 103 |
# -------------------------------
|
| 104 |
# GRADIO UI
|
| 105 |
# -------------------------------
|
| 106 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 107 |
gr.Markdown("# 👾 CREEPER AI — SMART IMAGE GENERATOR")
|
| 108 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
with gr.Row():
|
| 111 |
with gr.Column():
|
|
|
|
| 112 |
prompt_in = gr.Textbox(label="Prompt")
|
| 113 |
neg_in = gr.Textbox(label="Negative Prompt")
|
| 114 |
|
|
@@ -119,6 +125,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 119 |
gen_btn = gr.Button("Generate")
|
| 120 |
|
| 121 |
status = gr.Markdown("🟢 Ready")
|
|
|
|
| 122 |
|
| 123 |
with gr.Column():
|
| 124 |
gallery = gr.Gallery(columns=1)
|
|
@@ -128,8 +135,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 128 |
|
| 129 |
gen_btn.click(
|
| 130 |
generate,
|
| 131 |
-
inputs=[prompt_in, neg_in, resolution, steps],
|
| 132 |
-
outputs=[gallery, status]
|
| 133 |
)
|
| 134 |
|
| 135 |
demo.launch()
|
|
|
|
| 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):
|
| 22 |
+
"""Enhance user prompt using small LLM"""
|
| 23 |
+
if not user_prompt.strip():
|
| 24 |
+
return ""
|
| 25 |
+
inputs = tokenizer_enhancer(user_prompt, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
outputs = model_enhancer.generate(
|
| 27 |
**inputs,
|
| 28 |
max_new_tokens=50,
|
| 29 |
temperature=0.7,
|
| 30 |
do_sample=True
|
| 31 |
)
|
| 32 |
+
return tokenizer_enhancer.decode(outputs[0], skip_special_tokens=True).strip()
|
|
|
|
| 33 |
|
| 34 |
# -------------------------------
|
| 35 |
+
# IMAGE MODEL SETUP (CPU)
|
| 36 |
# -------------------------------
|
| 37 |
IMG_MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 38 |
IMG_ADAPTER_ID = "latent-consistency/lcm-lora-sdv1-5"
|
|
|
|
| 63 |
return f"⏱️ Estimated: ~{mins}m {secs}s"
|
| 64 |
|
| 65 |
# -------------------------------
|
| 66 |
+
# GENERATE IMAGE WITH BLUR REVEAL
|
| 67 |
# -------------------------------
|
| 68 |
+
def generate(password, prompt, neg_prompt, resolution, steps):
|
| 69 |
+
if password != PASSWORD:
|
| 70 |
+
return [Image.new("RGB", (int(resolution), int(resolution)), (255,255,255))], "❌ Wrong password", ""
|
| 71 |
+
|
| 72 |
+
# 1️⃣ Enhance prompts
|
| 73 |
+
enhanced_prompt = enhance_text(prompt)
|
| 74 |
+
enhanced_negative = enhance_text(neg_prompt)
|
| 75 |
+
|
| 76 |
+
# Show the enhanced prompt for debugging
|
| 77 |
+
yield [Image.new("RGB", (int(resolution), int(resolution)), (255,255,255))], "🟡 Generating...", enhanced_prompt
|
| 78 |
+
|
| 79 |
+
# 2️⃣ Generate the image
|
| 80 |
seed = random.randint(0, 10**9)
|
| 81 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 82 |
pipe.scheduler.set_timesteps(int(steps))
|
|
|
|
| 91 |
generator=gen
|
| 92 |
).images[0]
|
| 93 |
|
| 94 |
+
# 3️⃣ Progressive blur reveal
|
| 95 |
max_blur = 20
|
| 96 |
for i in range(10):
|
| 97 |
blur_pct = 100 - i*10
|
| 98 |
blurred = img.filter(ImageFilter.GaussianBlur(radius=max_blur * blur_pct/100))
|
| 99 |
+
yield [blurred], "🟢 Revealing...", enhanced_prompt
|
| 100 |
time.sleep(1)
|
| 101 |
|
| 102 |
+
# 4️⃣ Final image
|
| 103 |
+
yield [img], f"✅ Done | Seed: {seed}", enhanced_prompt
|
| 104 |
|
| 105 |
# -------------------------------
|
| 106 |
# GRADIO UI
|
| 107 |
# -------------------------------
|
| 108 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 109 |
gr.Markdown("# 👾 CREEPER AI — SMART IMAGE GENERATOR")
|
| 110 |
+
gr.Markdown(
|
| 111 |
+
"1: The higher the resolution & steps, the longer the image takes to make.\n"
|
| 112 |
+
"2: The more detailed the prompt and negative prompt, the better the result."
|
| 113 |
+
)
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
with gr.Column():
|
| 117 |
+
password_in = gr.Textbox(label="Password", placeholder="Enter password to enable generation")
|
| 118 |
prompt_in = gr.Textbox(label="Prompt")
|
| 119 |
neg_in = gr.Textbox(label="Negative Prompt")
|
| 120 |
|
|
|
|
| 125 |
gen_btn = gr.Button("Generate")
|
| 126 |
|
| 127 |
status = gr.Markdown("🟢 Ready")
|
| 128 |
+
enhanced_box = gr.Textbox(label="Enhanced Prompt (sent to image model)", interactive=False)
|
| 129 |
|
| 130 |
with gr.Column():
|
| 131 |
gallery = gr.Gallery(columns=1)
|
|
|
|
| 135 |
|
| 136 |
gen_btn.click(
|
| 137 |
generate,
|
| 138 |
+
inputs=[password_in, prompt_in, neg_in, resolution, steps],
|
| 139 |
+
outputs=[gallery, status, enhanced_box]
|
| 140 |
)
|
| 141 |
|
| 142 |
demo.launch()
|