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 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("MatthiasBachfischer/open-engineering-orcas", "open-engineering-orcas.safetensors")) pipe.fuse_lora(lora_scale=1.0) pipe.to(device="cuda", dtype=torch.bfloat16) theme = gr.themes.Base( primary_hue=gr.themes.Color(c100="#f4e5dc", c200="#f6c1b0", c300="#f59a86", c400="#f05b48", c50="#fef2f2", c500="#ea1b0a", c600="#c41708", c700="#9d1207", c800="#991b1b", c900="#7f1d1d", c950="#6c1e1e"), font=[gr.themes.GoogleFont('Arial'), 'ui-sans-serif', 'system-ui', 'sans-serif'], ).set( button_primary_background_fill='*primary_500', button_primary_text_color='*neutral_50' ) with gr.Blocks(theme=theme) as demo: gr.Markdown( """
This space hosts a fine-tuned FLUX.1 dev LoRA model to create Open Engineering Orca mascots.
Tip: Be specific in your description for best results!