esx2ve CiaraRowles commited on
Commit
c9bae7e
β€’
1 Parent(s): e38c92a

all files (#1)

Browse files

- all files (fdc77c35cee9a70d4bfa097f5e0e6d232db5df93)


Co-authored-by: Ciara <[email protected]>

Files changed (3) hide show
  1. README.md +4 -4
  2. app.py +142 -0
  3. requirements.txt +9 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: IP Adapter Instruct
3
- emoji: 🐒
4
  colorFrom: purple
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 4.40.0
8
  app_file: app.py
9
  pinned: false
10
- license: apache-2.0
11
  ---
 
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: IP Test
3
+ emoji: 😻
4
  colorFrom: purple
5
+ colorTo: pink
6
  sdk: gradio
7
  sdk_version: 4.40.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
+ based on: https://huggingface.co/spaces/multimodalart/Ip-Adapter-FaceID <3
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import spaces
3
+ from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL
4
+ from transformers import AutoFeatureExtractor
5
+ from ip_adapter.pipeline_stable_diffusion_extra_cfg import StableDiffusionPipelineCFG
6
+ from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
7
+
8
+ from ip_adapter.ip_adapter_instruct import IPAdapterInstruct
9
+ from huggingface_hub import hf_hub_download
10
+ import gradio as gr
11
+ import cv2
12
+
13
+ base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
14
+ vae_model_path = "stabilityai/sd-vae-ft-mse"
15
+ image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
16
+ ip_ckpt = hf_hub_download(repo_id="CiaraRowles/IP-Adapter-Instruct", filename="ip-adapter-instruct-sd15.bin", repo_type="model")
17
+
18
+ safety_model_id = "CompVis/stable-diffusion-safety-checker"
19
+ safety_feature_extractor = AutoFeatureExtractor.from_pretrained(safety_model_id)
20
+ safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id)
21
+
22
+ device = "cuda"
23
+
24
+ noise_scheduler = DDIMScheduler(
25
+ num_train_timesteps=1000,
26
+ beta_start=0.00085,
27
+ beta_end=0.012,
28
+ beta_schedule="scaled_linear",
29
+ clip_sample=False,
30
+ set_alpha_to_one=False,
31
+ steps_offset=1,
32
+ )
33
+ vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
34
+ pipe = StableDiffusionPipelineCFG.from_pretrained(
35
+ base_model_path,
36
+ scheduler=noise_scheduler,
37
+ vae=vae,
38
+ torch_dtype=torch.float16,
39
+ feature_extractor=safety_feature_extractor,
40
+ safety_checker=safety_checker
41
+ ).to(device)
42
+
43
+ #pipe.load_lora_weights("h94/IP-Adapter-FaceID", weight_name="ip-adapter-faceid-plusv2_sd15_lora.safetensors")
44
+ #pipe.fuse_lora()
45
+
46
+ ip_model = IPAdapterInstruct(pipe, image_encoder_path, ip_ckpt, device,dtypein=torch.float16,num_tokens=16)
47
+
48
+ cv2.setNumThreads(1)
49
+
50
+ @spaces.GPU(enable_queue=True)
51
+ def generate_image(images, prompt, negative_prompt,instruct_query, scale, nfaa_negative_prompt, progress=gr.Progress(track_tqdm=True)):
52
+ faceid_all_embeds = []
53
+ first_iteration = True
54
+ image = images
55
+ yield None
56
+ total_negative_prompt = f"{negative_prompt} {nfaa_negative_prompt}"
57
+ print("Generating normal")
58
+
59
+ # Calculate aspect ratio
60
+ aspect_ratio = image.width / image.height
61
+
62
+ # Set base_size (you can adjust this value as needed)
63
+ base_size = 512
64
+
65
+ # Calculate new width and height
66
+ if aspect_ratio > 1: # Landscape
67
+ new_width = base_size
68
+ new_height = int(base_size / aspect_ratio)
69
+ else: # Portrait or square
70
+ new_height = base_size
71
+ new_width = int(base_size * aspect_ratio)
72
+
73
+ # Ensure dimensions are multiples of 8 (required by some models)
74
+ new_width = (new_width // 8) * 8
75
+ new_height = (new_height // 8) * 8
76
+
77
+ image = ip_model.generate(
78
+ prompt=prompt,
79
+ negative_prompt=total_negative_prompt,
80
+ pil_image=image,
81
+ scale=scale,
82
+ width=new_width,
83
+ height=new_height,
84
+ num_inference_steps=30,
85
+ query=instruct_query
86
+ )
87
+
88
+ yield image
89
+
90
+
91
+
92
+ def swap_to_gallery(images):
93
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
94
+
95
+ def remove_back_to_files():
96
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
97
+ css = '''
98
+ h1{margin-bottom: 0 !important}
99
+ '''
100
+ with gr.Blocks(css=css) as demo:
101
+ gr.Markdown("# IP-Adapter-Instruct demo")
102
+ gr.Markdown("Demo for the [CiaraRowles/IP-Adapter-Instruct model](https://huggingface.co/CiaraRowles/IP-Adapter-Instruct)")
103
+ with gr.Row():
104
+ with gr.Column():
105
+ files = gr.Image(
106
+ label="Input image",
107
+ type="pil"
108
+ )
109
+ uploaded_files = gr.Gallery(label="Your image", visible=False, columns=5, rows=1, height=125)
110
+ with gr.Column(visible=False) as clear_button:
111
+ remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
112
+ prompt = gr.Textbox(label="Prompt",
113
+ info="Try something like 'a photo of a man/woman/person'",
114
+ placeholder="A photo of a [man/woman/person]...")
115
+ instruct_query = gr.Dropdown(
116
+ label="Instruct Query",
117
+ choices=[
118
+ "use everything from the image",
119
+ "use the style",
120
+ "use the colour",
121
+ "use the pose",
122
+ "use the composition",
123
+ "use the face"
124
+ ],
125
+ value="use everything from the image"
126
+ )
127
+ negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="low quality")
128
+ submit = gr.Button("Submit")
129
+ with gr.Accordion(open=False, label="Advanced Options"):
130
+ 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")
131
+ scale = gr.Slider(label="Scale", value=0.8, step=0.1, minimum=0, maximum=2)
132
+ with gr.Column():
133
+ gallery = gr.Gallery(label="Generated Images")
134
+
135
+ submit.click(fn=generate_image,
136
+ inputs=[files, prompt, negative_prompt,instruct_query, scale, nfaa_negative_prompts],
137
+ outputs=gallery)
138
+
139
+ 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.")
140
+ gr.Markdown("based on: https://huggingface.co/spaces/multimodalart/Ip-Adapter-FaceID")
141
+
142
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ diffusers==0.29.2
2
+ transformers
3
+ accelerate
4
+ safetensors
5
+ einops
6
+ onnxruntime-gpu
7
+ spaces==0.19.4
8
+ opencv-python
9
+ git+https://github.com/unity-research/IP-Adapter-Instruct.git