File size: 9,273 Bytes
3f5c7da 9224688 644e9d0 9224688 644e9d0 29f9e56 13b9d32 9224688 644e9d0 9224688 bd7ebe6 13b9d32 9224688 203aadd df67f98 203aadd 9224688 bd7ebe6 203aadd bd7ebe6 9224688 203aadd 9224688 bd7ebe6 9224688 13b9d32 203aadd 9224688 203aadd 9224688 203aadd 9224688 203aadd 9224688 203aadd 9224688 203aadd 9224688 bd7ebe6 9224688 203aadd 9224688 203aadd 644e9d0 203aadd 9224688 203aadd 9224688 13b9d32 9224688 13b9d32 9224688 bd7ebe6 644e9d0 bd7ebe6 9224688 203aadd 9224688 203aadd 9224688 13b9d32 9224688 13b9d32 9224688 13b9d32 9224688 203aadd 9224688 bd7ebe6 203aadd bd7ebe6 9224688 203aadd 9224688 203aadd 9224688 203aadd 9224688 203aadd 9224688 203aadd 9224688 13b9d32 644e9d0 13b9d32 9224688 bd7ebe6 9224688 bd7ebe6 9224688 203aadd 9224688 13b9d32 9224688 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | import os
# Must be set before torch/diffusers import
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
# MUST come before torch / diffusers (ZeroGPU scanner)
import spaces
import torch
import gradio as gr
import asyncio
import httpx
import tempfile
import traceback
from fastapi import Request
from fastapi.responses import JSONResponse
from starlette.routing import Route as StarletteRoute
# ✅ CORRECT pipeline for Juggernaut Z — NOT Lumina2Pipeline
from diffusers import ZImagePipeline
# ============================================================
# CONFIG
# ============================================================
MODEL_ID = os.getenv(
"MODEL_ID",
"RunDiffusion/Juggernaut-Z-Image",
)
TELEGRAM_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", "")
TELEGRAM_API = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}" if TELEGRAM_TOKEN else ""
MAX_IMAGES = int(os.getenv("MAX_IMAGES_PER_REQUEST", "10"))
GPU_DURATION = int(os.getenv("GPU_DURATION_SEC", "120"))
# Juggernaut Z recommended settings (official model card)
STEPS = int(os.getenv("GEN_STEPS", "35"))
GUIDANCE = float(os.getenv("GEN_GUIDANCE", "6.0"))
GEN_SIZE = int(os.getenv("GEN_SIZE", "1024"))
# ============================================================
# MODEL LOAD (module level — ZeroGPU streams weights on first GPU call)
# ============================================================
print(f"[BOOT] Loading Juggernaut Z pipeline: {MODEL_ID}")
# Use ZImagePipeline. If your diffusers version rejects it, fall back to:
# from diffusers import DiffusionPipeline
# pipe = DiffusionPipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
pipe = ZImagePipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.bfloat16,
)
print("[BOOT] Pipeline loaded (weights on CPU/disk; ZeroGPU will stream to VRAM)")
# ============================================================
# TELEGRAM HELPERS
# ============================================================
async def send_message(chat_id: int, text: str) -> None:
if not TELEGRAM_TOKEN:
return
try:
async with httpx.AsyncClient(timeout=15.0) as client:
await client.post(
f"{TELEGRAM_API}/sendMessage",
json={
"chat_id": chat_id,
"text": text,
"parse_mode": "HTML",
"disable_web_page_preview": True,
},
)
except Exception as e:
print(f"[TG] send_message failed: {e}")
async def send_photo(chat_id: int, path: str, caption: str = "") -> None:
if not TELEGRAM_TOKEN:
return
try:
async with httpx.AsyncClient(timeout=120.0) as client:
with open(path, "rb") as f:
await client.post(
f"{TELEGRAM_API}/sendPhoto",
data={"chat_id": chat_id, "caption": caption, "parse_mode": "HTML"},
files={"photo": f},
)
except Exception as e:
print(f"[TG] send_photo failed: {e}")
# ============================================================
# GPU FUNCTION — the ONLY @spaces.GPU function in this Space
# ============================================================
@spaces.GPU(duration=GPU_DURATION)
def generate_image_gpu(prompt: str, seed: int = -1) -> str:
"""
Generate one hyper-realistic image with Juggernaut Z.
Official recommended settings: 35 steps, CFG 6.0.
"""
# Move to GPU on first call. ZeroGPU caches weights in VRAM afterwards.
if pipe.device.type != "cuda":
pipe.to("cuda")
generator = None
if seed != -1:
generator = torch.Generator(device="cuda").manual_seed(int(seed))
result = pipe(
prompt=prompt,
generator=generator,
num_inference_steps=STEPS,
guidance_scale=GUIDANCE,
height=GEN_SIZE,
width=GEN_SIZE,
)
image = result.images[0]
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
image.save(tmp)
return tmp
# ============================================================
# TASK QUEUE + WORKER
# ============================================================
task_queue: asyncio.Queue = asyncio.Queue()
_worker_task: asyncio.Task | None = None
async def worker() -> None:
print("[WORKER] started")
while True:
chat_id = None
try:
chat_id, prompt, num = await task_queue.get()
await send_message(chat_id, f"🎨 Generating {num} hyper-realistic image(s)...")
for i in range(num):
await send_message(chat_id, f"⏳ Image {i + 1}/{num} — rendering (35 steps)...")
try:
# Run the sync GPU function in a thread so we don't block the loop
path = await asyncio.to_thread(generate_image_gpu, prompt)
await send_photo(chat_id, path, f"✅ Image {i + 1}/{num}")
try:
os.unlink(path)
except OSError:
pass
except Exception as e:
print(f"[WORKER] image {i + 1} failed: {e}\n{traceback.format_exc()}")
await send_message(chat_id, f"❌ Image {i + 1} failed: {e}")
await send_message(chat_id, "🎉 All done!")
except Exception as e:
print(f"[WORKER] fatal: {e}\n{traceback.format_exc()}")
if chat_id:
try:
await send_message(chat_id, f"❌ Worker error: {e}")
except Exception:
pass
finally:
task_queue.task_done()
# ============================================================
# FASTAPI ROUTES
# ============================================================
async def telegram_webhook(request: Request):
global _worker_task
# Lazy-start the worker inside the running event loop
if _worker_task is None:
_worker_task = asyncio.create_task(worker())
try:
update = await request.json()
msg = update.get("message", {})
chat_id = msg.get("chat", {}).get("id")
text = msg.get("text", "")
if chat_id and text:
if text.startswith("/start"):
await send_message(
chat_id,
"👋 <b>Hyper-Realistic Image Bot (Juggernaut Z)</b>\n\n"
f"Send <code>/imagine N <prompt></code> (N = 1..{MAX_IMAGES}).\n"
"Example: <code>/imagine 2 cinematic portrait of an old fisherman, dramatic lighting</code>",
)
elif text.startswith("/imagine"):
parts = text.split(maxsplit=2)
if len(parts) >= 3:
try:
num = int(parts[1])
if not 1 <= num <= MAX_IMAGES:
raise ValueError
except ValueError:
await send_message(chat_id, f"❌ Number must be 1..{MAX_IMAGES}")
return JSONResponse({"ok": True})
prompt = parts[2]
await task_queue.put((chat_id, prompt, num))
await send_message(chat_id, f"✅ Queued {num} image(s): <i>{prompt}</i>")
else:
await send_message(chat_id, "Usage: <code>/imagine N prompt</code>")
except Exception as e:
print(f"[WEBHOOK] error: {e}\n{traceback.format_exc()}")
return JSONResponse({"ok": True})
async def health_check(request: Request):
return JSONResponse({
"ok": True,
"status": "running",
"queue_size": task_queue.qsize(),
"worker_alive": _worker_task is not None and not _worker_task.done(),
})
# ============================================================
# GRADIO DEMO — hidden button satisfies the ZeroGPU scanner
# ============================================================
with gr.Blocks() as demo:
gr.Markdown("# 🎨 Hyper-Realistic Image Bot (Juggernaut Z)\n\nServes a Telegram bot via `/telegram`.")
_btn = gr.Button("init-gpu", visible=False)
_in = gr.Textbox(value="cinematic portrait, dramatic lighting, photorealistic", visible=False)
_out = gr.File(visible=False)
_btn.click(fn=generate_image_gpu, inputs=[_in], outputs=[_out])
# ============================================================
# LAUNCH — proven pattern from your history
# ============================================================
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
ssr_mode=False, # prevents Node proxy from swallowing /telegram
prevent_thread_lock=True, # returns control so we can inject routes
)
# Inject routes at index 0 so they beat Gradio's catch-all Mount
live_app = demo.app
live_app.router.routes.insert(
0, StarletteRoute("/telegram", telegram_webhook, methods=["POST"])
)
live_app.router.routes.insert(
0, StarletteRoute("/health", health_check, methods=["GET"])
)
live_app.build_middleware_stack()
print("[BOOT] Routes injected: POST /telegram, GET /health")
print("[BOOT] Set webhook → https://<space>.hf.space/telegram")
demo.block_thread() |