Dr Freddy Wordingham

by Dr Freddy Wordingham

Lesson

Web App

15. Code refactor

Over this course we've developed a web-app that can classify user-uploaded images using a convolutional neural network. And it's been a lot of new tech and new code!

Let's take a look at how we can simplify some of our code.

šŸ”™ Backend


In particular, the monolithic main.py file has grown to be a little unweildly. We can simplify things by moving each of the endpoints into its own file.

šŸ“‘ APPENDIX


šŸƒ How to Run

🧱 Build Frontend

Navigate to the frontend/ directory:

cd frontend

Install any missing frontend dependancies:

npm install

Build the files for distributing the frontend to clients:

npm run build

šŸ–² Run the Backend

Go back to the project root directory:

cd ..

Activate the virtual environment, if you haven't already:

source .venv/bin/activate

Install any missing packages:

pip install -r requirements.txt

If you haven't already, train a CNN:

python scripts/train.py

Continue training an existing model:

python scripts/continue_training.py

Serve the web app:

python -m uvicorn main:app --port 8000 --reload

šŸš€ Deploy

Deploy to the cloud:

serverless deploy

Remove from the cloud:

severless remove

šŸ—‚ļø Updated Files

Project structure
.
ā”œā”€ā”€ .venv/
ā”œā”€ā”€ .gitignore
ā”œā”€ā”€ .serverless/
ā”œā”€ā”€ resources
│   └── dog.jpg
ā”œā”€ā”€ frontend
│   ā”œā”€ā”€ build/
│   ā”œā”€ā”€ node_modules/
│   ā”œā”€ā”€ public/
│   ā”œā”€ā”€ src
│   │   ā”œā”€ā”€ App.css
│   │   ā”œā”€ā”€ App.test.tsx
│   │   ā”œā”€ā”€ App.tsx
│   │   ā”œā”€ā”€ ImageGrid.tsx
│   │   ā”œā”€ā”€ ImageUpload.tsx
│   │   ā”œā”€ā”€ index.css
│   │   ā”œā”€ā”€ index.tsx
│   │   ā”œā”€ā”€ Predictions.css
│   │   ā”œā”€ā”€ Predictions.tsx
│   │   ā”œā”€ā”€ logo.svg
│   │   ā”œā”€ā”€ react-app-env.d.ts
│   │   ā”œā”€ā”€ reportWebVitals.ts
│   │   ā”œā”€ā”€ setupTests.ts
│   │   └── Sum.tsx
│   ā”œā”€ā”€ .gitignore
│   ā”œā”€ā”€ package-lock.json
│   ā”œā”€ā”€ package.json
│   ā”œā”€ā”€ README.md
│   └── tsconfig.json
ā”œā”€ā”€ output
│   ā”œā”€ā”€ activations_conv2d/
│   ā”œā”€ā”€ activations_conv2d_1/
│   ā”œā”€ā”€ activations_conv2d_2/
│   ā”œā”€ā”€ activations_dense/
│   ā”œā”€ā”€ activations_dense_1/
│   ā”œā”€ā”€ model.h5
│   ā”œā”€ā”€ sample_images.png
│   └── training_history.png
ā”œā”€ā”€ scripts
│   ā”œā”€ā”€ classify.py
│   ā”œā”€ā”€ continue_training.py
│   └── train.py
ā”œā”€ā”€ main.py
ā”œā”€ā”€ README.md
ā”œā”€ā”€ requirements.txt
└── serverless.yml

This website stores cookies on your computer. These cookies are used to improve your website experience and provide more personalised services to you, both on this website and through other media. To find out more about the cookies we use, see our Privacy Policy.

By continuing to use this site, you accept our use of cookies and agree to our privacy policy, which helps us provide you with a better browsing experience and personalised content.