File size: 5,805 Bytes
d0f7b62
 
 
 
 
f8f262c
d0f7b62
 
 
 
 
 
 
 
 
 
 
337f738
 
 
d0f7b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337f738
 
d0f7b62
 
 
 
 
badd631
d0f7b62
 
 
 
db36750
d0f7b62
 
c336c33
f130269
d0f7b62
 
db36750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0f7b62
db36750
 
 
 
 
 
 
 
d0f7b62
f130269
 
d0f7b62
 
db36750
d0f7b62
 
 
 
 
 
 
 
 
 
 
 
 
c336c33
 
 
 
d0f7b62
 
 
 
 
 
db36750
 
 
 
 
 
 
 
 
 
 
 
d0f7b62
 
 
 
c4baf05
d0f7b62
 
db36750
d0f7b62
06aa73b
d0f7b62
 
 
f816302
db36750
d0f7b62
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import torch
import spaces
from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL
from transformers import AutoFeatureExtractor
from ip_adapter.pipeline_stable_diffusion_extra_cfg import StableDiffusionPipelineCFG
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker

from ip_adapter.ip_adapter_instruct import IPAdapterInstruct
from huggingface_hub import hf_hub_download
import gradio as gr
import cv2

base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
vae_model_path = "stabilityai/sd-vae-ft-mse"
image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
ip_ckpt = hf_hub_download(repo_id="CiaraRowles/IP-Adapter-Instruct", filename="ip-adapter-instruct-sd15.bin", repo_type="model")

safety_model_id = "CompVis/stable-diffusion-safety-checker"
safety_feature_extractor = AutoFeatureExtractor.from_pretrained(safety_model_id)
safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id)

device = "cuda"

noise_scheduler = DDIMScheduler(
    num_train_timesteps=1000,
    beta_start=0.00085,
    beta_end=0.012,
    beta_schedule="scaled_linear",
    clip_sample=False,
    set_alpha_to_one=False,
    steps_offset=1,
)
vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
pipe = StableDiffusionPipelineCFG.from_pretrained(
    base_model_path,
    scheduler=noise_scheduler,
    vae=vae,
    torch_dtype=torch.float16,
    feature_extractor=safety_feature_extractor,
    safety_checker=safety_checker
).to(device)

#pipe.load_lora_weights("h94/IP-Adapter-FaceID", weight_name="ip-adapter-faceid-plusv2_sd15_lora.safetensors")
#pipe.fuse_lora()

ip_model = IPAdapterInstruct(pipe, image_encoder_path, ip_ckpt, device,dtypein=torch.float16,num_tokens=16)

cv2.setNumThreads(1)

@spaces.GPU(enable_queue=True)
def generate_image(images, prompt, negative_prompt,instruct_query, scale, nfaa_negative_prompt, progress=gr.Progress(track_tqdm=True)):
    faceid_all_embeds = []
    first_iteration = True
    image = images
    yield None
    total_negative_prompt = f"{negative_prompt} {nfaa_negative_prompt}"
    print("Generating normal")

    # Calculate aspect ratio
    aspect_ratio = image.width / image.height

    # Set base_size (you can adjust this value as needed)
    base_size = 512

    # Calculate new width and height
    if aspect_ratio > 1:  # Landscape
        new_width = base_size
        new_height = int(base_size / aspect_ratio)
    else:  # Portrait or square
        new_height = base_size
        new_width = int(base_size * aspect_ratio)

    # Ensure dimensions are multiples of 8 (required by some models)
    new_width = (new_width // 8) * 8
    new_height = (new_height // 8) * 8

    image = ip_model.generate(
        prompt=prompt,
        negative_prompt=total_negative_prompt,
        pil_image=image,
        scale=scale,
        width=new_width,
        height=new_height,
        num_inference_steps=30,
        query=instruct_query
    )
    
    yield image



def swap_to_gallery(images):
    return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)

def remove_back_to_files():
    return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
css = '''
h1{margin-bottom: 0 !important}
'''
with gr.Blocks(css=css) as demo:
    gr.Markdown("# IP-Adapter-Instruct demo")
    gr.Markdown("Demo for the [CiaraRowles/IP-Adapter-Instruct model](https://huggingface.co/CiaraRowles/IP-Adapter-Instruct)")
    with gr.Row():
        with gr.Column():
            files = gr.Image(
                label="Input image",
                type="pil"
            )
            uploaded_files = gr.Gallery(label="Your image", visible=False, columns=5, rows=1, height=125)
            with gr.Column(visible=False) as clear_button:
                remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
            prompt = gr.Textbox(label="Prompt",
                       info="Try something like 'a photo of a man/woman/person'",
                       placeholder="A photo of a [man/woman/person]...")
            instruct_query = gr.Dropdown(
                label="Instruct Query",
                choices=[
                    "use everything from the image",
                    "use the style",
                    "use the colour",
                    "use the pose",
                    "use the composition",
                    "use the face"
                ],
                value="use everything from the image"
            )
            negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="low quality")
            submit = gr.Button("Submit")
            with gr.Accordion(open=False, label="Advanced Options"):
                nfaa_negative_prompts = gr.Textbox(label="Appended Negative Prompts", info="Negative prompts to steer generations towards safe for all audiences outputs", value="naked, bikini, skimpy, scanty, bare skin, lingerie, swimsuit, exposed, see-through")    
                scale = gr.Slider(label="Scale", value=0.8, step=0.1, minimum=0, maximum=2)
        with gr.Column():
            gallery = gr.Gallery(label="Generated Images")
        
        submit.click(fn=generate_image,
                    inputs=[files, prompt, negative_prompt,instruct_query, scale, nfaa_negative_prompts],
                    outputs=gallery)
    
    gr.Markdown("This demo includes extra features to mitigate the implicit bias of the model and prevent explicit usage of it to generate content with faces of people, including third parties, that is not safe for all audiences, including naked or semi-naked people.")
    gr.Markdown("based on: https://huggingface.co/spaces/multimodalart/Ip-Adapter-FaceID")

demo.launch()