Upload 3 files
Browse filesFiles changed (3)
- app.py+34-11
- generate.pyAdded+174
- requirements.txt+1
123456 unmodified lines12131415161718192021222324252627282930315 unmodified lines37383940414243444546474849501 unmodified line525354555657585960616263646566676869707172import shutilfrom pathlib import Pathimport gradio as grfrom src.generate import GenerateConfig, generate_one6 unmodified linesINPUT_DIR.mkdir(exist_ok=True)OUTPUT_DIR.mkdir(exist_ok=True)def save_reference_images(style_name, files):style_dir = INPUT_DIR / style_name.strip()style_dir.mkdir(parents=True, exist_ok=True)for file_path in files or []:src = Path(file_path)if src.exists():shutil.copy2(src, style_dir / src.name)return style_dirdef run_generation(prompt, style_name, files, steps, guidance, width, height, seed):save_reference_images(style_name, files)style = load_style(INPUT_DIR, style_name)cfg = GenerateConfig(5 unmodified linesdevice="cuda",cpu_offload=True,use_ref_images=True,ref_max_images=10,ref_sample_mode="random",seed=None if seed in ("", None) else int(seed),num_outputs=1,)paths = generate_one(user_prompt=prompt,style=style,out_root=OUTPUT_DIR,cfg=cfg,1 unmodified linereturn str(paths[0])demo = gr.Interface(fn=run_generation,inputs=[gr.Textbox(label="Prompt"),gr.Textbox(label="Style Name"),gr.File(file_count="multiple", file_types=["image"], label="Reference Images"),gr.Slider(1, 30, value=8, step=1, label="Steps"),gr.Slider(1.0, 10.0, value=1.0, step=0.1, label="Guidance"),gr.Slider(256, 1024, value=512, step=64, label="Width"),gr.Slider(256, 1024, value=512, step=64, label="Height"),gr.Textbox(label="Seed (optional)")],outputs=gr.Image(label="Generated Image"),title="StyleX",description="Generate an image from a prompt and reference style images.")if __name__ == "__main__":12345676 unmodified lines1415161718192021222324252627282930313233343536373839404142434445464748495051525 unmodified lines58596061626364656667686970711 unmodified line7374757677787980818283848586878889909192939495import shutilfrom pathlib import Pathimport spacesimport gradio as grfrom src.generate import GenerateConfig, generate_one6 unmodified linesINPUT_DIR.mkdir(exist_ok=True)OUTPUT_DIR.mkdir(exist_ok=True)def save_reference_images(style_name: str, files) -> Path:style_name = style_name.strip()if not style_name:raise ValueError("Style Name is required.")style_dir = INPUT_DIR / style_namestyle_dir.mkdir(parents=True, exist_ok=True)# Clear old files so each run uses only the newly uploaded referencesfor item in style_dir.iterdir():if item.is_file():item.unlink()saved = 0for file_path in files or []:src = Path(file_path)if src.exists():shutil.copy2(src, style_dir / src.name)saved += 1if saved == 0:raise ValueError("Please upload at least one reference image.")return style_dir@spaces.GPU(duration=180)def run_generation(prompt, style_name, files, steps, guidance, width, height, seed):if not prompt or not prompt.strip():raise ValueError("Prompt is required.")save_reference_images(style_name, files)style = load_style(INPUT_DIR, style_name)cfg = GenerateConfig(5 unmodified linesdevice="cuda",cpu_offload=True,use_ref_images=True,ref_max_images=4,ref_sample_mode="random",seed=None if seed in ("", None) else int(seed),num_outputs=1,)paths = generate_one(user_prompt=prompt.strip(),style=style,out_root=OUTPUT_DIR,cfg=cfg,1 unmodified linereturn str(paths[0])demo = gr.Interface(fn=run_generation,inputs=[gr.Textbox(label="Prompt", lines=3, placeholder="Describe the image you want"),gr.Textbox(label="Style Name", placeholder="Example: cyberpunk"),gr.File(file_count="multiple", file_types=["image"], label="Reference Images"),gr.Slider(1, 20, value=6, step=1, label="Steps"),gr.Slider(1.0, 5.0, value=1.0, step=0.1, label="Guidance"),gr.Slider(256, 1024, value=512, step=64, label="Width"),gr.Slider(256, 1024, value=512, step=64, label="Height"),gr.Textbox(label="Seed (optional)", placeholder="Leave blank for random"),],outputs=gr.Image(label="Generated Image"),title="StyleX",description="Generate an image from a prompt and uploaded reference images.",allow_flagging="never",)if __name__ == "__main__":
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import List, Optional
from PIL import Image
from .styles import Style
from .utils import timestamp
IMG_EXTS = {".png", ".jpg", ".jpeg", ".webp"}
def _list_images(folder: Path) -> List[Path]:
if not folder.exists():
return []
files: List[Path] = []
for p in folder.iterdir():
if p.is_file() and p.suffix.lower() in IMG_EXTS:
files.append(p)
return sorted(files)
def _load_pil(path: Path) -> Image.Image:
return Image.open(path).convert("RGB")
def _pick_reference_images(
style_folder: Path,
max_images: int,
sample_mode: str = "random",
seed: Optional[int] = None,
) -> List[Image.Image]:
import torch
paths = _list_images(style_folder)
if not paths or max_images <= 0:
return []
max_images = min(max_images, len(paths))
if sample_mode == "first":
chosen = paths[:max_images]
else:
generator = torch.Generator(device="cpu")
generator.manual_seed(0 if seed is None else int(seed))
idx = torch.randperm(len(paths), generator=generator).tolist()
chosen = [paths[i] for i in idx[:max_images]]
return [_load_pil(p) for p in chosen]
@dataclass
class GenerateConfig:
model_id: str
steps: int = 6
guidance: float = 1.0
height: int = 512
width: int = 512
device: str = "cuda"
torch_dtype_name: str = "bfloat16"
cpu_offload: bool = True
use_ref_images: bool = True
ref_max_images: int = 4
ref_sample_mode: str = "random"
seed: Optional[int] = None
num_outputs: int = 1
def _is_flux2(model_id: str) -> bool:
mid = model_id.lower()
return "flux.2" in mid or "flux2" in mid
def _resolve_torch_dtype(dtype_name: str):
import torch
if dtype_name == "float16":
return torch.float16
if dtype_name == "float32":
return torch.float32
return torch.bfloat16
def _load_flux2_pipeline(model_id: str, cfg: GenerateConfig):
import torch
from diffusers import Flux2KleinPipeline, Flux2Pipeline
torch_dtype = _resolve_torch_dtype(cfg.torch_dtype_name)
if "klein" in model_id.lower():
pipe = Flux2KleinPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
else:
pipe = Flux2Pipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
if cfg.device == "cuda" and torch.cuda.is_available():
if cfg.cpu_offload:
pipe.enable_model_cpu_offload()
else:
pipe.to("cuda")
else:
pipe.to("cpu")
return pipe
def generate_one(
user_prompt: str,
style: Style,
out_root: Path,
cfg: GenerateConfig,
) -> List[Path]:
import torch
out_root.mkdir(parents=True, exist_ok=True)
if not _is_flux2(cfg.model_id):
raise ValueError(
"This generate.py expects a FLUX.2 model_id "
f"(got: {cfg.model_id})"
)
pipe = _load_flux2_pipeline(cfg.model_id, cfg)
if cfg.seed is None:
generator = None
else:
gen_device = "cuda" if (cfg.device == "cuda" and torch.cuda.is_available()) else "cpu"
generator = torch.Generator(device=gen_device).manual_seed(int(cfg.seed))
ref_images: Optional[List[Image.Image]] = None
if cfg.use_ref_images:
ref_images = _pick_reference_images(
style.folder,
max_images=cfg.ref_max_images,
sample_mode=cfg.ref_sample_mode,
seed=cfg.seed,
)
if ref_images:
print(
f"FLUX.2 reference images used: {len(ref_images)} "
f"(mode={cfg.ref_sample_mode}, max={cfg.ref_max_images})"
)
else:
print("FLUX.2 reference images: none found (running txt2img).")
call_kwargs = dict(
prompt=user_prompt,
height=int(cfg.height),
width=int(cfg.width),
num_inference_steps=max(1, int(cfg.steps)),
guidance_scale=float(cfg.guidance),
generator=generator,
num_images_per_prompt=max(1, int(cfg.num_outputs)),
)
if ref_images:
call_kwargs["image"] = ref_images
result = pipe(**call_kwargs)
out_images: List[Image.Image] = list(result.images)
time_id = timestamp()
out_dir = out_root / style.name
out_dir.mkdir(parents=True, exist_ok=True)
paths: List[Path] = []
for i, img in enumerate(out_images, start=1):
out_path = out_dir / f"{time_id}_{i:02d}.png"
img.save(out_path)
paths.append(out_path)
return paths1234gradiotorchdiffuserstransformers12345gradiospacestorchdiffuserstransformers
app.py+34-11
generate.pyAdded+174
requirements.txt+1