Pash1986 commited on
Commit
daae077
1 Parent(s): 127cf93

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +105 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pymongo import MongoClient
3
+ from PIL import Image
4
+ import base64
5
+ import os
6
+ import io
7
+ import boto3
8
+ import json
9
+
10
+ bedrock_runtime = boto3.client('bedrock-runtime',
11
+ aws_access_key_id=os.environ.get('AWS_ACCESS_KEY'),
12
+ aws_secret_access_key=os.environ.get('AWS_SECRET_KEY'),
13
+ region_name="us-east-1"
14
+ )
15
+
16
+ def construct_bedrock_body(base64_string, text):
17
+ if text:
18
+ return json.dumps(
19
+ {
20
+ "inputImage": base64_string,
21
+ "embeddingConfig": {"outputEmbeddingLength": 1024},
22
+ "inputText": text
23
+ }
24
+ )
25
+
26
+ return json.dumps(
27
+ {
28
+ "inputImage": base64_string,
29
+ "embeddingConfig": {"outputEmbeddingLength": 1024},
30
+ }
31
+ )
32
+
33
+
34
+ def get_embedding_from_titan_multimodal(body):
35
+
36
+
37
+ response = bedrock_runtime.invoke_model(
38
+ body=body,
39
+ modelId="amazon.titan-embed-image-v1",
40
+ accept="application/json",
41
+ contentType="application/json",
42
+ )
43
+
44
+ response_body = json.loads(response.get("body").read())
45
+ return response_body["embedding"]
46
+
47
+ uri = os.environ.get('MONGODB_ATLAS_URI')
48
+ client = MongoClient(uri)
49
+ db_name = 'celebrity_1000_embeddings'
50
+ collection_name = 'celeb_images'
51
+
52
+ celeb_images = client[db_name][collection_name]
53
+
54
+ def start_image_search(image, text):
55
+ if not image:
56
+ ## Alert the user to upload an image
57
+ raise gr.Error("Please upload an image first, make sure to press the 'Submit' button after selecting the image.")
58
+ buffered = io.BytesIO()
59
+ image.save(buffered, format="JPEG")
60
+ img_byte = buffered.getvalue()
61
+ # Encode this byte array to Base64
62
+ img_base64 = base64.b64encode(img_byte)
63
+
64
+ # Convert Base64 bytes to string for JSON serialization
65
+ img_base64_str = img_base64.decode('utf-8')
66
+ body = construct_bedrock_body(img_base64_str, text)
67
+ embedding = get_embedding_from_titan_multimodal(body)
68
+
69
+ doc = list(celeb_images.aggregate([{
70
+ "$vectorSearch": {
71
+ "index": "vector_index",
72
+ "path" : "embeddings",
73
+ "queryVector": embedding,
74
+ "numCandidates" : 15,
75
+ "limit" : 3
76
+ }}, {"$project": {"image":1}}]))
77
+
78
+ images = []
79
+ for image in doc:
80
+ images.append(Image.open(io.BytesIO(base64.b64decode(image['image']))))
81
+
82
+ return images
83
+
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown(
86
+ """
87
+ # MongoDB's Vector Celeb Image matcher
88
+
89
+ Upload an image and find the most similar celeb image from the database.
90
+
91
+ 💪 Make a great pose to impact the search! 🤯
92
+
93
+ """)
94
+
95
+ ### Image gradio input
96
+ gr.Interface(
97
+ fn=start_image_search,
98
+ inputs=[gr.Image(type="pil", label="Upload an image"),gr.Textbox(label="Enter an adjusment to the image")],
99
+ ## outputs=gr.Image(type="pil")
100
+ outputs=gr.Gallery(
101
+ label="Generated images", show_label=True, elem_id="gallery"
102
+ , columns=[3], rows=[1], object_fit="contain", height="auto")
103
+ )
104
+
105
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pymongo
2
+ boto3
3
+ gradio