A complete deep learning application template featuring a Streamlit frontend, FastAPI backend, and MySQL/SQLite database for image classification tasks.
- Deep Learning Application Template
This application follows a modern, scalable architecture:
βββββββββββββββββββ HTTP Requests ββββββββββββββββββββ
β β ββββββββββββββββββββ β β
β Streamlit β β FastAPI β
β Frontend β βββββββββββββββββββββΊβ Backend β
β β β β
β (User Interface)β β (Business Logic) β
βββββββββββββββββββ ββββββββββββββββββββ
β
β
β Image Preprocessing
βΌ
βββββββββββββββββββββββββββ
β β
β PyTorch Model β
β (Inference Logic) β
β β
βββββββββββββββββββββββββββ
β
β Prediction Results
βΌ
βββββββββββββββββββββββββββ
β β
β MySQL β
β Database β
β β
β β’ Image Path β
β β’ Predicted Class β
β β’ Confidence Score β
β β’ Detection Time β
β β
βββββββββββββββββββββββββββ
Flow:
User uploads image β Streamlit sends to FastAPI β FastAPI preprocesses image β
β PyTorch model performs inference β Results stored in MySQL β
β Response to Streamlit β Display to user
- Streamlit: Provides an interactive UI for uploading images and viewing classification results
- Handles image display and prediction visualization
- Communicates with the backend via REST APIs
- FastAPI: High-performance web framework for creating REST APIs
- Handles image preprocessing and model inference
- Manages communication with the database
- Implements async request handling
- PyTorch/TorchVision: A popular deep learning framework
- Performs image preprocessing and model inference
- MySQL: Stores classification results including:
- Image file paths
- Predicted class labels
- Confidence scores
- Timestamps of predictions
- Python 3.8+
- MySQL Server
- UV package manager
-
Clone this repository:
git clone https://github.com/MLSysTeam/deep-learning-app-template cd deep-learning-app-template -
Install dependencies using UV:
uv sync # equivalent to uv pip install -r requirements.txtafter installation, you will see a
.venvfolder created in the project root. -
Set up the MySQL database (skip). In our example, we'll use sqlite for simplicity that doesn't require any setup.
The application includes automatic database creation functionality with a fallback mechanism, which simplifies the setup process:
For quick testing and development, the application will automatically:
- Attempt to connect to the configured MySQL database
- If MySQL is unavailable or access is denied, it will fall back to using a local SQLite database
- Automatically create the required tables regardless of which database is used
Simply run the application and it will handle database initialization automatically!
If you want to use MySQL in a production setting:
-
Install MySQL Server (one-time setup)
- On Ubuntu/Debian:
sudo apt-get install mysql-server - On CentOS/RHEL:
sudo yum install mysql-server - On macOS:
brew install mysql - Or download from the official MySQL website
- On Ubuntu/Debian:
-
Start the MySQL Service
# On Ubuntu/Debian sudo systemctl start mysql # On macOS brew services start mysql
-
Create a MySQL User with Permissions (if not using root)
CREATE USER 'dl_app_user'@'localhost' IDENTIFIED BY 'secure_password'; GRANT ALL PRIVILEGES ON *.* TO 'dl_app_user'@'localhost'; FLUSH PRIVILEGES;
Update your environment variables:
-
Copy
.env.exampleto.env:cp .env.example .env
-
Edit
.envwith your MySQL credentials:DB_USER=your_mysql_username DB_PASSWORD=your_mysql_password DB_HOST=localhost DB_PORT=3306 DB_NAME=image_classification
Note: If the application cannot connect to MySQL (due to wrong credentials, MySQL not running, etc.), it will automatically fall back to using a local SQLite database (
image_classifications.db) for development and testing purposes.
-
Start the backend (in terminal 1):
./start_backend.sh
Or run directly:
uv uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
After the backend is up and running, you can access the interactive API docs at
http://localhost:8000/docs. -
Start the frontend (in terminal 2):
./start_frontend.sh
Or run directly:
uv streamlit run app/frontend.py
- Access the Streamlit frontend at
http://localhost:8501 - Upload an image file (JPG, PNG, etc.)
- Click "Classify Image" to send the image to the backend
- View the classification result on the frontend
- Results are stored in the MySQL database
.
βββ app/
β βββ __init__.py
β βββ main.py # FastAPI backend application
β βββ frontend.py # Streamlit frontend application
β βββ database.py # Database models and connection
β βββ model_handler.py # ML model handling logic
βββ uploads/ # Directory for storing uploaded images
βββ pyproject.toml # Project dependencies and metadata
βββ requirements.txt # Dependencies list
βββ .env.example # Environment variables example
βββ start_backend.sh # Script to start backend service
βββ start_frontend.sh # Script to start frontend service
βββ README.md # This file
βββ README_zh.md # Chinese version of README
To integrate your own PyTorch model:
-
Modify app/model_handler.py to load your model:
- Update the
__init__method to load your specific model - Adjust the
predictmethod to handle your model's input/output format - Modify the
preprocess_imagemethod if your model requires different preprocessing
- Update the
-
Update the classification classes if needed:
- Replace the example ImageNet classes with your specific classes
The application creates the following table automatically:
CREATE TABLE image_classifications (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
image_path VARCHAR(255),
predicted_class VARCHAR(100),
confidence VARCHAR(10),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);- Frontend: Streamlit
- Backend: FastAPI
- Database: MySQL/SQLite
- ML Framework: PyTorch, TorchVision
- Package Management: UV, pip
- Image Processing: Pillow
- git - the simple guide
- use a different branch to work on a new feature (recommended!)
- FastAPI with SQL Database
- learn to use different SQL databases with FastAPI
Contributions are welcome! Feel free to submit a pull request or open an issue to improve this template.
This project is licensed under the MIT License - see the LICENSE file for details.