This FastAPI-based project manages inspection orders/batches, detects defects in images, and generates inspection reports.
- Orders/Batches Management (create/update/fetch).
- Defect Detection via model inference.
- Visualization: Store defect coordinates, display bounding boxes.
- Reports: Generate summaries of inspections.
- Historical Queries and Data Retention.
- Build the Docker image:
docker-compose build
- Run the Docker container:
docker-compose up
docker-compose --env-file .env.docker up
- Access the API:
http://localhost:8000
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies :
pip install -r requirements.txt
3.Run the server:
bash uvicorn main:app --reload
This project uses Alembic for database migrations to maintain schema consistency across environments.
-
Generate a new migration script:
# Make sure containers are running docker-compose up -d # Create a migration based on model changes docker exec -it image_fastapi-app-1 alembic revision --autogenerate -m "describe your changes"
-
Apply migrations:
docker exec -it image_fastapi-app-1 alembic upgrade head -
Check migration status:
docker exec -it image_fastapi-app-1 alembic current -
Downgrade to a specific version if needed:
docker exec -it image_fastapi-app-1 alembic downgrade <revision_id>
-
Configure environment for RDS:
# Set environment variables or use a .env file for production export DB_HOST=your-rds-endpoint.region.rds.amazonaws.com export DB_USER=db_username export DB_PASSWORD=db_password export DB_NAME=db_name
-
Generate migration script:
# With environment variables configured alembic revision --autogenerate -m "describe your changes"
-
Apply migrations to RDS:
alembic upgrade head
-
Verify migration status:
alembic current
When working with both local Docker and remote RDS environments:
-
Always test migrations locally first before applying to production RDS.
-
Sync environments when out of sync:
# To sync Docker DB with RDS (when RDS is ahead): docker exec -it image_fastapi-app-1 alembic upgrade head # To create missing columns directly with SQL (alternative): docker exec -it image_fastapi-db-1 mysql -u<username> -p<password> <database> -e "ALTER TABLE table_name ADD COLUMN column_name data_type;"
-
Compare schemas to identify differences:
# For Docker database docker exec -it image_fastapi-db-1 mysql -u<username> -p<password> <database> -e "DESCRIBE table_name;" # For RDS (requires mysql client) mysql -h<rds-host> -u<username> -p<password> <database> -e "DESCRIBE table_name;"
This project uses several AWS services:
-
S3 Storage: For storing images and files
- Required permissions: s3:PutObject, s3:GetObject, s3:DeleteObject
- Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, S3_BUCKET_NAME, AWS_S3_BUCKET
-
DynamoDB: For storing image metadata, labels, and predictions
- Required tables: Images, Labels, Predictions
- Environment variables: DYNAMODB_TABLE_NAME, RAW_LABELS_TABLE_NAME
- Run
/api/dynamodb/create-tablesendpoint to initialize tables
The DynamoDB Manager automatically creates required tables with appropriate indexes if they don't exist.
The project uses multiple environment files:
.env.local: For local development.env.production: For production deployment.env.docker: For Docker environment
Required variables:
- Database: DATABASE_URL, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD, MYSQL_ROOT_PASSWORD
- AWS: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, S3_BUCKET_NAME, DYNAMODB_TABLE_NAME, RAW_LABELS_TABLE_NAME
- Authentication: SECRET_KEY
- Email: SMTP_SERVER, SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD
- LLM APIs: OPENAI_API_KEY, CLAUDE_API_KEY, DEEPSEEK_API_KEY, GENEMIS_API_KEY
To migrate between environments:
python migrate.py [local|production|both].
├── main.py # FastAPI entry point and router inclusion
├── orders.py # Endpoints for managing orders/batches
├── defects.py # Endpoints for defect detection and annotation
├── models.py # Database ORM models
├── database.py # Database connection logic
├── schemas.py # Pydantic models and data validation
├── alembic/ # Database migration files
│ ├── versions/ # Migration version scripts
│ └── env.py # Alembic environment configuration
├── Dockerfile # Container definition for consistent deployment
└── docker-compose.yml # Docker Compose configuration for development environment
.
├── [main.py](http://_vscodecontentref_/0) # FastAPI entry point and router registration
├── [models.py](http://_vscodecontentref_/1) # Database ORM models for all entities
├── [schemas.py](http://_vscodecontentref_/2) # Pydantic models for request/response validation
├── [database.py](http://_vscodecontentref_/3) # Database connection and session management
├── [auth.py](http://_vscodecontentref_/4) # Authentication and authorization logic
├── [middleware.py](http://_vscodecontentref_/5) # Custom middleware including token verification
├── [dynamodb_manager.py](http://_vscodecontentref_/6) # AWS DynamoDB integration for NoSQL data
├── routers/ # API routes organized by domain
│ ├── [users.py](http://_vscodecontentref_/7) # User management endpoints
│ ├── [predictions.py](http://_vscodecontentref_/8) # Image upload, project, and prediction endpoints
│ ├── reports.py # Reporting and analytics endpoints
│ └── llms/ # Large Language Model integrations
│ ├── [gpts.py](http://_vscodecontentref_/9) # OpenAI GPT integration
│ ├── claudes.py # Anthropic Claude integration
│ ├── [deepseeks.py](http://_vscodecontentref_/10) # DeepSeek integration
│ └── [configs.py](http://_vscodecontentref_/11) # LLM configuration management
├── alembic/ # Database migration infrastructure
│ └── versions/ # Migration version scripts
├── migrations/ # Migration scripts
├── tests/ # Unit and integration tests
└── [docker-compose.yml](http://_vscodecontentref_/12) # Docker configuration
When running the server, access the interactive API documentation at:
- Swagger UI: http://localhost:3000/docs
- ReDoc: http://localhost:3000/redoc
The API is organized into these main areas:
/api/users/: User management and authentication/api/predictions/: Projects, images, and annotations/api/reports/: Analysis and report generation/api/llms/: AI-assisted functionality (GPT, Claude, DeepSeek)
The project uses pytest for testing with environment isolation:
# Run all tests
python -m pytest
# Run with coverage
python -m pytest --cov=. --cov-report=xml tests/
# Run specific test files
python -m pytest tests/test_users.py-
Fork and clone the repository:
git clone https://github.com/your-username/image_fastapi.git cd image_fastapi -
Create a new branch with your feature/fix:
git checkout -b feature-or-fix-name
-
Make your changes and commit:
git add . git commit -m "Describe your changes"
-
Submit a pull request: Go to the repository on GitHub and create a pull request.