Fullstack handwritten digits recognizer project with TensorFlow, OpenCV, Django, and React.
Digits.Recognizer.Demo.2.mp4
See the demo: digits-recognizer.apps.nashruddinamin.com
Ensure that you have Python 3.8+, Pipenv, and recent Node.js installed on your local machine.
To run the backend server:
-
Change to the
backend/
directory and install the dependencies:cd backend pipenv install --dev
-
Run the development server:
pipenv run python manage.py runserver
It will run Django's development server on port 8000.
-
Open
http://localhost:8000
with your browser to see the welcome message. -
(Optional) run the unit tests:
pipenv run pytest
To run the frontend app:
-
Change to the
frontend/
directory and install the dependencies:cd frontend yarn install
-
Run the server:
yarn start
-
Open
http://localhost:3000
with your browser to see the frontend app.
Ensure that you have Docker and Docker Compose installed on your machine. Build and run the containers with:
docker-compose build
docker-compose up
Then open http://localhost:3000
with your browser to see the frontend app.
If you want to run on machine other than localhost (e.g: EC2 instance), you need to
set the FRONTEND_URL
and BACKEND_URL
environment variables so both frontend and
backend know how to talk to each other.
For example, if the public IP address of your EC2 instance is 1.2.3.4
then you need
to run the app with:
export FRONTEND_URL=http://1.2.3.4:3000
export BACKEND_URL=http://1.2.3.4:8000
docker-compose build
docker-compose up
There are many ways to deploy the app to AWS. This example will show you how to deploy the React frontend to AWS S3 and the Django backend to AWS Lambda.
Make sure you have Python 3.8+, Pipenv, Docker, and recent Node.js installed on your machine. In addition, this example assumes that you have a domain name with two subdomains registered with Route53:
app.example.com
api.example.com
and you have obtained the SSL certificate from ACM.
To deploy the backend to AWS Lambda:
-
Change your working directory to the
backend/
dir:cd backend
-
Create new virtualenv and install Zappa:
python3 -m venv venv . venv/bin/activate pip install troposphere==2.7.1 zappa
-
Create a new file named
zappa_settings.json
with the following content:{ "prod": { "aws_region": "us-west-2", "django_settings": "config.settings.production", "profile_name": "default", "project_name": "digits-recognizer-backend", "s3_bucket": "digits-recognizer-backend", "domain": "api.example.com", "certificate_arn": "<certificate ARN>", "environment_variables": { "DJANGO_SETTINGS_MODULE": "config.settings.production", "SECRET_KEY": "<some random string>", "FRONTEND_HOSTNAME": "app.example.com", "BACKEND_HOSTNAME": "api.example.com" } } }
Note that the environment variables above are required by the Django app.
Visit the Zappa repository to learn more about other settings you can use.
-
Generate
zappa_settings.py
from the JSON file above:zappa save-python-settings-file
-
Build the docker image:
docker build -f Dockerfile-lambda -t digits-recognizer-backend .
-
Push the Docker image to an ECR repository:
# Create a new repository aws ecr create-repository --repository-name digits-recognizer-backend # Retrieve an authentication token and authenticate your Docker client aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin <account number>.dkr.ecr.us-west-2.amazonaws.com # Tag your image so you can push the image to the repository docker tag \ digits-recognizer-backend:latest \ <account number>.dkr.ecr.us-west-2.amazonaws.com/digits-recognizer-backend:latest # Push this image to your newly created ECR repository docker push <account number>.dkr.ecr.us-west-2.amazonaws.com/digits-recognizer-backend:latest
-
Deploy with Zappa:
zappa deploy -d <account number>.dkr.ecr.us-west-2.amazonaws.com/digits-recognizer-backend:latest
-
Register your custom domain name with your API gateway:
zappa certify
-
Ping your backend:
curl http://api.example.com
It should return the welcome message.
To deploy the frontend to AWS S3:
-
Follow these tutorials for configuring S3 bucket to host static website:
After finishing those tutorials, you will have a bucket that is publicly accessible from your subdomain. You might want to put a sample index.html in your bucket to make sure everything is working as expected.
-
Change your working directory to the
frontend/
dir:cd frontend
-
Create a new file named
.env.production
with the following content:REACT_APP_API_BASE_URL=https://api.example.com
-
Build the frontend files:
yarn build
-
Copy the files to your S3 bucket:
aws s3 sync ./build s3://<your S3 bucket>
-
The frontend should be available at
https://app.example.com
.
MIT