Skip to content

API server

kwankill edited this page Jun 21, 2024 · 13 revisions

# API Server

## main.py

Features:

A server that uses FastAPI to respond to one API request. Connect to GCP to process data.

Features: A server that responds to various API requests using FastAPI. Connects to GCP Postgre to process data.

### API Endpoints:

Top 5 Similar Clothes (/find): find a similar Clothes in our DB and Return the Top 5 Similar Clothes INFO (product name, price, discount price, discount rate, photo location, keyword, purchase link) --> After receiving the uploaded image file and searching for similar images, return the ID of the similar image and related data list (product name, price, discount price, discount rate, photo location, keyword, purchase link)

## FastAPI

FastAPI is a modern web framework based on Python that focuses on building fast and efficient web APIs, which value performance, ease of use, security, and standards compliance, and offer a variety of functions.

Key Features

Quick Search Our service shows faster search results than when the results of the Faiss index in memory are stored in memory, stored in another DB, and then imported again.

Image preprocessing Preprocess the uploaded image before entering it into the model. Use the torchvision.transforms module to resize, center-cut, tensor transform, and normalize the image.

Use the EfficientNet B3 model We load and predict the model using the timm library to load the pre-trained EfficientNet B3 model and convert the image into a vector for use.

Using the FAISS Index For large-scale vector retrieval, we use the FAISS index and load the pre-generated FAISS index to search for similar images.

vector normalization To increase the accuracy of the search, the norm of the vector is calculated and normalized using numpy.

Connecting to the GCP Database Connect to the PostgreSQL database to query the metadata for the retrieved images, and use psycopg2 to establish database connections and perform queries.

Default Use Examples

The following example shows how to create a simple API using FastAPI.

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

This example creates a FastAPI application and defines two endpoints: /Endpoint returns the "Hello World" message, /items/{item_id}Endpoint returns a JSON response by processing path and query parameters.

Installation and Run To install FastAPI, use the following command.

pip install fastapi
pip install "uvicorn[standard]"

Use uvicorn to run the application.

uvicorn main:app --reload where main is the name of the Python file where the FastAPI application is defined, and app is the FastAPI instance. The -- reload option automatically restarts the server when the code changes.

API Pre-Work and Use

Use faiss to vectorize images contained in PostgreSQL, map ID and vector to vector DB, and store the index of vector DB

The image delivered to fastapi is also vectorized in the same way and then searched in the vector DB

   index = faiss.read_index("faiss_index2.index")

   # query_vector is the vector in the Input image
   D, I = index.search(query_vector,k)

Sort I in order of highest similarity (most similar image) Select k images in the highest order and query PostgreSQL to get detailed data from the images and return APIs

Run API server using nohup to enable API to operate in the background even if the connection is terminated

   nohup uvicorn main2:app --host 0.0.0.0 --port 8005 &

Library

API

Model

Clone this wiki locally