Spaces:
Paused
Paused
CryptoCreeper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import time
|
| 5 |
-
import re
|
| 6 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 7 |
|
| 8 |
# -------------------------------
|
|
@@ -25,80 +24,60 @@ pipe.enable_vae_slicing()
|
|
| 25 |
pipe.set_progress_bar_config(disable=True)
|
| 26 |
|
| 27 |
# -------------------------------
|
| 28 |
-
#
|
| 29 |
# -------------------------------
|
| 30 |
-
def
|
| 31 |
"""
|
| 32 |
-
|
| 33 |
-
Ensures SD generates exactly the object the user wants.
|
| 34 |
"""
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
known_objects = ["apple","banana","snake","cat","dog","fox","rabbit","dragon","bird","frog","hamster"]
|
| 40 |
-
obj_match = next((w for w in known_objects if w in p), None)
|
| 41 |
-
subject = obj_match if obj_match else p
|
| 42 |
|
| 43 |
# Style detection
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
prompt = f"a single {subject}, centered, isolated"
|
| 50 |
-
|
| 51 |
-
if is_cute:
|
| 52 |
-
prompt += ", cute, friendly, rounded body, big expressive eyes, soft lighting, smooth cartoon style, pastel colors"
|
| 53 |
-
elif is_cartoon:
|
| 54 |
-
prompt += ", cartoon style, clean lines, vibrant colors, simple background"
|
| 55 |
-
elif is_realistic:
|
| 56 |
-
prompt += ", ultra realistic, natural anatomy, professional photography"
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
return prompt, negative
|
| 67 |
|
| 68 |
# -------------------------------
|
| 69 |
-
# ETA CALCULATION
|
| 70 |
# -------------------------------
|
| 71 |
def estimate_time(steps, resolution):
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
256: 6,
|
| 75 |
-
512: 12,
|
| 76 |
-
768: 25,
|
| 77 |
-
1024: 45
|
| 78 |
-
}[int(resolution)]
|
| 79 |
-
|
| 80 |
-
overhead = 10 # initial model load / conditioning
|
| 81 |
est = overhead + steps * per_step
|
| 82 |
minutes = est // 60
|
| 83 |
seconds = est % 60
|
| 84 |
return f"⏱️ Estimated time: ~{int(minutes)}m {int(seconds)}s"
|
| 85 |
|
| 86 |
# -------------------------------
|
| 87 |
-
# GENERATION
|
| 88 |
# -------------------------------
|
| 89 |
def generate(prompt, resolution, steps):
|
|
|
|
| 90 |
start_time = time.time()
|
| 91 |
yield None, "🧠 Understanding your prompt..."
|
| 92 |
-
|
| 93 |
-
refined_prompt, neg_prompt = refine_prompt(prompt)
|
| 94 |
-
|
| 95 |
yield None, "🎨 Generating image (CPU, please wait)..."
|
| 96 |
-
|
|
|
|
| 97 |
seed = random.randint(0, 10**9)
|
| 98 |
gen = torch.Generator("cpu").manual_seed(seed)
|
| 99 |
-
|
| 100 |
-
pipe.scheduler.set_timesteps(int(steps)) # ensure LCM fast path
|
| 101 |
|
|
|
|
| 102 |
img = pipe(
|
| 103 |
prompt=refined_prompt,
|
| 104 |
negative_prompt=neg_prompt,
|
|
@@ -108,7 +87,7 @@ def generate(prompt, resolution, steps):
|
|
| 108 |
height=int(resolution),
|
| 109 |
generator=gen
|
| 110 |
).images[0]
|
| 111 |
-
|
| 112 |
duration = int(time.time() - start_time)
|
| 113 |
yield [img], f"✅ Finished in {duration}s | Seed: {seed}"
|
| 114 |
|
|
|
|
| 2 |
import torch
|
| 3 |
import random
|
| 4 |
import time
|
|
|
|
| 5 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 6 |
|
| 7 |
# -------------------------------
|
|
|
|
| 24 |
pipe.set_progress_bar_config(disable=True)
|
| 25 |
|
| 26 |
# -------------------------------
|
| 27 |
+
# FAST THINKING PROMPT ENGINE
|
| 28 |
# -------------------------------
|
| 29 |
+
def refine_prompt_fast(user_prompt: str):
|
| 30 |
"""
|
| 31 |
+
Instant deterministic prompt refinement (<1ms)
|
|
|
|
| 32 |
"""
|
| 33 |
+
# Known objects
|
| 34 |
+
known_objects = {"apple","banana","snake","cat","dog","fox","rabbit","dragon","bird","frog","hamster"}
|
| 35 |
+
object_found = next((w for w in known_objects if w in user_prompt.lower()), None)
|
| 36 |
+
subject = object_found if object_found else user_prompt.lower().strip()
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Style detection
|
| 39 |
+
style = ""
|
| 40 |
+
if any(w in user_prompt.lower() for w in ["cute","adorable","kawaii"]):
|
| 41 |
+
style = ", cute, friendly, rounded body, big eyes, pastel colors, cartoon style"
|
| 42 |
+
elif any(w in user_prompt.lower() for w in ["realistic","photo","photograph"]):
|
| 43 |
+
style = ", ultra realistic, natural anatomy, professional photography"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
+
style = ", high quality, clean background"
|
| 46 |
|
| 47 |
+
# Construct prompt
|
| 48 |
+
prompt = f"a single {subject}, centered, isolated{style}"
|
| 49 |
+
|
| 50 |
+
# Negative prompt (always same)
|
| 51 |
+
negative = "multiple objects, duplicate, blurry, low quality, cropped, out of frame, horror, grotesque, aggressive, weird colors, artifacts"
|
| 52 |
|
| 53 |
return prompt, negative
|
| 54 |
|
| 55 |
# -------------------------------
|
| 56 |
+
# REALISTIC ETA CALCULATION
|
| 57 |
# -------------------------------
|
| 58 |
def estimate_time(steps, resolution):
|
| 59 |
+
per_step = {256:6, 512:12, 768:25, 1024:45}[int(resolution)]
|
| 60 |
+
overhead = 2 # thinking stage is <1s, plus small overhead
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
est = overhead + steps * per_step
|
| 62 |
minutes = est // 60
|
| 63 |
seconds = est % 60
|
| 64 |
return f"⏱️ Estimated time: ~{int(minutes)}m {int(seconds)}s"
|
| 65 |
|
| 66 |
# -------------------------------
|
| 67 |
+
# IMAGE GENERATION WITH LIVE STATUS
|
| 68 |
# -------------------------------
|
| 69 |
def generate(prompt, resolution, steps):
|
| 70 |
+
# --- THINKING PHASE ---
|
| 71 |
start_time = time.time()
|
| 72 |
yield None, "🧠 Understanding your prompt..."
|
| 73 |
+
refined_prompt, neg_prompt = refine_prompt_fast(prompt)
|
|
|
|
|
|
|
| 74 |
yield None, "🎨 Generating image (CPU, please wait)..."
|
| 75 |
+
|
| 76 |
+
# --- GENERATION PHASE ---
|
| 77 |
seed = random.randint(0, 10**9)
|
| 78 |
gen = torch.Generator("cpu").manual_seed(seed)
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
pipe.scheduler.set_timesteps(int(steps)) # fast path
|
| 81 |
img = pipe(
|
| 82 |
prompt=refined_prompt,
|
| 83 |
negative_prompt=neg_prompt,
|
|
|
|
| 87 |
height=int(resolution),
|
| 88 |
generator=gen
|
| 89 |
).images[0]
|
| 90 |
+
|
| 91 |
duration = int(time.time() - start_time)
|
| 92 |
yield [img], f"✅ Finished in {duration}s | Seed: {seed}"
|
| 93 |
|