Instructions to use KIMCAHLLIE/shinkai-style-lora with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use KIMCAHLLIE/shinkai-style-lora with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", dtype=torch.bfloat16, device_map="cuda") pipe.load_lora_weights("KIMCAHLLIE/shinkai-style-lora") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
| import os | |
| import time | |
| import re | |
| import torch | |
| from diffusers import StableDiffusionPipeline | |
| def slugify(text: str, max_len: int = 40) -> str: | |
| text = text.lower() | |
| text = re.sub(r"[^a-z0-9]+", "_", text).strip("_") | |
| return text[:max_len] if len(text) > max_len else text | |
| def build_pipe( | |
| base_model_id: str = "runwayml/stable-diffusion-v1-5", | |
| dtype: torch.dtype = torch.float16, | |
| device: str = "cuda", | |
| use_xformers: bool = False, # T4์์ ์ด์ ์์ผ๋ฉด False ์ถ์ฒ | |
| ): | |
| pipe = StableDiffusionPipeline.from_pretrained( | |
| base_model_id, | |
| torch_dtype=dtype, | |
| safety_checker=None, # ๊ณผ์ /๋ก์ปฌ ๋ฐ๋ชจ๋ฉด ๋ณดํต ๋ | |
| ).to(device) | |
| if use_xformers: | |
| try: | |
| pipe.enable_xformers_memory_efficient_attention() | |
| print("xformers enabled โ ") | |
| except Exception as e: | |
| print(f"xformers not available (skip): {e}") | |
| return pipe | |
| def try_set_lora_scale(pipe, lora_scale: float) -> bool: | |
| """ | |
| diffusers ๋ฒ์ ์ ๋ฐ๋ผ set_adapters ์ง์ ์ฌ๋ถ๊ฐ ๋ฌ๋ผ์ ๋ฐฉ์ด์ ์ผ๋ก ์ฒ๋ฆฌ. | |
| """ | |
| try: | |
| pipe.set_adapters(["default"], adapter_weights=[float(lora_scale)]) | |
| return True | |
| except Exception: | |
| return False | |
| def main(): | |
| # ===== ์ฌ์ฉ์ ์ค์ ===== | |
| trigger_word = "shinkai_makoto_style" | |
| base_model_id = "runwayml/stable-diffusion-v1-5" | |
| # ์ต์ข LoRA ํด๋ (ํ์ ์ checkpoint-1500์ผ๋ก ๋ฐ๊ฟ๋ ๋จ) | |
| lora_model_path = "./shinkai_lora_output" | |
| # ๊ฒฐ๊ณผ ๊ฐ์ ํต์ฌ: LoRA ๊ฐ๋ ๋ฎ์ถ๊ธฐ | |
| lora_scales = [0.65, 0.75, 0.85] | |
| # guidance๋ ๋ฎ์ถ ์ชฝ์ด ํํ ์์ ์ ์ ๋ฆฌํ ๊ฒฝ์ฐ๊ฐ ๋ง์ | |
| guidance_scales = [6.5, 7.0] | |
| # ๋ํ ์ผ์ ์ํด steps ์ด์ง ๋๋ฆผ | |
| num_inference_steps = 45 | |
| # ์ฌํ์ฑ | |
| seed = 1337 | |
| # T4๋ฉด False ์ถ์ฒ(์์ ), L4/A100์ด๋ฉด True๋ OK | |
| use_xformers = False | |
| # ===== ํ๊ฒฝ ํ๋กฌํํธ 5๊ฐ ์ธํธ ===== | |
| prompts = [ | |
| ( | |
| "rainy_neon_city", | |
| f"{trigger_word}, a rainy city street at dusk, neon lights reflecting on wet asphalt, " | |
| "small girl silhouette holding a transparent umbrella, wide shot, cinematic composition, " | |
| "soft lighting, detailed background, anime illustration", | |
| ), | |
| ( | |
| "sunset_cityscape", | |
| f"{trigger_word}, a vast cityscape under a dramatic sunset sky, glowing clouds, " | |
| "warm orange and pink tones, tiny human figure on a rooftop looking at the sky, " | |
| "wide angle, atmospheric perspective, anime background art", | |
| ), | |
| ( | |
| "starry_town_night", | |
| f"{trigger_word}, a quiet town under a starry night sky, bright stars, soft clouds drifting, " | |
| "cinematic lighting, calm mood, highly detailed sky, anime background illustration", | |
| ), | |
| ( | |
| "railway_rain_perspective", | |
| f"{trigger_word}, a railway stretching into the distance during rainfall, wet rails reflecting city lights, " | |
| "empty platform, deep perspective, moody atmosphere, anime cinematic background", | |
| ), | |
| ( | |
| "rural_after_rain", | |
| f"{trigger_word}, a quiet rural town with fields and houses, dramatic cloudy sky after rain, " | |
| "soft sunlight breaking through clouds, wide shot, peaceful mood, anime background art", | |
| ), | |
| ] | |
| negative_prompt = ( | |
| "lowres, bad anatomy, bad hands, extra fingers, missing fingers, " | |
| "deformed face, cross-eye, distorted, ugly, blurry, noisy, watermark, text" | |
| ) | |
| # ===== ์ฒดํฌ ===== | |
| if not os.path.exists(lora_model_path): | |
| raise FileNotFoundError(f"LoRA ๊ฒฝ๋ก๊ฐ ์์ต๋๋ค: {lora_model_path}") | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| dtype = torch.float16 if device == "cuda" else torch.float32 | |
| # ===== ํ์ดํ๋ผ์ธ ๋ก๋(1๋ฒ๋ง) ===== | |
| print("Loading base model:", base_model_id) | |
| pipe = build_pipe( | |
| base_model_id=base_model_id, | |
| dtype=dtype, | |
| device=device, | |
| use_xformers=use_xformers, | |
| ) | |
| # LoRA ๋ก๋(1๋ฒ๋ง) | |
| print("Loading LoRA from:", lora_model_path) | |
| pipe.load_lora_weights(lora_model_path) | |
| # ===== ์ถ๋ ฅ ํด๋ ===== | |
| out_dir = "inference_outputs_landscape" | |
| os.makedirs(out_dir, exist_ok=True) | |
| ts = time.strftime("%Y%m%d_%H%M%S") | |
| print(f"Output dir: {out_dir}") | |
| print(f"Steps={num_inference_steps}, seed={seed}, lora_scales={lora_scales}, guidance_scales={guidance_scales}") | |
| print("Negative prompt:", negative_prompt) | |
| # ===== ์์ฑ ===== | |
| total = len(prompts) * len(lora_scales) * len(guidance_scales) | |
| done = 0 | |
| for lora_scale in lora_scales: | |
| supported = try_set_lora_scale(pipe, lora_scale) | |
| if supported: | |
| print(f"\nโ LoRA scale set ({lora_scale})") | |
| else: | |
| print(f"\nโ ๏ธ set_adapters not supported; using default LoRA strength (requested {lora_scale})") | |
| for gs in guidance_scales: | |
| for name, prompt in prompts: | |
| done += 1 | |
| gen = torch.Generator(device=device).manual_seed(seed) | |
| fname = ( | |
| f"{out_dir}/" | |
| f"{ts}_p{name}_lora{lora_scale}_gs{gs}_steps{num_inference_steps}_seed{seed}.png" | |
| ) | |
| print(f"[{done}/{total}] -> {fname}") | |
| if device == "cuda": | |
| with torch.autocast("cuda", dtype=dtype): | |
| image = pipe( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| num_inference_steps=num_inference_steps, | |
| guidance_scale=float(gs), | |
| generator=gen, | |
| ).images[0] | |
| else: | |
| with torch.no_grad(): | |
| image = pipe( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| num_inference_steps=num_inference_steps, | |
| guidance_scale=float(gs), | |
| ).images[0] | |
| image.save(fname) | |
| print("\nโ All done!") | |
| print(f"Check: {out_dir}") | |
| if __name__ == "__main__": | |
| main() | |