Instructions to use freshcodes/cat_in_space with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use freshcodes/cat_in_space with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", dtype=torch.bfloat16, device_map="cuda") pipe.load_lora_weights("freshcodes/cat_in_space") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Create handler.py
Browse files- handler.py +47 -0
handler.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# handler.py
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
import torch
|
| 7 |
+
from diffusers import DiffusionPipeline
|
| 8 |
+
|
| 9 |
+
class EndpointHandler:
|
| 10 |
+
def __init__(self, path=""):
|
| 11 |
+
# The default container mounts your repo at /repository
|
| 12 |
+
model_dir = path or "/repository"
|
| 13 |
+
# Load your SDXL pipeline in fp16, no device_map, no offloading
|
| 14 |
+
self.pipe = DiffusionPipeline.from_pretrained(
|
| 15 |
+
model_dir,
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
+
use_safetensors=True,
|
| 18 |
+
).to("cuda")
|
| 19 |
+
self.pipe.set_progress_bar_config(disable=True)
|
| 20 |
+
|
| 21 |
+
def __call__(self, data: dict):
|
| 22 |
+
# Accept either {"inputs": "..."} or {"prompt": "..."} + optional "parameters"
|
| 23 |
+
prompt = data.get("inputs") or data.get("prompt") or ""
|
| 24 |
+
params = data.get("parameters") or {}
|
| 25 |
+
|
| 26 |
+
width = int(params.get("width", 768))
|
| 27 |
+
height = int(params.get("height", 768))
|
| 28 |
+
steps = int(params.get("num_inference_steps", 25))
|
| 29 |
+
guidance = float(params.get("guidance_scale", 7.0))
|
| 30 |
+
negative = params.get("negative_prompt")
|
| 31 |
+
seed = params.get("seed")
|
| 32 |
+
generator = (torch.Generator(device="cuda").manual_seed(int(seed))
|
| 33 |
+
if seed is not None else None)
|
| 34 |
+
|
| 35 |
+
image = self.pipe(
|
| 36 |
+
prompt=prompt,
|
| 37 |
+
negative_prompt=negative,
|
| 38 |
+
width=width,
|
| 39 |
+
height=height,
|
| 40 |
+
num_inference_steps=steps,
|
| 41 |
+
guidance_scale=guidance,
|
| 42 |
+
generator=generator,
|
| 43 |
+
).images[0]
|
| 44 |
+
|
| 45 |
+
buf = io.BytesIO()
|
| 46 |
+
image.save(buf, format="PNG")
|
| 47 |
+
return {"image_base64": base64.b64encode(buf.getvalue()).decode("utf-8")}
|