Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/raae 9/repo upgrades #23

Merged
merged 17 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Test Suite

on:
pull_request:
branches:
- master

push:
branches:
- master

jobs:
test:
name: Python ${{ matrix.python-version }} - ${{ matrix.connection }} [redis-stack ${{matrix.redis-stack-version}}]
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
python-version: ["3.11"]
redis-stack-version: ['latest']

services:
redis:
image: redis/redis-stack-server:${{matrix.redis-stack-version}}
ports:
- 6379:6379

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Install Poetry
uses: snok/install-poetry@v1

- name: Install dependencies
working-directory: ./backend
run: |
poetry install --all-extras

- name: Run tests
working-directory: ./backend
run: |
poetry run test
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
*.egg-info
build
__pycache__
node_modules
.DS_Store
images
.ipynb_checkpoints
product_metadata.json
product_vectors.json
product_vectors.json
data/
!backend/data
.env
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the setup for vscode debugger. I like to check these in in case people want it for development purposes since it's a reference architecture.

"configurations": [
{
"name": "Python Debugger: FastAPI",
"type": "debugpy",
"cwd": "${workspaceFolder}/backend/",
"env": {
"PYTHONPATH": "${cwd}",
"REDIS_HOST": "localhost",
},
"request": "launch",
"module": "uvicorn",
"args": [
"productsearch.main:app",
"--port=8888",
"--reload"
],
"jinja": true,
}
]
}
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"python.testing.pytestArgs": [
"backend"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.cwd": "${workspaceFolder}/backend/",
}
45 changes: 27 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
FROM node:18.8-alpine AS ReactImage
FROM node:22.0-alpine AS ReactImage

WORKDIR /app/gui
WORKDIR /app/frontend

ENV NODE_PATH=/app/gui/node_modules
ENV PATH=$PATH:/app/gui/node_modules/.bin
ENV NODE_PATH=/app/frontend/node_modules
ENV PATH=$PATH:/app/frontend/node_modules/.bin

COPY ./gui/package.json ./
RUN yarn install --no-optional
COPY ./frontend/package.json ./
RUN npm install

ADD ./gui ./
RUN yarn build
ADD ./frontend ./
RUN npm run build

FROM python:3.8-slim-buster AS ApiImage
FROM python:3.11-slim-buster AS ApiImage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tylerhutcherson slim worked fine for this one so kept the smaller sizes

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

RUN python3 -m pip install --upgrade pip setuptools wheel

WORKDIR /app/
COPY ./data/ ./data
VOLUME [ "/data" ]

RUN apt-get update && \
apt-get install -y curl && \
rm -rf /var/lib/apt/lists/*

RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false

RUN mkdir -p /app/backend

# copy deps first so we don't have to reload everytime
COPY ./backend/poetry.lock ./backend/pyproject.toml ./backend/

WORKDIR /app/backend
RUN poetry install --all-extras --no-interaction

COPY ./app/ .
RUN pip install -e .
COPY ./backend/ .

# add static react files to fastapi image
COPY --from=ReactImage /app/gui/build /app/backend/vecsim_app/templates/build
COPY --from=ReactImage /app/frontend/build /app/backend/productsearch/templates/build

LABEL org.opencontainers.image.source https://github.com/RedisVentures/redis-product-search

WORKDIR /app/backend/vecsim_app

CMD ["sh", "./entrypoint.sh"]
CMD ["poetry", "run", "start-app"]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2022, Sam Partee
Copyright (c) 2022, Redis Inc
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
93 changes: 65 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

<div align="center">
<a href="https://github.com/spartee/redis-vector-search"><img src="https://github.com/Spartee/redis-vector-search/blob/master/app/vecsim_app/data/redis-logo.png?raw=true" width="30%"><img></a>
<a href="https://github.com/spartee/redis-vector-search"><img src="https://redis.io/wp-content/uploads/2024/04/Logotype.svg?raw=true" width="30%"><img></a>
<br />
<br />
<div display="inline-block">
<a href="https://ecommerce.redisventures.com"><b>Hosted Demo</b></a>&nbsp;&nbsp;&nbsp;
<a href="https://github.com/Spartee/redis-vector-search"><b>Code</b></a>&nbsp;&nbsp;&nbsp;
<a href="https://github.com/redis-developer/redis-product-search"><b>Code</b></a>&nbsp;&nbsp;&nbsp;
<a href="https://redis.io/docs/stack/search/reference/vectors/"><b>Redis VSS Documentation</b></a>&nbsp;&nbsp;&nbsp;
</div>
<br />
Expand Down Expand Up @@ -33,23 +33,57 @@ The following Redis Stack capabilities are available in this demo:
This app was built as a Single Page Application (SPA) with the following components:

- **[Redis Stack](https://redis.io/docs/stack/)**: Vector database + JSON storage
- **[FastAPI](https://fastapi.tiangolo.com/)** (Python 3.8)
- JWT authentication using [OAuth2 "password
flow"](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/) and
PyJWT
- **[RedisVL](https://redisvl.com)** for Python vector db client
- **[FastAPI](https://fastapi.tiangolo.com/)** for backend API
- **[Pydantic](https://pydantic-docs.helpmanual.io/)** for schema and validation
- **[React](https://reactjs.org/)** (with Typescript)
- **[Redis OM](https://redis.io/docs/stack/get-started/tutorials/stack-python/)** for ORM
- **[Docker Compose](https://docs.docker.com/compose/)** for development
- **[MaterialUI](https://material-ui.com/)** for some UI elements
- **[React-Bootstrap](https://react-bootstrap.github.io/)** for some UI elements
- **[react-admin](https://github.com/marmelab/react-admin)** for the admin dashboard
- Using the same token based authentication as FastAPI backend (JWT)
- **[Pytorch/Img2Vec](https://github.com/christiansafka/img2vec)** and **[Huggingface Sentence Transformers](https://huggingface.co/sentence-transformers)** for vector embedding creation

Some inspiration was taken from this [Cookiecutter project](https://github.com/Buuntu/fastapi-react)
and turned into a SPA application instead of a separate front-end server approach.

### General Project Structure

Much inspiration taken from [tiangelo/full-stack-fastapi-template](https://github.com/tiangolo/full-stack-fastapi-template)

```
/backend
/productsearch
/api
/routes
product.py # primary API logic lives here
/db
load.py # seeds Redis DB
redis_helpers.py # redis util
/schema
# pydantic models for serialization/validation from API
/tests
/utils
config.py
spa.py # logic for serving compiled react project
main.py # entrypoint
/frontend
/public
# index, manifest, logos, etc.
/src
/config
/styles
/views
# primary components live here

api.ts # logic for connecting with BE
App.tsx # project entry
Routes.tsk # route definitions
...
/data
# folder mounted as volume in Docker
# load script auto populates initial data from S3

```

### Datasets

The dataset was taken from the the following Kaggle links.
Expand All @@ -58,12 +92,10 @@ The dataset was taken from the the following Kaggle links.
- [Smaller Dataset](https://www.kaggle.com/datasets/paramaggarwal/fashion-product-images-small)


## Running the App
## Running the App with docker-compose
Before running the app, install [Docker Desktop](https://www.docker.com/products/docker-desktop/).



#### Redis Cloud (recommended)
#### Using Redis Cloud (recommended)

1. [Get your Redis Cloud Database](https://app.redislabs.com/) (if needed).

Expand All @@ -81,34 +113,39 @@ Before running the app, install [Docker Desktop](https://www.docker.com/products

> The benefit of this approach is that the db will persist beyond application runs. So you can make updates and re run the app without having to provision the dataset or create another search index.

#### Redis Docker
#### Using Redis Docker
```bash
$ docker compose -f docker-local-redis.yml up
```

### Customizing (optional)
You can use the Jupyter Notebook in the `data/` directory to create product embeddings and product metadata JSON files. Both files will end up stored in the `data/` directory and used when creating your own container.

Create your own containers using the `build.sh` script and then make sure to update the `.yml` file with the right image name.
## Running without docker-compose

### Run frontend

### Using a React development env
It's typically easier to write front end code in an interactive environment, testing changes in realtime.

1. Deploy the app using steps above.
2. Install NPM packages (you may need to use `npm` to install `yarn`)
1. Install NPM packages
```bash
$ cd gui/
$ yarn install --no-optional
$ cd frontend/
$ npm install
````
4. Use `yarn` to serve the application from your machine
2. Use `npm` to serve the application from your machine
```bash
$ yarn start
$ npm run start
```
5. Navigate to `http://localhost:3000` in a browser.
3. Navigate to `http://localhost:3000` in a browser.

All changes to your local code will be reflected in your display in semi realtime.

### Run backend
Pre-step: install [poetry](https://python-poetry.org/).

1. `cd backend`
2. `poetry install` to get necessary python deps
3. `poetry run start` to launch uvicorn server with FastAPI app

### vscode debugger

Included in the project is a `./vscode/launch.json` for local debugging purposes.

### Troubleshooting
Sometimes you need to clear out some Docker cached artifacts. Run `docker system prune`, restart Docker Desktop, and try again.

Expand Down
7 changes: 0 additions & 7 deletions app/requirements.txt

This file was deleted.

43 changes: 0 additions & 43 deletions app/setup.cfg

This file was deleted.

4 changes: 0 additions & 4 deletions app/setup.py

This file was deleted.

6 changes: 0 additions & 6 deletions app/vecsim_app/__init__.py

This file was deleted.

Loading
Loading