Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,245 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import random
|
| 4 |
-
import time
|
| 5 |
import requests
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
-
|
| 10 |
-
# ============================================================
|
| 11 |
# CONFIG
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
TEXT_MODEL_ID = "HuggingFaceTB/SmolLM-135M-Instruct"
|
| 16 |
|
| 17 |
Z_IMAGE_TURBO_API = "https://mrfakename-z-image-turbo.hf.space/gradio_api/mcp/"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
pipe = DiffusionPipeline.from_pretrained(
|
| 23 |
-
SD_MODEL_ID,
|
| 24 |
-
safety_checker=None,
|
| 25 |
-
torch_dtype=torch.float32,
|
| 26 |
-
)
|
| 27 |
-
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 28 |
-
pipe.load_lora_weights(LCM_ADAPTER_ID)
|
| 29 |
-
pipe.to("cpu")
|
| 30 |
-
|
| 31 |
-
# ============================================================
|
| 32 |
-
# LOAD TEXT MODEL (CPU)
|
| 33 |
-
# ============================================================
|
| 34 |
-
tokenizer = AutoTokenizer.from_pretrained(TEXT_MODEL_ID)
|
| 35 |
-
text_model = AutoModelForCausalLM.from_pretrained(TEXT_MODEL_ID)
|
| 36 |
-
text_model.to("cpu")
|
| 37 |
-
|
| 38 |
-
# ============================================================
|
| 39 |
# PROMPT ENHANCER
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
"Please enhance this prompt so it is suitable for an image generator "
|
| 44 |
-
"that requires clear instructions. Analyse the prompt and output as "
|
| 45 |
-
"much
|
| 46 |
f"Prompt to enhance: {user_prompt}"
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
|
|
|
| 58 |
|
| 59 |
-
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# ============================================================
|
| 67 |
-
def generate_image(
|
| 68 |
-
prompt,
|
| 69 |
negative_prompt,
|
| 70 |
-
|
| 71 |
-
steps,
|
| 72 |
ultra_speed,
|
|
|
|
| 73 |
):
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
placeholder = torch.ones((512, 512, 3))
|
| 78 |
-
yield [placeholder.numpy()], "🧠 Preparing..."
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
# ULTRA SPEED PATH (EXTERNAL API)
|
| 82 |
-
# ========================================================
|
| 83 |
if ultra_speed:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
response = requests.post(
|
| 95 |
-
Z_IMAGE_TURBO_API + "Z_Image_Turbo_generate_image",
|
| 96 |
-
json=payload,
|
| 97 |
-
timeout=120,
|
| 98 |
-
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
raise RuntimeError(
|
| 102 |
-
f"HTTP {response.status_code}\n\nResponse body:\n{response.text}"
|
| 103 |
-
)
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
except Exception as e:
|
| 119 |
-
error_message = f"""
|
| 120 |
-
❌ **Ultra Speed Error**
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import traceback
|
| 4 |
|
| 5 |
+
# -----------------------------
|
|
|
|
|
|
|
|
|
|
| 6 |
# CONFIG
|
| 7 |
+
# -----------------------------
|
| 8 |
+
|
| 9 |
+
TEXT_MODEL = "HuggingFaceTB/SmolLM-135M-Instruct"
|
|
|
|
| 10 |
|
| 11 |
Z_IMAGE_TURBO_API = "https://mrfakename-z-image-turbo.hf.space/gradio_api/mcp/"
|
| 12 |
|
| 13 |
+
DEFAULT_IMAGE_MODEL = "runwayml/stable-diffusion-v1-5"
|
| 14 |
+
|
| 15 |
+
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# PROMPT ENHANCER
|
| 17 |
+
# -----------------------------
|
| 18 |
+
|
| 19 |
+
def enhance_prompt(user_prompt):
|
| 20 |
+
system_prompt = (
|
| 21 |
"Please enhance this prompt so it is suitable for an image generator "
|
| 22 |
+
"that requires clear instructions. Analyse the prompt, and output as "
|
| 23 |
+
"much detail as possible about it.\n\n"
|
| 24 |
f"Prompt to enhance: {user_prompt}"
|
| 25 |
)
|
| 26 |
|
| 27 |
+
from transformers import pipeline
|
| 28 |
+
|
| 29 |
+
pipe = pipeline(
|
| 30 |
+
"text-generation",
|
| 31 |
+
model=TEXT_MODEL,
|
| 32 |
+
max_new_tokens=500,
|
| 33 |
+
temperature=0.6,
|
| 34 |
+
do_sample=True
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
result = pipe(system_prompt)
|
| 38 |
+
return result[0]["generated_text"].strip()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# -----------------------------
|
| 42 |
+
# DEFAULT CPU IMAGE GENERATOR
|
| 43 |
+
# -----------------------------
|
| 44 |
+
|
| 45 |
+
def generate_cpu_image(prompt, negative_prompt, resolution):
|
| 46 |
+
from diffusers import StableDiffusionPipeline
|
| 47 |
+
import torch
|
| 48 |
+
|
| 49 |
+
width, height = resolution
|
| 50 |
+
|
| 51 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 52 |
+
DEFAULT_IMAGE_MODEL,
|
| 53 |
+
torch_dtype=torch.float32
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
pipe = pipe.to("cpu")
|
| 57 |
+
|
| 58 |
+
image = pipe(
|
| 59 |
+
prompt=prompt,
|
| 60 |
+
negative_prompt=negative_prompt,
|
| 61 |
+
width=width,
|
| 62 |
+
height=height,
|
| 63 |
+
num_inference_steps=20
|
| 64 |
+
).images[0]
|
| 65 |
+
|
| 66 |
+
return image
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# -----------------------------
|
| 70 |
+
# ULTRA SPEED IMAGE GENERATOR
|
| 71 |
+
# -----------------------------
|
| 72 |
+
|
| 73 |
+
def generate_ultra_image(prompt, width, height, steps):
|
| 74 |
+
payload = {
|
| 75 |
+
"prompt": prompt,
|
| 76 |
+
"width": width,
|
| 77 |
+
"height": height,
|
| 78 |
+
"num_inference_steps": steps,
|
| 79 |
+
"seed": 0,
|
| 80 |
+
"randomize_seed": True
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
response = requests.post(
|
| 85 |
+
Z_IMAGE_TURBO_API + "Z_Image_Turbo_generate_image",
|
| 86 |
+
json=payload,
|
| 87 |
+
timeout=120
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if response.status_code != 200:
|
| 91 |
+
raise RuntimeError(
|
| 92 |
+
f"HTTP {response.status_code}: {response.text}"
|
| 93 |
+
)
|
| 94 |
|
| 95 |
+
data = response.json()
|
| 96 |
+
|
| 97 |
+
if "error" in data:
|
| 98 |
+
raise RuntimeError(str(data["error"]))
|
| 99 |
+
|
| 100 |
+
return data["image"]
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
error_message = (
|
| 104 |
+
"❌ **Ultra Speed Error**\n\n"
|
| 105 |
+
f"**Exception type:** `{type(e).__name__}`\n\n"
|
| 106 |
+
"**Details:**\n"
|
| 107 |
+
"```\n"
|
| 108 |
+
f"{str(e)}\n"
|
| 109 |
+
"```\n\n"
|
| 110 |
+
"**Traceback:**\n"
|
| 111 |
+
"```\n"
|
| 112 |
+
f"{traceback.format_exc()}\n"
|
| 113 |
+
"```"
|
| 114 |
)
|
| 115 |
+
return None, error_message
|
| 116 |
|
|
|
|
| 117 |
|
| 118 |
+
# -----------------------------
|
| 119 |
+
# MAIN PIPELINE
|
| 120 |
+
# -----------------------------
|
| 121 |
|
| 122 |
+
def generate(
|
| 123 |
+
user_prompt,
|
|
|
|
|
|
|
|
|
|
| 124 |
negative_prompt,
|
| 125 |
+
resolution_choice,
|
|
|
|
| 126 |
ultra_speed,
|
| 127 |
+
ultra_steps
|
| 128 |
):
|
| 129 |
+
width_map = {
|
| 130 |
+
"512": (512, 512),
|
| 131 |
+
"768": (768, 768),
|
| 132 |
+
"1024": (1024, 1024)
|
| 133 |
+
}
|
| 134 |
|
| 135 |
+
width, height = width_map[resolution_choice]
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
# ULTRA SPEED PATH
|
|
|
|
|
|
|
| 138 |
if ultra_speed:
|
| 139 |
+
result = generate_ultra_image(
|
| 140 |
+
prompt=user_prompt,
|
| 141 |
+
width=width,
|
| 142 |
+
height=height,
|
| 143 |
+
steps=ultra_steps
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
if isinstance(result, tuple):
|
| 147 |
+
_, error = result
|
| 148 |
+
return None, error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
return result, ""
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
# NORMAL PATH (TEXT → IMAGE)
|
| 153 |
+
enhanced_prompt = enhance_prompt(user_prompt)
|
| 154 |
|
| 155 |
+
image = generate_cpu_image(
|
| 156 |
+
enhanced_prompt,
|
| 157 |
+
negative_prompt,
|
| 158 |
+
(width, height)
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
return image, ""
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# -----------------------------
|
| 165 |
+
# UI
|
| 166 |
+
# -----------------------------
|
| 167 |
+
|
| 168 |
+
with gr.Blocks() as demo:
|
| 169 |
+
gr.Markdown("## 🖼️ Image Generator")
|
| 170 |
+
|
| 171 |
+
prompt = gr.Textbox(label="Prompt")
|
| 172 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value="")
|
| 173 |
+
|
| 174 |
+
ultra_speed = gr.Checkbox(
|
| 175 |
+
label="Ultra Speed (a few generations per day)",
|
| 176 |
+
value=False
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
resolution = gr.Radio(
|
| 180 |
+
["512", "768", "1024"],
|
| 181 |
+
value="512",
|
| 182 |
+
label="Resolution"
|
| 183 |
+
)
|
| 184 |
|
| 185 |
+
ultra_steps = gr.Slider(
|
| 186 |
+
minimum=6,
|
| 187 |
+
maximum=20,
|
| 188 |
+
step=1,
|
| 189 |
+
value=8,
|
| 190 |
+
label="Ultra Speed Steps",
|
| 191 |
+
visible=False
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
generate_btn = gr.Button("Generate")
|
| 195 |
+
|
| 196 |
+
output_image = gr.Image()
|
| 197 |
+
error_box = gr.Markdown()
|
| 198 |
+
|
| 199 |
+
# -------------------------
|
| 200 |
+
# UI LOGIC
|
| 201 |
+
# -------------------------
|
| 202 |
+
|
| 203 |
+
def toggle_ultra(checked):
|
| 204 |
+
if checked:
|
| 205 |
+
return (
|
| 206 |
+
gr.Radio(
|
| 207 |
+
["512", "1024"],
|
| 208 |
+
value="512"
|
| 209 |
+
),
|
| 210 |
+
gr.update(visible=True)
|
| 211 |
+
)
|
| 212 |
+
else:
|
| 213 |
+
return (
|
| 214 |
+
gr.Radio(
|
| 215 |
+
["512", "768", "1024"],
|
| 216 |
+
value="512"
|
| 217 |
+
),
|
| 218 |
+
gr.update(visible=False)
|
| 219 |
+
)
|
| 220 |
|
| 221 |
+
ultra_speed.change(
|
| 222 |
+
toggle_ultra,
|
| 223 |
+
inputs=ultra_speed,
|
| 224 |
+
outputs=[resolution, ultra_steps]
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
generate_btn.click(
|
| 228 |
+
generate,
|
| 229 |
+
inputs=[
|
| 230 |
+
prompt,
|
| 231 |
+
negative_prompt,
|
| 232 |
+
resolution,
|
| 233 |
+
ultra_speed,
|
| 234 |
+
ultra_steps
|
| 235 |
+
],
|
| 236 |
+
outputs=[output_image, error_box]
|
| 237 |
+
)
|
| 238 |
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
+
# -----------------------------
|
| 241 |
+
# ENTRYPOINT
|
| 242 |
+
# -----------------------------
|
| 243 |
|
| 244 |
+
if __name__ == "__main__":
|
| 245 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|