InstaSoyjak / app.py
hysts's picture
hysts HF staff
Fix
28ed681
raw
history blame
No virus
2.35 kB
import gradio as gr
import torch
from PIL import Image
from diffusers import AutoPipelineForText2Image, DDIMScheduler
from transformers import CLIPVisionModelWithProjection
import numpy as np
import spaces
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
"h94/IP-Adapter",
subfolder="models/image_encoder",
torch_dtype=torch.float16,
)
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16,
image_encoder=image_encoder,
)
pipeline.scheduler = DDIMScheduler.from_config(pipeline.scheduler.config)
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name=["ip-adapter-plus_sdxl_vit-h.safetensors", "ip-adapter-plus-face_sdxl_vit-h.safetensors"])
pipeline.set_ip_adapter_scale([0.7, 0.5])
pipeline.enable_model_cpu_offload()
@spaces.GPU
def transform_image(face_image):
generator = torch.Generator(device="cpu").manual_seed(0)
# Check if the input is already a PIL Image
if isinstance(face_image, Image.Image):
processed_face_image = face_image
# If the input is a NumPy array, convert it to a PIL Image
elif isinstance(face_image, np.ndarray):
processed_face_image = Image.fromarray(face_image)
else:
raise ValueError("Unsupported image format")
# Load the style image from the local path
style_image_path = "examples/soyjak2.jpg"
style_image = Image.open(style_image_path)
# Perform the transformation
image = pipeline(
prompt="soyjak",
ip_adapter_image=[style_image, processed_face_image],
negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality",
num_inference_steps=30,
generator=generator,
).images[0]
return image
# Gradio interface setup
demo = gr.Interface(
fn=transform_image,
inputs=gr.Image(label="Upload your face image"),
outputs=gr.Image(label="Your Soyjak"),
title="InstaSoyjak - turn anyone into a Soyjak",
description="All you need to do is upload an image. Please use responsibly. Please follow me on Twitter if you like this space: https://twitter.com/angrypenguinPNG. Idea from Yacine, please give him a follow: https://twitter.com/yacineMTB.",
)
demo.queue(max_size=20) # Configures the queue with a maximum size of 20
demo.launch()