manvis's picture
app.py
a43556b verified
import gradio as gr
from PIL import Image
import torch
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
from controlnet_aux import OpenposeDetector
import numpy as np
# Load ControlNet and SD model
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-openpose", torch_dtype=torch.float16
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
torch_dtype=torch.float16,
).to("cuda")
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_xformers_memory_efficient_attention()
pipe.enable_model_cpu_offload()
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
def skin_swap(minecraft_img, skin_texture):
# 1. Generate pose from Minecraft screenshot
pose = openpose(minecraft_img)
# 2. Generate the prompt dynamically
prompt = ("Replace the Minecraft character in the image using the second image "
"as a Minecraft skin texture. Match the pose and body proportions. "
"Keep the environment, lighting, and blocky style the same.")
# 3. Run image generation
result = pipe(prompt, image=pose, num_inference_steps=30, guidance_scale=7.5).images[0]
return result