Spaces:
Sleeping
Sleeping
Update utils.py
Browse files4 unmodified lines56789101112131415161718191 unmodified line2122232425262728293031323334353637383940414243446 unmodified lines5152535455565758596061624 unmodified linesimport torchfrom diffusers import StableDiffusionImg2ImgPipelinefrom PIL import Image# --- Place any download or path setup here ---MODEL_ID ="runwayml/stable-diffusion-v1-5" # Can swap for custom path if using IP-AdapterADAPTER_PATH = "/workspace/.cache/huggingface/ip_adapter/ip-adapter_sd15.bin"ADAPTER_DIR = "/workspace/.cache/huggingface/ip_adapter"DEVICE = "cpu"MODEL_CACHE = "/workspace/.cache/huggingface"# (Optional) Download IP-Adapter weights and patch pipeline if desired1 unmodified linepipe = StableDiffusionImg2ImgPipeline.from_pretrained(MODEL_ID,torch_dtype=torch.float32,cache_dir=MODEL_CACHE,# safety_checker=None, # Disable for demo/testing; enable in prod).to(DEVICE)pipe.load_ip_adapter(pretrained_model_name_or_path_or_dict="h94/IP-Adapter",subfolder="models",weight_name="ip-adapter_sd15.bin")# pipe.load_ip_adapter(# pretrained_model_name_or_path_or_dict=ADAPTER_DIR,# subfolder=".", # The weights file is directly in ADAPTER_DIR# weight_name="ip-adapter_sd15.bin"# # Optionally: subfolder="models" if using the repo, not a direct path# )def generate_sticker(input_image, prompt):"""Given a user image and a prompt, generates a sticker/emoji-style portrait."""6 unmodified lines# ).to(DEVICE)# Preprocess the image (resize, etc)init_image = input_image.convert("RGB").resize((512, 512))# Run inference (low strength for identity preservation)result = pipe(prompt=prompt,image=init_image,strength=0.65,guidance_scale=7.5,num_inference_steps=304 unmodified lines567891011121314151617181920212223242526271 unmodified line2930313233343536373839404142434445464748495051525354556 unmodified lines626364656667686970717273747576777879804 unmodified linesimport torchfrom diffusers import StableDiffusionImg2ImgPipelinefrom transformers import CLIPImageProcessor, CLIPVisionModelWithProjectionfrom PIL import Image# --- Place any download or path setup here --- old# MODEL_ID ="runwayml/stable-diffusion-v1-5" # Can swap for custom path if using IP-Adapter# ADAPTER_PATH = "/workspace/.cache/huggingface/ip_adapter/ip-adapter_sd15.bin"# ADAPTER_DIR = "/workspace/.cache/huggingface/ip_adapter"# DEVICE = "cpu"# MODEL_CACHE = "/workspace/.cache/huggingface"# ---- SETTINGS ----MODEL_ID = "runwayml/stable-diffusion-v1-5"IPADAPTER_REPO = "h94/IP-Adapter"IPADAPTER_WEIGHT_NAME = "ip-adapter_sd15.bin"DEVICE = "cpu" # Change to "cuda" if you have GPUCACHE_DIR = os.environ.get("HF_HOME", "/data/huggingface")# (Optional) Download IP-Adapter weights and patch pipeline if desired1 unmodified linepipe = StableDiffusionImg2ImgPipeline.from_pretrained(MODEL_ID,torch_dtype=torch.float32,cache_dir=CACHE_DIR,# safety_checker=None, # Disable for demo/testing; enable in prod).to(DEVICE)pipe.load_ip_adapter(pretrained_model_name_or_path_or_dict=IPADAPTER_REPO,subfolder="models",weight_name=IPADAPTER_WEIGHT_NAME)# Load vision encoder and processor for IP-Adapter embeddingvision_encoder = CLIPVisionModelWithProjection.from_pretrained(f"{IPADAPTER_REPO}/clip_vision_model",cache_dir=CACHE_DIR,)image_processor = CLIPImageProcessor.from_pretrained(f"{IPADAPTER_REPO}/clip_vision_model",cache_dir=CACHE_DIR,)def generate_sticker(input_image: Image.Image, prompt: str):"""Given a user image and a prompt, generates a sticker/emoji-style portrait."""6 unmodified lines# ).to(DEVICE)# Preprocess the image (resize, etc)face_img = input_image.convert("RGB").resize((224, 224))inputs = image_processor(images=face_img, return_tensors="pt").to(DEVICE)with torch.no_grad():image_embeds = vision_encoder(**inputs).image_embeds# 2. Prepare image for SD pipelineinit_image = input_image.convert("RGB").resize((512, 512))# Run inference (low strength for identity preservation)result = pipe(prompt=prompt,image=init_image,image_embeds=image_embeds,strength=0.65,guidance_scale=7.5,num_inference_steps=30
utils.py+34-16