|
| 1 | +# PhotoMapAI - GitHub Copilot Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +PhotoMapAI is an AI-powered image browser and search tool that enables semantic search, clustering, and visualization of large photo collections. It uses the CLIP computer vision model for text and image-based search, and provides a unique "semantic map" that clusters and visualizes images by their content. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +### Tech Stack |
| 10 | +- **Frontend**: HTML, CSS, JavaScript (ES6 modules), Swiper.js, Plotly.js |
| 11 | +- **Backend**: Python 3.10-3.13, FastAPI, Uvicorn |
| 12 | +- **AI/ML**: CLIP (clip-anytorch), PyTorch, scikit-learn, UMAP |
| 13 | +- **Image Processing**: Pillow, pillow-heif |
| 14 | +- **Testing**: pytest (Python), Jest (JavaScript) |
| 15 | + |
| 16 | +### Project Structure |
| 17 | +``` |
| 18 | +PhotoMapAI/ |
| 19 | +├── photomap/ |
| 20 | +│ ├── backend/ # FastAPI backend, routers, and business logic |
| 21 | +│ │ ├── routers/ # API endpoints (album, search, umap, index, etc.) |
| 22 | +│ │ ├── metadata_modules/ # Metadata extraction modules |
| 23 | +│ │ └── *.py # Core backend modules |
| 24 | +│ └── frontend/ # Frontend assets |
| 25 | +│ ├── static/ # JavaScript, CSS, images |
| 26 | +│ └── templates/ # Jinja2 HTML templates |
| 27 | +├── tests/ |
| 28 | +│ ├── backend/ # pytest tests for Python backend |
| 29 | +│ └── frontend/ # Jest tests for JavaScript |
| 30 | +├── docs/ # MkDocs documentation |
| 31 | +└── docker/ # Docker configurations |
| 32 | +``` |
| 33 | + |
| 34 | +## Development Guidelines |
| 35 | + |
| 36 | +### Code Style and Conventions |
| 37 | + |
| 38 | +#### Python |
| 39 | +- Use type hints for function parameters and return values |
| 40 | +- Follow PEP 8 style guidelines |
| 41 | +- Use docstrings for modules, classes, and functions |
| 42 | +- Prefer f-strings for string formatting |
| 43 | +- Use pathlib.Path for file path operations instead of os.path |
| 44 | +- Import organization: standard library → third-party → local imports |
| 45 | + |
| 46 | +#### JavaScript |
| 47 | +- Use ES6 modules with explicit imports/exports |
| 48 | +- Use const/let instead of var |
| 49 | +- Use descriptive function and variable names |
| 50 | +- Add comments for complex logic, especially in event handlers |
| 51 | +- Follow existing file structure: one module per file with clear responsibilities |
| 52 | + |
| 53 | +### Testing |
| 54 | + |
| 55 | +#### Python Tests (pytest) |
| 56 | +- Location: `tests/backend/` |
| 57 | +- Run with: `pytest tests` or `make test` |
| 58 | +- Use fixtures defined in `fixtures.py` for common test setup |
| 59 | +- Test naming: `test_<functionality>.py` and `test_<feature_name>()` |
| 60 | +- Always test API endpoints with the FastAPI test client |
| 61 | + |
| 62 | +#### JavaScript Tests (Jest) |
| 63 | +- Location: `tests/frontend/` |
| 64 | +- Run with: `npm test` or `make test` |
| 65 | +- Use Jest with jsdom environment |
| 66 | +- Test DOM interactions and event handlers |
| 67 | + |
| 68 | +### Building and Running |
| 69 | + |
| 70 | +#### Development Setup |
| 71 | +```bash |
| 72 | +# Install development dependencies |
| 73 | +pip install -e .[testing,development] |
| 74 | + |
| 75 | +# Install JavaScript dependencies |
| 76 | +npm install |
| 77 | + |
| 78 | +# Run tests |
| 79 | +make test |
| 80 | + |
| 81 | +# Build documentation |
| 82 | +make docs |
| 83 | + |
| 84 | +# Build package |
| 85 | +make build |
| 86 | +``` |
| 87 | + |
| 88 | +#### Running the Application |
| 89 | +```bash |
| 90 | +# Start the PhotoMapAI server |
| 91 | +start_photomap |
| 92 | + |
| 93 | +# Server runs at http://localhost:8050 |
| 94 | +``` |
| 95 | + |
| 96 | +### API Structure |
| 97 | + |
| 98 | +The backend uses FastAPI with modular routers: |
| 99 | +- `/api/albums/` - Album management and configuration |
| 100 | +- `/api/search/` - Image search (similarity, text, metadata) |
| 101 | +- `/api/umap/` - UMAP visualization data |
| 102 | +- `/api/index/` - Image indexing and embedding generation |
| 103 | +- `/api/curation/` - Image curation tools |
| 104 | +- `/api/filetree/` - File system navigation |
| 105 | +- `/api/upgrade/` - Application upgrade and version management |
| 106 | + |
| 107 | +### Configuration Management |
| 108 | + |
| 109 | +- Configuration stored in YAML files managed by `config.py` |
| 110 | +- Albums are defined with paths, embeddings, and metadata |
| 111 | +- Use `get_config_manager()` to access the singleton ConfigManager instance |
| 112 | +- Configuration includes album settings, UMAP parameters, and application preferences |
| 113 | + |
| 114 | +### Important Patterns |
| 115 | + |
| 116 | +#### Backend |
| 117 | +- Use FastAPI dependency injection for shared resources |
| 118 | +- Routers should be modular and focused on specific functionality |
| 119 | +- Use Pydantic models for request/response validation |
| 120 | +- Log important operations with Python's logging module |
| 121 | +- Handle file paths with pathlib.Path |
| 122 | +- Use the ConfigManager singleton pattern via `get_config_manager()` for accessing application configuration |
| 123 | + |
| 124 | +#### Frontend |
| 125 | +- State management: Use the centralized `state.js` module for application state |
| 126 | +- Event handling: Register global keyboard shortcuts in `events.js` |
| 127 | +- Modular design: Each feature (UMAP, slideshow, metadata) has its own module |
| 128 | +- Use localStorage for persisting user preferences |
| 129 | +- Use sessionStorage for temporary state during navigation |
| 130 | + |
| 131 | +### Dependencies and Security |
| 132 | + |
| 133 | +- Keep dependencies minimal and well-maintained |
| 134 | +- Use `pillow-heif` for HEIC image support |
| 135 | +- Pin setuptools<67 to avoid CLIP deprecation warnings |
| 136 | +- All image processing is local; no data sent to external services |
| 137 | + |
| 138 | +### Documentation |
| 139 | + |
| 140 | +- Use MkDocs with Material theme for documentation |
| 141 | +- Documentation location: `docs/` |
| 142 | +- Build docs: `make docs` |
| 143 | +- Deploy docs: `make deploy-docs` (GitHub Pages) |
| 144 | +- Include developer documentation in `docs/developer/` |
| 145 | + |
| 146 | +### Common Tasks |
| 147 | + |
| 148 | +#### Adding a New API Endpoint |
| 149 | +1. Create or update a router in `photomap/backend/routers/` |
| 150 | +2. Use Pydantic models for request/response validation |
| 151 | +3. Add appropriate error handling |
| 152 | +4. Include the router in `photomap_server.py` |
| 153 | +5. Add tests in `tests/backend/` |
| 154 | + |
| 155 | +#### Adding Frontend Features |
| 156 | +1. Create a new module in `photomap/frontend/static/javascript/` |
| 157 | +2. Export functions that other modules can import |
| 158 | +3. Update `events.js` if adding keyboard shortcuts |
| 159 | +4. Test with Jest in `tests/frontend/` |
| 160 | +5. Update state.js if managing new application state |
| 161 | + |
| 162 | +#### Working with Images |
| 163 | +- Use PIL/Pillow for image operations |
| 164 | +- Generate thumbnails for performance |
| 165 | +- Support HEIC format via pillow-heif |
| 166 | +- Store embeddings in .npz format (NumPy) |
| 167 | + |
| 168 | +### Performance Considerations |
| 169 | + |
| 170 | +- Frontend: Lazy load images, use thumbnails for grid views |
| 171 | +- Backend: Cache embeddings, use async operations where appropriate |
| 172 | +- UMAP: Pre-compute and cache UMAP projections per album |
| 173 | +- Search: Use efficient similarity search with pre-computed embeddings |
| 174 | + |
| 175 | +### Debugging |
| 176 | + |
| 177 | +- Backend logs: Use Python logging module with appropriate levels |
| 178 | +- Frontend: Use browser DevTools console |
| 179 | +- Test specific functionality in isolation before integration |
| 180 | +- Check GitHub Actions for CI/CD test results |
| 181 | + |
| 182 | +## Additional Resources |
| 183 | + |
| 184 | +- [User Guide](https://lstein.github.io/PhotoMapAI/user-guide/basic-usage/) |
| 185 | +- [Developer Architecture Guide](https://lstein.github.io/PhotoMapAI/developer/architecture/) |
| 186 | +- [Installation Guide](https://lstein.github.io/PhotoMapAI/installation/) |
| 187 | +- [Contributing Guide](CONTRIBUTING.md) |
0 commit comments