add image_as_payload_to_embeddings
Browse files
app.py
CHANGED
@@ -9,13 +9,13 @@ import math
|
|
9 |
# from transformers import CLIPTextModel, CLIPTokenizer
|
10 |
import os
|
11 |
|
12 |
-
|
13 |
# clip_model_id = "openai/clip-vit-large-patch14-336"
|
14 |
# clip_retrieval_indice_name, clip_model_id ="laion5B-L-14", "/laion/CLIP-ViT-L-14-laion2B-s32B-b82K"
|
15 |
clip_retrieval_service_url = "https://knn.laion.ai/knn-service"
|
16 |
# available models = ['RN50', 'RN101', 'RN50x4', 'RN50x16', 'RN50x64', 'ViT-B/32', 'ViT-B/16', 'ViT-L/14', 'ViT-L/14@336px']
|
17 |
# clip_model="ViT-B/32"
|
18 |
clip_model="ViT-L/14"
|
|
|
19 |
clip_model_id ="laion5B-L-14"
|
20 |
|
21 |
|
@@ -35,6 +35,62 @@ def debug_print(*args, **kwargs):
|
|
35 |
if debug_print_on:
|
36 |
print(*args, **kwargs)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
def image_to_embedding(input_im):
|
39 |
# debug_print("image_to_embedding")
|
40 |
input_im = Image.fromarray(input_im)
|
@@ -181,6 +237,16 @@ def on_image_load_update_embeddings(image_data):
|
|
181 |
# return gr.Text.update(embeddings_b64)
|
182 |
return embeddings_b64
|
183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
def on_prompt_change_update_embeddings(prompt):
|
185 |
debug_print("on_prompt_change_update_embeddings")
|
186 |
# prompt to embeddings
|
@@ -578,6 +644,23 @@ UI elements to mock out the API
|
|
578 |
_output = gr.Textbox(value="", lines=2, label="Output")
|
579 |
_btn = gr.Button(value="Submit")
|
580 |
_btn.click(on_image_load_update_embeddings, inputs=_input, outputs=[_output], api_name="image_to_embeddings")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
581 |
|
582 |
# ![Alt Text](file/pup1.jpg)
|
583 |
|
|
|
9 |
# from transformers import CLIPTextModel, CLIPTokenizer
|
10 |
import os
|
11 |
|
|
|
12 |
# clip_model_id = "openai/clip-vit-large-patch14-336"
|
13 |
# clip_retrieval_indice_name, clip_model_id ="laion5B-L-14", "/laion/CLIP-ViT-L-14-laion2B-s32B-b82K"
|
14 |
clip_retrieval_service_url = "https://knn.laion.ai/knn-service"
|
15 |
# available models = ['RN50', 'RN101', 'RN50x4', 'RN50x16', 'RN50x64', 'ViT-B/32', 'ViT-B/16', 'ViT-L/14', 'ViT-L/14@336px']
|
16 |
# clip_model="ViT-B/32"
|
17 |
clip_model="ViT-L/14"
|
18 |
+
clip_image_size = 224
|
19 |
clip_model_id ="laion5B-L-14"
|
20 |
|
21 |
|
|
|
35 |
if debug_print_on:
|
36 |
print(*args, **kwargs)
|
37 |
|
38 |
+
# support sending images as base64
|
39 |
+
|
40 |
+
def encode_numpy_array(image_np):
|
41 |
+
import base64
|
42 |
+
import json
|
43 |
+
# Flatten the numpy array and convert it to bytes
|
44 |
+
image_bytes = image_np.tobytes()
|
45 |
+
|
46 |
+
# Encode the byte data as base64
|
47 |
+
encoded_image = base64.b64encode(image_bytes).decode()
|
48 |
+
payload = {
|
49 |
+
"encoded_image": encoded_image,
|
50 |
+
"width": image_np.shape[1],
|
51 |
+
"height": image_np.shape[0],
|
52 |
+
"channels": image_np.shape[2],
|
53 |
+
}
|
54 |
+
payload_json = json.dumps(payload)
|
55 |
+
return payload_json
|
56 |
+
|
57 |
+
def decode_numpy_array(payload):
|
58 |
+
import base64
|
59 |
+
import json
|
60 |
+
payload_json = json.loads(payload)
|
61 |
+
# payload_json = payload.json()
|
62 |
+
encoded_image = payload_json["encoded_image"]
|
63 |
+
width = payload_json["width"]
|
64 |
+
height = payload_json["height"]
|
65 |
+
channels = payload_json["channels"]
|
66 |
+
# Decode the base64 data
|
67 |
+
decoded_image = base64.b64decode(encoded_image)
|
68 |
+
|
69 |
+
# Convert the byte data back to a NumPy array
|
70 |
+
image_np = np.frombuffer(decoded_image, dtype=np.uint8).reshape(height, width, channels)
|
71 |
+
|
72 |
+
return image_np
|
73 |
+
|
74 |
+
|
75 |
+
def preprocess_image(image_np, max_size=224):
|
76 |
+
from torchvision.transforms import Compose, Resize, CenterCrop
|
77 |
+
# Convert the numpy array to a PIL image
|
78 |
+
image = Image.fromarray(image_np)
|
79 |
+
|
80 |
+
# Define the transformation pipeline
|
81 |
+
transforms = Compose([
|
82 |
+
Resize(max_size, interpolation=Image.BICUBIC),
|
83 |
+
CenterCrop(max_size),
|
84 |
+
])
|
85 |
+
|
86 |
+
# Apply the transformations to the image
|
87 |
+
image = transforms(image)
|
88 |
+
|
89 |
+
# Convert the PIL image back to a numpy array
|
90 |
+
image_np = np.array(image)
|
91 |
+
|
92 |
+
return image_np
|
93 |
+
|
94 |
def image_to_embedding(input_im):
|
95 |
# debug_print("image_to_embedding")
|
96 |
input_im = Image.fromarray(input_im)
|
|
|
237 |
# return gr.Text.update(embeddings_b64)
|
238 |
return embeddings_b64
|
239 |
|
240 |
+
def on_image_as_payload_update_embeddings(payload):
|
241 |
+
debug_print("on_image_as_payload_update_embeddings")
|
242 |
+
if payload is None or payload == "":
|
243 |
+
return ''
|
244 |
+
image_data = decode_numpy_array(payload)
|
245 |
+
embeddings = image_to_embedding(image_data)
|
246 |
+
embeddings_b64 = embedding_to_base64(embeddings)
|
247 |
+
# return gr.Text.update(embeddings_b64)
|
248 |
+
return embeddings_b64
|
249 |
+
|
250 |
def on_prompt_change_update_embeddings(prompt):
|
251 |
debug_print("on_prompt_change_update_embeddings")
|
252 |
# prompt to embeddings
|
|
|
644 |
_output = gr.Textbox(value="", lines=2, label="Output")
|
645 |
_btn = gr.Button(value="Submit")
|
646 |
_btn.click(on_image_load_update_embeddings, inputs=_input, outputs=[_output], api_name="image_to_embeddings")
|
647 |
+
with gr.Row():
|
648 |
+
def _on_image_load(input_image):
|
649 |
+
debug_print("_on_image_load")
|
650 |
+
# resize if size is bigger than clip_image_size
|
651 |
+
if input_image.shape[0] > clip_image_size or input_image.shape[1] > clip_image_size:
|
652 |
+
input_image = preprocess_image(input_image, clip_image_size)
|
653 |
+
payload = encode_numpy_array(input_image)
|
654 |
+
return payload
|
655 |
+
|
656 |
+
_input = gr.Image(label="Image Prompt", show_label=True)
|
657 |
+
with gr.Accordion(f"Image (base64)", open=False):
|
658 |
+
_input_as_texct = gr.Textbox(value="", lines=2, label="Output")
|
659 |
+
_input.change(_on_image_load, inputs=_input, outputs=[_input_as_texct])
|
660 |
+
with gr.Accordion(f"Embeddings (base64)", open=False):
|
661 |
+
_output = gr.Textbox(value="", lines=2, label="Output")
|
662 |
+
_btn = gr.Button(value="Submit")
|
663 |
+
_btn.click(on_image_as_payload_update_embeddings, inputs=_input_as_texct, outputs=[_output], api_name="image_as_payload_to_embeddings")
|
664 |
|
665 |
# ![Alt Text](file/pup1.jpg)
|
666 |
|