ZennyKenny's picture
Update app.py
000f373 verified
raw
history blame
5.2 kB
import gradio as gr
import numpy as np
import random
import spaces
import os
import torch
import re
from PIL import Image
from diffusers import DiffusionPipeline, AutoencoderTiny
from huggingface_hub import login
from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
# Ensure image_preview dir exists
os.makedirs("image_preview", exist_ok=True)
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
# ✅ Load model only once
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
pipe = DiffusionPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev",
torch_dtype=dtype,
vae=taef1
).to(device)
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
pipe.load_lora_weights("ZennyKenny/flux_lora_natalie-diffusion")
def sanitize_filename(name):
return re.sub(r"[^a-zA-Z0-9_-]", "_", name)[:80]
@spaces.GPU(duration=75)
def infer(user_token, prompt, seed=42, randomize_seed=False, width=1024, height=1024,
guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
# Authenticate using user's token for this session
login(token=user_token)
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
full_prompt = f"XTON {prompt}"
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
prompt=full_prompt,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
width=width,
height=height,
generator=generator,
output_type="pil",
):
# Save low-quality JPG
safe_name = sanitize_filename(prompt)
img_path = f"image_preview/{safe_name}_{seed}.jpg"
img.convert("RGB").save(img_path, "JPEG", quality=60)
# Collect previews
previews = [f"image_preview/{f}" for f in sorted(os.listdir("image_preview")) if f.endswith(".jpg")]
return img, seed, previews
examples = [
["your_token_here", "a man walking in the forest"],
["your_token_here", "a viking ship sailing down a river"],
["your_token_here", "a woman resting by an open fire"],
["your_token_here", "a sword fight in a medieval village"]
]
with gr.Blocks(css="style.css") as natalie_diffusion:
with gr.Row():
with gr.Column(scale=1, elem_id="left-column"):
gr.Markdown("""
# ХТОНЬ: Natalie LoRA Image Generator
Generate images in the surreal style of artist [Natalie Kav](https://www.behance.net/nataliKav), adapted using a custom LoRA on the FLUX.1 [dev] model.
> This space is designed for prototyping concept art for a forthcoming game called **ХТОНЬ**. All outputs are generated locally in the browser using GPU acceleration.
""")
hf_token_input = gr.Textbox(
label="Your Hugging Face API Token",
placeholder="Paste your token here",
type="password"
)
with gr.Row():
prompt = gr.Text(
label="Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt...",
container=False,
)
run_button = gr.Button("🎨 Generate", scale=0)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
with gr.Row():
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=28)
result_example = gr.Image(visible=False)
gr.Examples(
examples=examples,
fn=infer,
inputs=[hf_token_input, prompt],
outputs=[result_example, seed, gr.Gallery(visible=False)],
cache_examples=False,
)
with gr.Column(scale=1, elem_id="right-column"):
result = gr.Image(label="", show_label=False, elem_id="generated-image")
with gr.Column():
gr.Markdown("<h3 style='text-align:center;'>Generated Images Preview</h3>")
gallery = gr.Gallery(label="", columns=4, height="auto", object_fit="cover")
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[hf_token_input, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
outputs=[result, seed, gallery],
)
if __name__ == "__main__":
natalie_diffusion.launch()