Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

343 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

image_fastapi

This FastAPI-based project manages inspection orders/batches, detects defects in images, and generates inspection reports.

Features

  1. Orders/Batches Management (create/update/fetch).
  2. Defect Detection via model inference.
  3. Visualization: Store defect coordinates, display bounding boxes.
  4. Reports: Generate summaries of inspections.
  5. Historical Queries and Data Retention.

Running the Project

Using Docker

  1. Build the Docker image:
    docker-compose build
  2. Run the Docker container:
    docker-compose up
    docker-compose --env-file .env.docker up
  3. Access the API:
    http://localhost:8000

Using a Virtual Environment

  1. Create and activate a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
  2. Install dependencies :

    pip install -r requirements.txt

3.Run the server: bash uvicorn main:app --reload

Database Migrations

This project uses Alembic for database migrations to maintain schema consistency across environments.

Docker Environment Migrations

  1. 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"
  2. Apply migrations:

    docker exec -it image_fastapi-app-1 alembic upgrade head
  3. Check migration status:

    docker exec -it image_fastapi-app-1 alembic current
  4. Downgrade to a specific version if needed:

    docker exec -it image_fastapi-app-1 alembic downgrade <revision_id>

Remote RDS Migrations

  1. 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
  2. Generate migration script:

    # With environment variables configured
    alembic revision --autogenerate -m "describe your changes"
  3. Apply migrations to RDS:

    alembic upgrade head
  4. Verify migration status:

    alembic current

Handling Environment Differences

When working with both local Docker and remote RDS environments:

  1. Always test migrations locally first before applying to production RDS.

  2. 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;"
  3. 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;"

AWS Services Configuration

This project uses several AWS services:

  1. 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
  2. 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-tables endpoint to initialize tables

The DynamoDB Manager automatically creates required tables with appropriate indexes if they don't exist.

Environment Variables

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]

File Structure

.
├── 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

Project Structure

.
├── [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

API Documentation

When running the server, access the interactive API documentation at:

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)

Testing

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

Contributing

  1. Fork and clone the repository:

    git clone https://github.com/your-username/image_fastapi.git
    cd image_fastapi
  2. Create a new branch with your feature/fix:

    git checkout -b feature-or-fix-name
  3. Make your changes and commit:

    git add .
    git commit -m "Describe your changes"
  4. Submit a pull request: Go to the repository on GitHub and create a pull request.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages