|
|
|
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image, AutoPipelineForInpainting |
|
from diffusers.utils import load_image |
|
from pathlib import Path |
|
import torch |
|
import numpy as np |
|
import requests |
|
from io import BytesIO |
|
from PIL import Image |
|
from huggingface_hub import HfApi |
|
import os |
|
|
|
api = HfApi() |
|
|
|
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg" |
|
response = requests.get(url) |
|
original_image = Image.open(BytesIO(response.content)).convert("RGB") |
|
original_image = original_image.resize((768, 512)) |
|
|
|
original_image = load_image( |
|
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" |
|
) |
|
|
|
mask = np.ones((768, 768), dtype=np.float32) |
|
|
|
mask[:250, 250:-250] = 0 |
|
|
|
|
|
|
|
pipe = AutoPipelineForInpainting.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16) |
|
|
|
|
|
|
|
|
|
pipe.enable_model_cpu_offload() |
|
|
|
prompt = "A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k" |
|
negative_prompt = "" |
|
|
|
prompt = "A fantasy landscape, Cinematic lighting" |
|
prompt = "a hat" |
|
negative_prompt = "low quality, bad quality" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generator = torch.Generator(device="cpu").manual_seed(0) |
|
|
|
|
|
images = pipe(prompt=prompt, image=original_image, mask_image=mask, generator=generator, num_images_per_prompt=1, num_inference_steps=25).images |
|
|
|
for i, image in enumerate(images): |
|
file_name = f"bb_1_{i}" |
|
path = os.path.join(Path.home(), "images", f"{file_name}.png") |
|
image.save(path) |
|
|
|
api.upload_file( |
|
path_or_fileobj=path, |
|
path_in_repo=path.split("/")[-1], |
|
repo_id="patrickvonplaten/images", |
|
repo_type="dataset", |
|
) |
|
print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png") |
|
|