An optimized, high-performance Gradio application for advanced image generation using AWS Nova Canvas. This refactored version provides comprehensive image manipulation capabilities with improved error handling, performance optimizations, and better monitoring.
- Text to Image: Generate images from text prompts
- Inpainting: Modify specific image areas
- Outpainting: Extend image boundaries
- Image Variation: Create image variations
- Image Conditioning: Generate images based on input image and text
- Color Guided Content: Create images using reference color palettes
- Background Removal: Remove image backgrounds
- Health Monitoring: Real-time system health and performance metrics
- AWS credentials configured (AmazonBedrockFullAccess)
- HF Token for NSFW content checking (optional)
- Python >= 3.12
- Docker (for containerized deployment)
git clone <repository-url>
cd canvas-demo
pip install -r requirements.txt
Create a .env
file in the root directory:
# AWS Configuration (Lambda-compatible names)
AMP_AWS_ID=<your-aws-access-key>
AMP_AWS_SECRET=<your-aws-secret-key>
AWS_REGION=us-east-1
BUCKET_REGION=us-west-2
NOVA_IMAGE_BUCKET=<your-bucket-name>
# Optional Features
HF_TOKEN=<huggingface-token>
ENABLE_NSFW_CHECK=true
RATE_LIMIT=20
LOG_LEVEL=INFO
# Lambda Configuration (auto-detected)
AWS_LAMBDA_FUNCTION_NAME=<function-name> # Auto-set in Lambda
AWS_LAMBDA_HTTP_PORT=8080 # Auto-set in Lambda
python app.py
docker build -t canvas-demo.
docker run -p 8080:8080 --env-file .env canvas-demo
The application automatically detects Lambda environment and configures accordingly.
When deployed to Lambda, all application logs are automatically sent to CloudWatch:
- Log Group:
/aws/lambda/canvas-demo
(auto-created by AWS) - Log Stream:
Canvas-Stream
(auto-created by application) - Log Format:
[timestamp] message
- Batching: Logs are batched for performance (10 logs or 30 seconds)
To view logs in AWS Console:
- Navigate to CloudWatch > Log groups
- Select
/aws/lambda/canvas-demo
- Click on
Canvas-Stream
log stream
Access /health
for health status or use the "System Info" tab in the UI.
- Request/error rates
- Memory usage
- Service health status
- Rate limiting statistics
src/
├── models/ # Configuration and data models
├── services/ # Business logic and AWS integrations
├── handlers/ # Request handlers and business operations
└── utils/ # Utilities (logging, exceptions, etc.)
- Config Management:
src/models/config.py
- Centralized configuration - AWS Services:
src/services/aws_client.py
- Optimized AWS client management - Image Processing:
src/services/image_processor.py
- Async image operations - Rate Limiting:
src/services/rate_limiter.py
- Optimized rate limiting - Health Monitoring:
src/handlers/health.py
- System health checks - Canvas Operations:
src/handlers/canvas_handlers.py
- Main business logic
- Model: Amazon Nova Canvas (amazon.nova-canvas-v1:0)
- Prompt Model: Amazon Nova Lite (us.amazon.nova-lite-v1:0)
- Default Resolution: 1024x1024
- Supported Formats: PNG, JPG
- Max Image Size: 4MP (4194304 pixels)
- Rate Limiting: Configurable (default: 20 requests/20min)
- Graceful Degradation: Services continue operating even if non-critical components fail
- User-Friendly Messages: Clear error messages without technical details
- Automatic Retries: Intelligent retry logic for transient failures
- Circuit Breakers: Prevent cascade failures from external services
- Input validation and sanitization
- No credentials in logs
- Secure error handling
- Rate limiting protection
- NSFW content filtering (optional)
Apache 2.0 License
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions, please use the GitHub issue tracker.