Update app.py
Browse files
app.py
CHANGED
|
@@ -45,16 +45,19 @@ collection_name = 'celeb_images'
|
|
| 45 |
celeb_images = client[db_name][collection_name]
|
| 46 |
|
| 47 |
# Function to generate image description using Claude 3 Sonnet
|
| 48 |
-
def generate_image_description_with_claude(image_base64):
|
| 49 |
claude_body = json.dumps({
|
| 50 |
"anthropic_version": "bedrock-2023-05-31",
|
| 51 |
"max_tokens": 1000,
|
| 52 |
-
"system": "Please
|
| 53 |
"messages": [{
|
| 54 |
"role": "user",
|
| 55 |
"content": [
|
| 56 |
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": image_base64}},
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
]
|
| 59 |
}]
|
| 60 |
})
|
|
@@ -94,16 +97,18 @@ def start_image_search(image, text):
|
|
| 94 |
}, {"$project": {"image": 1}}
|
| 95 |
]))
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
for image_doc in doc:
|
| 99 |
pil_image = Image.open(io.BytesIO(base64.b64decode(image_doc['image'])))
|
| 100 |
img_byte = io.BytesIO()
|
| 101 |
pil_image.save(img_byte, format='JPEG')
|
| 102 |
img_base64 = base64.b64encode(img_byte.getvalue()).decode('utf-8')
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
# Gradio Interface
|
| 109 |
with gr.Blocks() as demo:
|
|
@@ -116,8 +121,8 @@ with gr.Blocks() as demo:
|
|
| 116 |
""")
|
| 117 |
gr.Interface(fn=start_image_search,
|
| 118 |
inputs=[gr.Image(type="pil", label="Upload an image"), gr.Textbox(label="Enter an adjustment to the image")],
|
| 119 |
-
outputs=gr.Gallery(label="Located images
|
| 120 |
-
columns=[3], rows=[1], object_fit="contain", height="auto")
|
| 121 |
)
|
| 122 |
|
| 123 |
demo.launch()
|
|
|
|
| 45 |
celeb_images = client[db_name][collection_name]
|
| 46 |
|
| 47 |
# Function to generate image description using Claude 3 Sonnet
|
| 48 |
+
def generate_image_description_with_claude(images_base64_strs, image_base64):
|
| 49 |
claude_body = json.dumps({
|
| 50 |
"anthropic_version": "bedrock-2023-05-31",
|
| 51 |
"max_tokens": 1000,
|
| 52 |
+
"system": "Please act as face comperison analyzer.",
|
| 53 |
"messages": [{
|
| 54 |
"role": "user",
|
| 55 |
"content": [
|
| 56 |
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": image_base64}},
|
| 57 |
+
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": images_base64_strs[0]}},
|
| 58 |
+
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": images_base64_strs[1]}},
|
| 59 |
+
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": images_base64_strs[2]}},
|
| 60 |
+
{"type": "text", "text": "Please let the user know how his first image is similar to the other 3 and which one is the most similar?"}
|
| 61 |
]
|
| 62 |
}]
|
| 63 |
})
|
|
|
|
| 97 |
}, {"$project": {"image": 1}}
|
| 98 |
]))
|
| 99 |
|
| 100 |
+
images = []
|
| 101 |
+
images_base64_strs = []
|
| 102 |
for image_doc in doc:
|
| 103 |
pil_image = Image.open(io.BytesIO(base64.b64decode(image_doc['image'])))
|
| 104 |
img_byte = io.BytesIO()
|
| 105 |
pil_image.save(img_byte, format='JPEG')
|
| 106 |
img_base64 = base64.b64encode(img_byte.getvalue()).decode('utf-8')
|
| 107 |
+
images_base64_strs.append(img_base64)
|
| 108 |
+
images.append(pil_image)
|
| 109 |
|
| 110 |
+
description = generate_image_description_with_claude(images_base64_strs, img_base64_str)
|
| 111 |
+
return images_with_descriptions, description
|
| 112 |
|
| 113 |
# Gradio Interface
|
| 114 |
with gr.Blocks() as demo:
|
|
|
|
| 121 |
""")
|
| 122 |
gr.Interface(fn=start_image_search,
|
| 123 |
inputs=[gr.Image(type="pil", label="Upload an image"), gr.Textbox(label="Enter an adjustment to the image")],
|
| 124 |
+
outputs=[gr.Gallery(label="Located images for AI-generated descriptions", show_label=False, elem_id="gallery",
|
| 125 |
+
columns=[3], rows=[1], object_fit="contain", height="auto"),gr.TextBox(label="AI Based vision description")]
|
| 126 |
)
|
| 127 |
|
| 128 |
demo.launch()
|