-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_food_ml.py
63 lines (54 loc) · 1.99 KB
/
serve_food_ml.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
from fastapi import FastAPI, UploadFile, File
from PIL import Image
import io
import pickle5 as pickle
import torch
import clip
#models are saved in either joblib or pickle
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model, preprocess = clip.load("ViT-B/32", device=device)
class_names = []
with open('classes.txt', 'r') as f:
for line in f:
class_name = line.strip()
class_names.append(class_name)
# Define the FastAPI app
app = FastAPI()
def get_classifier(file_name: str):
with open(file_name, 'rb') as file:
classifier_ = pickle.load(file)
return classifier_
file_name = 'clip_food101_model.pkl'
classifier = get_classifier(file_name)
def get_features(image):
all_features = []
image_input = preprocess(image).unsqueeze(0).to(device)
with torch.no_grad():
features = model.encode_image(image_input.to(device))
all_features.append(features)
print(all_features)
return torch.cat(all_features).cpu().numpy()
# Define the prediction function
def predict_image(image: Image.Image) -> int:
# Preprocess the image
features = get_features(image)
# Make predictions using the loaded model
prediction = classifier.predict(features)
prob = classifier.predict_proba(features)
print(prob[0])
return int(prediction[0]), prob[0][int(prediction)]
# Define the prediction endpoint
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
# Read and decode the uploaded image file
contents = await file.read()
image = Image.open(io.BytesIO(contents))
# Validate the file format
allowed_formats = ('.jpeg', '.jpg', '.png')
if file.filename.lower().endswith(allowed_formats):
# Make predictions
prediction, prob = predict_image(image)
#macarons - for everything not food
return {"prediction": class_names[prediction], "confidence": prob}
else:
return {"error": "Invalid file format. Only JPEG, JPG, and PNG images are supported."}