-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_template.py
131 lines (94 loc) · 3.73 KB
/
api_template.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
import pickle
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from fastapi import File
from fastapi import UploadFile
from fastapi import Form
import torch
import torch.nn as nn
from pydantic import BaseModel
import faiss, os, shutil, uuid
import numpy as np
from contextlib import asynccontextmanager
import image_processor
class FeatureExtractor(nn.Module):
def __init__(self,
decoder: dict = None):
super(FeatureExtractor, self).__init__()
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.model = torch.load('model_evaluation/feature/best_weights.pt')
self.decoder = decoder
def forward(self, image):
x = self.main(image)
return x
def predict(self, image):
with torch.no_grad():
x = self.forward(image)
return x
# Don't change this, it will be useful for one of the methods in the API
class TextItem(BaseModel):
text: str
try:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = torch.load('model_evaluation/feature/best_weights.pt')
pass
except:
raise OSError("No Feature Extraction model found. Check that you have the decoder and the model in the correct location")
try:
index = faiss.read_index('images_faiss.index')
pass
except:
raise OSError("No Image model found. Check that you have the encoder and the model in the correct location")
@asynccontextmanager
async def lifespan(app: FastAPI):
#everything else on startup is already implemented
image_processor.validate_output_dir("received_files")
yield
# Clean up the uploads folder
for filename in os.listdir('received_files'):
file_path = os.path.join('received_files', filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
print("uploads directory cleared. Bye!")
app = FastAPI(lifespan=lifespan)
print("Starting server")
@app.get('/healthcheck')
def healthcheck():
msg = "API is up and running!"
return {"message": msg}
@app.post('/healthcheck/file')
def healthcheck_file(image: UploadFile = File(...)):
newPath = os.path.join('received_files',str(uuid.uuid4())+'_'+image.filename)
with open(newPath, "wb") as buffer:
shutil.copyfileobj(image.file, buffer)
return {"message": newPath}
@app.post('/predict/feature_embedding')
def predict_image(image: UploadFile = File(...)):
newPath = os.path.join('received_files',str(uuid.uuid4())+'_'+image.filename)
with open(newPath, "wb") as buffer:
shutil.copyfileobj(image.file, buffer)
embedding = image_processor.getEmbedding(image=newPath,model=model)
return JSONResponse(content={
"features": embedding.tolist(),
})
@app.post('/predict/similar_images')
def predict_combined(matches: str = 3,image: UploadFile = File(...)):
newPath = os.path.join('received_files',str(uuid.uuid4())+'_'+image.filename)
with open(newPath, "wb") as buffer:
shutil.copyfileobj(image.file, buffer)
searchEmbedding = image_processor.getEmbedding(newPath,model=model)
searchEmbedding = np.array(searchEmbedding,dtype="float32",ndmin=2)
nprobe = 5
distances, indices = index.search(x=searchEmbedding,k=matches)
return JSONResponse(content={
"similar_indices": indices.tolist(), # Return the index of similar images here
})
if __name__ == '__main__':
uvicorn.run("api:app", host="0.0.0.0", port=8080)