import spaces import argparse import os import time from os import path from safetensors.torch import load_file from huggingface_hub import hf_hub_download from transformers.utils.hub import move_cache # move_cache() cache_path = path.join(path.dirname(path.abspath(__file__)), "models") # os.environ["TRANSFORMERS_CACHE"] = cache_path os.environ["HF_HUB_CACHE"] = cache_path os.environ["HF_HOME"] = cache_path import gradio as gr import torch from diffusers import FluxPipeline torch.backends.cuda.matmul.allow_tf32 = True class timer: def __init__(self, method_name="timed process"): self.method = method_name def __enter__(self): self.start = time.time() print(f"{self.method} starts") def __exit__(self, exc_type, exc_val, exc_tb): end = time.time() print(f"{self.method} took {str(round(end - self.start, 2))}s") if not path.exists(cache_path): os.makedirs(cache_path, exist_ok=True) pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16) pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors")) pipe.fuse_lora(lora_scale=0.125) pipe.to(device="cuda", dtype=torch.bfloat16) css = """ # gen_btn{height: 100%} #gen_column{align-self: stretch} .primary{background-color: #4C76FF !important} #grower-label-span span{background-color: #4C76FF !important} #grower-label-image label{background-color: #4C76FF !important} """ js_code = """ function createGradioAnimation() { const emojis = ['✨', '🤖', '📈', '🎨', '🔍', '📱', '🔮', '🥰', '🌈', '💖']; const gravity = 0.5; const bounceFactor = -0.7; const friction = 0.9; document.getElementById('gen_btn').addEventListener('click', (event) => { const count = Math.floor(Math.random() * 6) + 10; for (let i = 0; i < count; i++) { createEmoji(event.clientX, event.clientY); } }); function createEmoji(x, y) { const emojiElement = document.createElement('div'); emojiElement.textContent = emojis[Math.floor(Math.random() * emojis.length)]; emojiElement.style.position = 'absolute'; emojiElement.style.fontSize = '24px'; emojiElement.style.transition = 'opacity 0.1s ease-out'; document.body.appendChild(emojiElement); const rect = emojiElement.getBoundingClientRect(); let posX = x - rect.width / 2; let posY = y - rect.height / 2; let velX = (Math.random() - 0.5) * 10; let velY = (Math.random() - 0.5) * 10; function update() { if (posY + rect.height >= window.innerHeight) { posY = window.innerHeight - rect.height; velY *= bounceFactor; } else { velY += gravity; } if (posX <= 0 || posX + rect.width >= window.innerWidth) { velX *= bounceFactor; } velX *= friction; velY *= friction; posX += velX; posY += velY; emojiElement.style.transform = `translate(${posX}px, ${posY}px)`; if (Math.abs(velX) > 0.1 || Math.abs(velY) > 0.1) { requestAnimationFrame(update); } else { emojiElement.style.opacity=0; setTimeout(function(){ emojiElement.remove();}, 2000); } } update(); } return 'Animation created'; } """ with gr.Blocks(theme='charbel-malo/Crystal', js=js_code) as demo: gr.Markdown( """
HyperFlux-based Image Generation Model 8Steps-Lora
Tip: Be specific in your description for best results!