-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannoy_app.py
242 lines (185 loc) · 7.39 KB
/
annoy_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import sqlite3
import glob
import torch
import clip
import uvicorn
import numpy as np
import pandas as pd
import random
import string
from fastapi import FastAPI
from fastapi import Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from fastapi import FastAPI, File, UploadFile
from fastapi import BackgroundTasks
from annoy import AnnoyIndex
from typing import List
from PIL import Image
from sklearn.metrics.pairwise import cosine_similarity
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
CONTENT_STORE = 'static'
DATABASE = 'database.db'
ANNOY_INDEX_FILE = 'annoy_indexes.ann'
NUM_OF_RESULTS_TO_SHOW = 5
NUM_OF_TREES_TO_BUILD = 5
INDEX_METRIC = 'angular'
embed_length = 512
annoy_indexer = AnnoyIndex(embed_length, INDEX_METRIC)
ANNOY_INDEX = 0
print("Loading Model...")
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
def create_tables():
conn = sqlite3.connect(DATABASE)
conn.execute('CREATE TABLE IF NOT EXISTS image_embeds (id INTEGER PRIMARY KEY, annoy_index TEXT, image_path TEXT, embedding BLOB)')
conn.execute('CREATE TABLE IF NOT EXISTS text_embeds (id INTEGER PRIMARY KEY, text_seq TEXT, embedding BLOB)')
conn.commit()
def preprocess_image(image_path):
image = Image.open(image_path)
image = preprocess(image).unsqueeze(0).to(device)
return image
def preprocess_text(text):
return clip.tokenize(text).to(device)
def store_uploaded_images(uploaded_files):
image_paths = []
for uploaded_file in uploaded_files:
rand_post_text = ''.join(random.choices(string.ascii_uppercase+string.digits, k = 10))
fname_split = uploaded_file.filename.split('.')
fname = fname_split[0] + rand_post_text + "." + fname_split[1]
file_location = f"{CONTENT_STORE}/{fname}"
with open(file_location, "wb+") as file_object:
file_object.write(uploaded_file.file.read())
image_paths.append(file_location)
return image_paths
def create_embedding(conn, image_path):
global ANNOY_INDEX
processed_image = preprocess_image(image_path)
with torch.no_grad():
embed = model.encode_image(processed_image).detach().numpy()
embed_str = embed.tostring()
## Insert data into DB
query = "INSERT INTO image_embeds(annoy_index, image_path, embedding) VALUES(?, ?, ?);"
conn.execute(query, (ANNOY_INDEX, image_path, embed_str))
conn.commit()
annoy_indexer.add_item(ANNOY_INDEX, embed[0])
ANNOY_INDEX += 1
print('Inserted', image_path)
def create_image_embeddings(uploaded_files):
conn = sqlite3.connect(DATABASE)
image_paths = store_uploaded_images(uploaded_files)
for image_path in image_paths:
create_embedding(conn, image_path)
annoy_indexer.build(NUM_OF_TREES_TO_BUILD)
annoy_indexer.save(ANNOY_INDEX_FILE)
update_annoy_index()
return 'Embeddings Created !'
def get_image_data_df():
con = sqlite3.connect(DATABASE)
df = pd.read_sql("SELECT image_path, embedding FROM image_embeds;", con)
return df
def exec_query(query):
conn = sqlite3.connect(DATABASE)
curr = conn.cursor()
curr.execute(query)
rows = curr.fetchall()
return rows
def update_annoy_index():
global ANNOY_INDEX
query = "SELECT id FROM image_embeds ORDER BY id DESC LIMIT 1;"
rows = exec_query(query)
if rows==[]:
ANNOY_INDEX = 0
else:
ANNOY_INDEX = int(rows[0][0]) + 1
print('Updated Annoy Index as:', ANNOY_INDEX)
## GET APIs
@app.get("/")
def index():
return {"message": "Hello World!"}
@app.get("/startindexinghome", response_class=HTMLResponse)
def imagesearchhome(request: Request):
return templates.TemplateResponse("create_img_embeds.html", {"request": request})
@app.get("/imagesearchhome", response_class=HTMLResponse)
def imagesearchhome(request: Request):
df = get_image_data_df()
images = df['image_path'].tolist()
return templates.TemplateResponse("index.html", {"request": request, "images": images})
@app.get("/revimagesearchhome", response_class=HTMLResponse)
def revimagesearchhome(request: Request):
df = get_image_data_df()
images = df['image_path'].tolist()
return templates.TemplateResponse("reverse_image_search.html", {"request": request, "images": images})
## POST APIs
@app.post("/startindexing", response_class=HTMLResponse)
async def create_image_embeds(request: Request, background_tasks: BackgroundTasks,
uploaded_files: List[UploadFile] = File(...)):
background_tasks.add_task(create_image_embeddings, uploaded_files)
return templates.TemplateResponse("create_img_embeds.html", {"request": request, "msg": "Indexing Started"})
@app.post("/imagesearch", response_class=HTMLResponse)
async def imagesearch(request: Request):
global NUM_OF_RESULTS_TO_SHOW
form_data = await request.form()
text = form_data['text']
## Using Annoy indexer
ann_indexer = AnnoyIndex(embed_length, INDEX_METRIC)
ann_indexer.load(ANNOY_INDEX_FILE)
total_indexes = ann_indexer.get_n_items()
print('Total Indexes in Annoy Indexer:', ann_indexer.get_n_items())
## Get Text Embedding
processed_text = preprocess_text([text])
with torch.no_grad():
text_embed = model.encode_text(processed_text)
if NUM_OF_RESULTS_TO_SHOW > total_indexes:
NUM_OF_RESULTS_TO_SHOW = total_indexes
## Get Indexes
indexes = ann_indexer.get_nns_by_vector(text_embed[0], NUM_OF_RESULTS_TO_SHOW, search_k=-1, include_distances=False)
indexes = [str(i) for i in indexes]
print('Indexes found relevant to the query:', indexes)
images = []
for index in indexes:
query = "SELECT image_path from image_embeds WHERE annoy_index = {0}".format(index)
row = exec_query(query)
images.append(row[0][0])
print(images)
context = {"request": request, "images": images, "text": text}
return templates.TemplateResponse("index.html", context)
@app.post("/revimagesearch", response_class=HTMLResponse)
async def revimagesearch(request: Request, uploaded_file: UploadFile = File(...)):
global NUM_OF_RESULTS_TO_SHOW
rand_post_text = ''.join(random.choices(string.ascii_uppercase+string.digits, k = 10))
fname_split = uploaded_file.filename.split('.')
fname = fname_split[0] + rand_post_text + "." + fname_split[1]
file_location = f"{CONTENT_STORE}/{fname}"
with open(file_location, "wb+") as file_object:
file_object.write(uploaded_file.file.read())
print('Saved at file_location', file_location)
## Using Annoy indexer
ann_indexer = AnnoyIndex(embed_length, INDEX_METRIC)
ann_indexer.load(ANNOY_INDEX_FILE)
total_indexes = ann_indexer.get_n_items()
print('Total Indexes in Annoy Indexer:', ann_indexer.get_n_items())
## Get Text Embedding
processed_text = preprocess_image(file_location)
with torch.no_grad():
text_embed = model.encode_text(processed_text)
if NUM_OF_RESULTS_TO_SHOW > total_indexes:
NUM_OF_RESULTS_TO_SHOW = total_indexes
## Get Indexes
indexes = ann_indexer.get_nns_by_vector(text_embed[0], NUM_OF_RESULTS_TO_SHOW, search_k=-1, include_distances=False)
indexes = [str(i) for i in indexes]
print('Indexes found relevant to the query:', indexes)
query = "SELECT image_path from image_embeds WHERE annoy_index IN ({0})".format(','.join(indexes))
rows = exec_query(query)
images = [i[0] for i in rows]
print(images)
context = {"request": request, 'images': images, "query_img": file_location}
return templates.TemplateResponse("reverse_image_search.html", context)
## Create tables
create_tables()
update_annoy_index()
if __name__ == "__main__":
uvicorn.run(app, host='127.0.0.1', port=8000, debug=True)