File size: 5,479 Bytes
0bb48b2 24f3093 9a9d227 24f3093 334dbbc 121c5b5 ef9877c 334dbbc 24f3093 4121175 87906f0 89ffd90 4121175 4056967 4121175 4056967 4121175 9a9d227 24f3093 ef9877c 4d8cbeb ef9877c 4121175 1956f2b 4121175 89ffd90 1956f2b 89ffd90 4e5fb56 89ffd90 24f3093 0309540 24f3093 | 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 | from fastapi import FastAPI,UploadFile, File
from typing import Annotated
from fastapi.responses import JSONResponse
import uvicorn
from pydantic import BaseModel
import requests
import os
import pandas as pd
app = FastAPI()
import unicodedata
import string
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
from PIL import Image
import torchvision.transforms as transforms
import io
all_letters = string.ascii_letters + " .,;'"
n_letters = len(all_letters)
all_categories = ['Arabic','Chinese','Czech','Dutch','English','French','German','Greek',
'Irish','Italian','Japanese','Korean','Polish','Portuguese','Russian','Scottish',
'Spanish','Vietnamese']
# Turn a Unicode string to plain ASCII, thanks to https://stackoverflow.com/a/518232/2809427
def unicodeToAscii(s):
return ''.join(
c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn'
and c in all_letters
)
# Find letter index from all_letters, e.g. "a" = 0
def letterToIndex(letter):
return all_letters.find(letter)
# Turn a line into a <line_length x 1 x n_letters>,
# or an array of one-hot letter vectors
def lineToTensor(line):
tensor = torch.zeros(len(line), 1, n_letters)
for li, letter in enumerate(line):
tensor[li][0][letterToIndex(letter)] = 1
return tensor
# Just return an output given a line
def evaluate_model(line_tensor):
model = torch.jit.load("torchscript_classify_names_lstm.pt")
hidden = (torch.zeros(1, 1, 128),torch.zeros(1, 1, 128))
output = model(line_tensor,hidden)
return output
def classify_lastname(last_name):
# Converting to Ascii and capitalizing first letter
last_name = unicodeToAscii(last_name)
last_name = last_name.title()
# Converting name to tensor
line_tensor = lineToTensor(last_name)
output = evaluate_model(line_tensor)
# Grabbing top probability and category
top_prob, top_cat = torch.topk(output,1)
prob = torch.exp(top_prob[0])
cat = top_cat[0]
model_output = {}
model_output[all_categories[cat[0].item()]] = round(prob[0].item(),2)
return model_output
# Define a request model
class Item(BaseModel):
text: str
# Endpoint to return all products by profit
@app.get("/products-by-profit")
def get_products_by_profit(top: int = 10, ascending: bool = False):
df = pd.read_csv("Sample-Superstore.csv",encoding='ISO-8859-1')
sorted_products = (
df.groupby("Product Name")["Profit"]
.sum()
.sort_values(ascending=ascending)
.head(top)
.reset_index()
.rename(columns={"Product Name": "product", "Profit": "profit"})
)
return sorted_products.to_dict(orient="records")
@app.post("/classifyname")
async def classify_name(lastname: Item):
return JSONResponse(content=classify_lastname(lastname.text), status_code=201)
def transform_img(img):
# Transformations that will be applied
the_transform = transforms.Compose([
transforms.Resize((224,224)),
transforms.CenterCrop((224,224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])
])
return the_transform(img)
# Returns string with class and probability
def classify_img(img):
class_names = ['AIR COMPRESSOR',
'ALTERNATOR',
'BATTERY',
'BRAKE CALIPER',
'BRAKE PAD',
'BRAKE ROTOR',
'CAMSHAFT',
'CARBERATOR',
'CLUTCH PLATE',
'COIL SPRING',
'CRANKSHAFT',
'CYLINDER HEAD',
'DISTRIBUTOR',
'ENGINE BLOCK',
'ENGINE VALVE',
'FUEL INJECTOR',
'FUSE BOX',
'GAS CAP',
'HEADLIGHTS',
'IDLER ARM',
'IGNITION COIL',
'INSTRUMENT CLUSTER',
'LEAF SPRING',
'LOWER CONTROL ARM',
'MUFFLER',
'OIL FILTER',
'OIL PAN',
'OIL PRESSURE SENSOR',
'OVERFLOW TANK',
'OXYGEN SENSOR',
'PISTON',
'PRESSURE PLATE',
'RADIATOR',
'RADIATOR FAN',
'RADIATOR HOSE',
'RADIO',
'RIM',
'SHIFT KNOB',
'SIDE MIRROR',
'SPARK PLUG',
'SPOILER',
'STARTER',
'TAILLIGHTS',
'THERMOSTAT',
'TORQUE CONVERTER',
'TRANSMISSION',
'VACUUM BRAKE BOOSTER',
'VALVE LIFTER',
'WATER PUMP',
'WINDOW REGULATOR']
model = torch.jit.load("car_part_traced_classifier_resnet50.ptl")
# Applying transformation to the image
model_img = transform_img(img)
model_img = model_img.view(1,3,224,224)
# Running image through the model
model.eval()
with torch.no_grad():
result = model(model_img)
# Converting values to softmax values
result = F.softmax(result,dim=1)
# Grabbing top 3 indices and probabilities for each index
top3_prob, top3_catid = torch.topk(result,3)
# Dictionary I will display
model_output = {}
for i in range(top3_prob.size(1)):
model_output[class_names[top3_catid[0][i].item()]] = top3_prob[0][i].item()
print(model_output)
return model_output
@app.post("/classifycarpart")
async def upload_image(file: UploadFile = File(...)):
try:
# Read and convert the image to a PIL Image
contents = await file.read()
image = Image.open(io.BytesIO(contents)).convert("RGB")
return JSONResponse(content=classify_img(image), status_code=201)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.get("/")
def api_home():
return JSONResponse({'detail': 'Welcome to FastAPI!'}, status_code=200)
|