linoyts HF Staff commited on
Commit
62151c8
·
verified ·
1 Parent(s): 29b1411

Update app.py

Browse files

initial functionality

Files changed (1) hide show
  1. app.py +41 -39
app.py CHANGED
@@ -30,7 +30,7 @@ ip_model = IPAdapterXL(pipe, image_encoder_repo, image_encoder_subfolder, ip_ckp
30
  # Initialize CLIP model
31
  clip_model, _, preprocess = open_clip.create_model_and_transforms('hf-hub:laion/CLIP-ViT-H-14-laion2B-s32B-b79K')
32
  clip_model.to(device)
33
- print("Models initialized successfully!")
34
 
35
  def get_image_embeds(pil_image, model=clip_model, preproc=preprocess, dev=device):
36
  """Get CLIP image embeddings for a given PIL image"""
@@ -41,9 +41,9 @@ def get_image_embeds(pil_image, model=clip_model, preproc=preprocess, dev=device
41
 
42
  def process_images(
43
  base_image,
44
- concept_image1, concept_desc1,
45
- concept_image2=None, concept_desc2=None,
46
- concept_image3=None, concept_desc3=None,
47
  rank1=10, rank2=10, rank3=10,
48
  prompt=None,
49
  scale=1.0,
@@ -52,28 +52,28 @@ def process_images(
52
  """Process the base image and concept images to generate modified images"""
53
  # Process base image
54
  base_image_pil = Image.fromarray(base_image).convert("RGB")
55
- base_embed = get_image_embeds(base_image_pil)
56
 
57
  # Process concept images
58
  concept_images = []
59
  concept_descriptions = []
60
 
61
- # Add first concept (required)
62
  if concept_image1 is not None:
63
  concept_images.append(concept_image1)
64
- concept_descriptions.append(concept_desc1 if concept_desc1 else "Concept 1")
65
  else:
66
  return None, "Please upload at least one concept image"
67
 
68
  # Add second concept (optional)
69
  if concept_image2 is not None:
70
  concept_images.append(concept_image2)
71
- concept_descriptions.append(concept_desc2 if concept_desc2 else "Concept 2")
72
 
73
  # Add third concept (optional)
74
  if concept_image3 is not None:
75
  concept_images.append(concept_image3)
76
- concept_descriptions.append(concept_desc3 if concept_desc3 else "Concept 3")
77
 
78
  # Get all ranks
79
  ranks = [rank1]
@@ -81,21 +81,23 @@ def process_images(
81
  ranks.append(rank2)
82
  if concept_image3 is not None:
83
  ranks.append(rank3)
 
84
 
85
  concept_embeds = []
86
- for img in concept_images:
87
- if img is not None:
88
- img_pil = Image.fromarray(img).convert("RGB")
89
- concept_embeds.append(get_image_embeds(img_pil))
90
-
91
- # Compute projection matrices
92
  projection_matrices = []
93
- for i, embed in enumerate(concept_embeds):
94
- # For a single image, we need to reshape to have the same format as a collection
95
- single_embed = embed.reshape(1, *embed.shape)
96
- projection_matrix = compute_dataset_embeds_svd(single_embed, ranks[i])
 
 
 
 
 
 
97
  projection_matrices.append(projection_matrix)
98
 
 
99
  # Create projection data structure for the composition
100
  projections_data = [
101
  {
@@ -116,14 +118,14 @@ def process_images(
116
  seed=seed
117
  )
118
 
119
- return modified_images
120
 
121
  def process_and_display(
122
  base_image,
123
- concept_image1, concept_desc1,
124
- concept_image2=None, concept_desc2=None,
125
- concept_image3=None, concept_desc3=None,
126
- rank1=10, rank2=10, rank3=10,
127
  prompt=None, scale=1.0, seed=420
128
  ):
129
  """Wrapper for process_images that handles UI updates"""
@@ -135,9 +137,9 @@ def process_and_display(
135
 
136
  modified_images = process_images(
137
  base_image,
138
- concept_image1, concept_desc1,
139
- concept_image2, concept_desc2,
140
- concept_image3, concept_desc3,
141
  rank1, rank2, rank3,
142
  prompt, scale, seed
143
  )
@@ -159,23 +161,23 @@ with gr.Blocks(title="Image Concept Composition") as demo:
159
  with gr.Row():
160
  with gr.Column(scale=2):
161
  concept_image1 = gr.Image(label="Concept Image 1 (Required)", type="numpy")
162
- with gr.Column(scale=1):
163
- concept_desc1 = gr.Textbox(label="Concept 1 Description", placeholder="Describe this concept")
164
- rank1 = gr.Slider(minimum=1, maximum=50, value=10, step=1, label="Rank 1")
165
 
166
  with gr.Row():
167
  with gr.Column(scale=2):
168
  concept_image2 = gr.Image(label="Concept Image 2 (Optional)", type="numpy")
169
- with gr.Column(scale=1):
170
- concept_desc2 = gr.Textbox(label="Concept 2 Description", placeholder="Describe this concept")
171
- rank2 = gr.Slider(minimum=1, maximum=50, value=10, step=1, label="Rank 2")
172
 
173
  with gr.Row():
174
  with gr.Column(scale=2):
175
  concept_image3 = gr.Image(label="Concept Image 3 (Optional)", type="numpy")
176
- with gr.Column(scale=1):
177
- concept_desc3 = gr.Textbox(label="Concept 3 Description", placeholder="Describe this concept")
178
- rank3 = gr.Slider(minimum=1, maximum=50, value=10, step=1, label="Rank 3")
179
 
180
  prompt = gr.Textbox(label="Guidance Prompt (Optional)", placeholder="Optional text prompt to guide generation")
181
 
@@ -192,9 +194,9 @@ with gr.Blocks(title="Image Concept Composition") as demo:
192
  fn=process_and_display,
193
  inputs=[
194
  base_image,
195
- concept_image1, concept_desc1,
196
- concept_image2, concept_desc2,
197
- concept_image3, concept_desc3,
198
  rank1, rank2, rank3,
199
  prompt, scale, seed
200
  ],
 
30
  # Initialize CLIP model
31
  clip_model, _, preprocess = open_clip.create_model_and_transforms('hf-hub:laion/CLIP-ViT-H-14-laion2B-s32B-b79K')
32
  clip_model.to(device)
33
+ # print("Models initialized successfully!")
34
 
35
  def get_image_embeds(pil_image, model=clip_model, preproc=preprocess, dev=device):
36
  """Get CLIP image embeddings for a given PIL image"""
 
41
 
42
  def process_images(
43
  base_image,
44
+ concept_image1, concept_name1,
45
+ concept_image2=None, concept_name2=None,
46
+ concept_image3=None, concept_name3=None,
47
  rank1=10, rank2=10, rank3=10,
48
  prompt=None,
49
  scale=1.0,
 
52
  """Process the base image and concept images to generate modified images"""
53
  # Process base image
54
  base_image_pil = Image.fromarray(base_image).convert("RGB")
55
+ base_embed = get_image_embeds(base_image_pil, clip_model, preprocess, device)
56
 
57
  # Process concept images
58
  concept_images = []
59
  concept_descriptions = []
60
 
61
+ # for demo purposes we allow for up to 3 different concepts and corresponding concept images
62
  if concept_image1 is not None:
63
  concept_images.append(concept_image1)
64
+ concept_descriptions.append(concept_name1)
65
  else:
66
  return None, "Please upload at least one concept image"
67
 
68
  # Add second concept (optional)
69
  if concept_image2 is not None:
70
  concept_images.append(concept_image2)
71
+ concept_descriptions.append(concept_name2)
72
 
73
  # Add third concept (optional)
74
  if concept_image3 is not None:
75
  concept_images.append(concept_image3)
76
+ concept_descriptions.append(concept_name3)
77
 
78
  # Get all ranks
79
  ranks = [rank1]
 
81
  ranks.append(rank2)
82
  if concept_image3 is not None:
83
  ranks.append(rank3)
84
+
85
 
86
  concept_embeds = []
 
 
 
 
 
 
87
  projection_matrices = []
88
+ # for the demo, we assume 1 concept image per concept
89
+ # for each concept image, we calculate it's image embeedings and load the concepts textual embeddings to copmpute the projection matrix over it
90
+ for i, concept_name in enumerate(concept_descriptions):
91
+ img_pil = Image.fromarray(concept_images[i]).convert("RGB")
92
+ concept_embeds.append(get_image_embeds(img_pil, clip_model, preprocess, device))
93
+ embeds_path = f"./IP_Composer/text_embeddings/{concept_name}_descriptions.npy"
94
+ with open(embeds_path, "rb") as f:
95
+ all_embeds_in = np.load(f)
96
+
97
+ projection_matrix = compute_dataset_embeds_svd(all_embeds_in, ranks[i])
98
  projection_matrices.append(projection_matrix)
99
 
100
+
101
  # Create projection data structure for the composition
102
  projections_data = [
103
  {
 
118
  seed=seed
119
  )
120
 
121
+ return modified_images[0]
122
 
123
  def process_and_display(
124
  base_image,
125
+ concept_image1, concept_name1="age",
126
+ concept_image2=None, concept_name2=None,
127
+ concept_image3=None, concept_name3=None,
128
+ rank1=30, rank2=30, rank3=30,
129
  prompt=None, scale=1.0, seed=420
130
  ):
131
  """Wrapper for process_images that handles UI updates"""
 
137
 
138
  modified_images = process_images(
139
  base_image,
140
+ concept_image1, concept_name1,
141
+ concept_image2, concept_name2,
142
+ concept_image3, concept_name3,
143
  rank1, rank2, rank3,
144
  prompt, scale, seed
145
  )
 
161
  with gr.Row():
162
  with gr.Column(scale=2):
163
  concept_image1 = gr.Image(label="Concept Image 1 (Required)", type="numpy")
164
+ with gr.Row():
165
+ concept_name1 = gr.Textbox(label="Concept 1 Description", placeholder="Describe this concept")
166
+ rank1 = gr.Slider(minimum=1, maximum=50, value=30, step=1, label="Rank 1")
167
 
168
  with gr.Row():
169
  with gr.Column(scale=2):
170
  concept_image2 = gr.Image(label="Concept Image 2 (Optional)", type="numpy")
171
+ with gr.Row():
172
+ concept_name2 = gr.Textbox(label="Concept 2 Description", placeholder="Describe this concept")
173
+ rank2 = gr.Slider(minimum=1, maximum=50, value=30, step=1, label="Rank 2")
174
 
175
  with gr.Row():
176
  with gr.Column(scale=2):
177
  concept_image3 = gr.Image(label="Concept Image 3 (Optional)", type="numpy")
178
+ with gr.Row():
179
+ concept_name3 = gr.Textbox(label="Concept 3 Description", placeholder="Describe this concept")
180
+ rank3 = gr.Slider(minimum=1, maximum=50, value=30, step=1, label="Rank 3")
181
 
182
  prompt = gr.Textbox(label="Guidance Prompt (Optional)", placeholder="Optional text prompt to guide generation")
183
 
 
194
  fn=process_and_display,
195
  inputs=[
196
  base_image,
197
+ concept_image1, concept_name1,
198
+ concept_image2, concept_name2,
199
+ concept_image3, concept_name3,
200
  rank1, rank2, rank3,
201
  prompt, scale, seed
202
  ],