import PIL.Image
from huggingface_hub import hf_hub_download
photomaker_path = hf_hub_download(repo_id="TencentARC/PhotoMaker", filename="photomaker-v1.bin", repo_type="model")
import sys
import gradio as gr
import torch
import os
from diffusers.utils import load_image
from diffusers import EulerDiscreteScheduler
from photomaker import PhotoMakerStableDiffusionXLPipeline
base_model_path = 'SG161222/RealVisXL_V3.0'
try:
if torch.cuda.is_available():
device = "cuda"
elif sys.platform == "darwin" and torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
except:
device = "cpu"
Load base model
pipe = PhotoMakerStableDiffusionXLPipeline.from_pretrained(
base_model_path, # can change to any base model based on SDXL
torch_dtype=torch.bfloat16,
use_safetensors=True,
variant="fp16"
).to(device)
Load PhotoMaker checkpoint
pipe.load_photomaker_adapter(
os.path.dirname(photomaker_path),
subfolder="",
weight_name=os.path.basename(photomaker_path),
trigger_word="img" # define the trigger word
)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
Also can cooperate with other LoRA modules
pipe.load_lora_weights(os.path.dirname(lora_path), weight_name=lora_model_name, adapter_name="xl_more_art-full")
pipe.set_adapters(["photomaker", "xl_more_art-full"], adapter_weights=[1.0, 0.5])
pipe.fuse_lora()
define the input ID images
#input_folder_name = './input'
#image_basename_list = os.listdir(input_folder_name)
#image_path_list = sorted([os.path.join(input_folder_name, basename) for basename in image_basename_list])
def generate_img(img):
print(type(img))
print("START PREDICTDING!")
#print(img)
input_id_images = []
input_id_images.append(load_image(img))
# Note that the trigger word img must follow the class word for personalization
prompt = "a half-body portrait of a man img wearing the sunglasses in Iron man suit, best quality"
negative_prompt = "(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth, grayscale"
generator = torch.Generator(device=device).manual_seed(42)
images = pipe(
prompt=prompt,
input_id_images=input_id_images,
negative_prompt=negative_prompt,
num_images_per_prompt=1,
num_inference_steps=1,
start_merge_step=10,
generator=generator,
).images[0]
return images
text_generation_interface = gr.Interface(
fn=generate_img,
inputs=[
gr.Image(label="paste image", type="pil"),
],
outputs=gr.Image(label="Generated IMAGE"),
title="Falcon-7B Instruct",
).launch()