Spaces:
Running
Running
Commit
Β·
57860a9
1
Parent(s):
2650203
Added context.txt
Browse files- README.md +8 -303
- context.txt +305 -0
README.md
CHANGED
|
@@ -1,305 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
- **AI Analysis** - Automatic tag and caption generation
|
| 9 |
-
- **Semantic Search** - FAISS-powered similarity search on embeddings
|
| 10 |
-
- **Image-to-Image Search** - Find similar photos to a reference image
|
| 11 |
-
- **RESTful API** - Full REST API with automatic documentation
|
| 12 |
-
- **Docker Support** - Production-ready Docker and Docker Compose setup
|
| 13 |
-
|
| 14 |
-
## π οΈ Tech Stack
|
| 15 |
-
|
| 16 |
-
- **Backend**: FastAPI
|
| 17 |
-
- **Database**: SQLModel + SQLite (PostgreSQL ready)
|
| 18 |
-
- **Search Engine**: FAISS (Fast Approximate Nearest Neighbors)
|
| 19 |
-
- **Image Processing**: Pillow
|
| 20 |
-
- **ORM**: SQLModel
|
| 21 |
-
- **API Documentation**: Swagger/OpenAPI
|
| 22 |
-
|
| 23 |
-
## π Prerequisites
|
| 24 |
-
|
| 25 |
-
- Python 3.10+
|
| 26 |
-
- Docker & Docker Compose (optional)
|
| 27 |
-
- 2GB+ RAM for FAISS index
|
| 28 |
-
|
| 29 |
-
## βοΈ Installation
|
| 30 |
-
|
| 31 |
-
### Local Development
|
| 32 |
-
|
| 33 |
-
1. **Clone and setup**
|
| 34 |
-
```bash
|
| 35 |
-
cd image_embedder
|
| 36 |
-
python -m venv venv
|
| 37 |
-
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 38 |
-
```
|
| 39 |
-
|
| 40 |
-
2. **Install dependencies**
|
| 41 |
-
```bash
|
| 42 |
-
pip install -r requirements.txt
|
| 43 |
-
```
|
| 44 |
-
|
| 45 |
-
3. **Create uploads directory**
|
| 46 |
-
```bash
|
| 47 |
-
mkdir -p uploads
|
| 48 |
-
```
|
| 49 |
-
|
| 50 |
-
4. **Run the server**
|
| 51 |
-
```bash
|
| 52 |
-
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
| 53 |
-
```
|
| 54 |
-
|
| 55 |
-
Server will start at `http://localhost:8000`
|
| 56 |
-
|
| 57 |
-
### Docker
|
| 58 |
-
|
| 59 |
-
```bash
|
| 60 |
-
# Build and run
|
| 61 |
-
docker compose up --build
|
| 62 |
-
|
| 63 |
-
# Run in background
|
| 64 |
-
docker compose up -d
|
| 65 |
-
|
| 66 |
-
# View logs
|
| 67 |
-
docker compose logs -f cloudzy_api
|
| 68 |
-
|
| 69 |
-
# Stop
|
| 70 |
-
docker compose down
|
| 71 |
-
```
|
| 72 |
-
|
| 73 |
-
## π API Endpoints
|
| 74 |
-
|
| 75 |
-
### Upload Photo
|
| 76 |
-
```bash
|
| 77 |
-
POST /upload
|
| 78 |
-
Content-Type: multipart/form-data
|
| 79 |
-
|
| 80 |
-
# Returns:
|
| 81 |
-
{
|
| 82 |
-
"id": 1,
|
| 83 |
-
"filename": "photo_20231023_120000.jpg",
|
| 84 |
-
"tags": ["nature", "landscape", "mountain"],
|
| 85 |
-
"caption": "A beautiful nature photograph",
|
| 86 |
-
"message": "Photo uploaded successfully with ID 1"
|
| 87 |
-
}
|
| 88 |
-
```
|
| 89 |
-
|
| 90 |
-
### Get Photo Metadata
|
| 91 |
-
```bash
|
| 92 |
-
GET /photo/{id}
|
| 93 |
-
|
| 94 |
-
# Returns:
|
| 95 |
-
{
|
| 96 |
-
"id": 1,
|
| 97 |
-
"filename": "photo_20231023_120000.jpg",
|
| 98 |
-
"tags": ["nature", "landscape"],
|
| 99 |
-
"caption": "A beautiful landscape",
|
| 100 |
-
"embedding": [0.123, -0.456, ...], # 512-dim vector
|
| 101 |
-
"created_at": "2023-10-23T12:00:00"
|
| 102 |
-
}
|
| 103 |
-
```
|
| 104 |
-
|
| 105 |
-
### List All Photos
|
| 106 |
-
```bash
|
| 107 |
-
GET /photos?skip=0&limit=10
|
| 108 |
-
|
| 109 |
-
# Returns: List of photo objects with pagination
|
| 110 |
-
```
|
| 111 |
-
|
| 112 |
-
### Semantic Search
|
| 113 |
-
```bash
|
| 114 |
-
GET /search?q=mountain&top_k=5
|
| 115 |
-
|
| 116 |
-
# Returns:
|
| 117 |
-
{
|
| 118 |
-
"query": "mountain",
|
| 119 |
-
"results": [
|
| 120 |
-
{
|
| 121 |
-
"photo_id": 1,
|
| 122 |
-
"filename": "photo_1.jpg",
|
| 123 |
-
"tags": ["nature", "mountain"],
|
| 124 |
-
"caption": "Mountain landscape",
|
| 125 |
-
"distance": 0.123
|
| 126 |
-
},
|
| 127 |
-
...
|
| 128 |
-
],
|
| 129 |
-
"total_results": 5
|
| 130 |
-
}
|
| 131 |
-
```
|
| 132 |
-
|
| 133 |
-
### Image-to-Image Search
|
| 134 |
-
```bash
|
| 135 |
-
POST /search/image-to-image?reference_photo_id=1&top_k=5
|
| 136 |
-
|
| 137 |
-
# Returns similar photos to reference photo 1
|
| 138 |
-
```
|
| 139 |
-
|
| 140 |
-
### Health Check
|
| 141 |
-
```bash
|
| 142 |
-
GET /health
|
| 143 |
-
|
| 144 |
-
# Returns service status and FAISS index stats
|
| 145 |
-
```
|
| 146 |
-
|
| 147 |
-
## π API Documentation
|
| 148 |
-
|
| 149 |
-
**Interactive Docs (Swagger UI)**:
|
| 150 |
-
```
|
| 151 |
-
http://localhost:8000/docs
|
| 152 |
-
```
|
| 153 |
-
|
| 154 |
-
**Alternative Docs (ReDoc)**:
|
| 155 |
-
```
|
| 156 |
-
http://localhost:8000/redoc
|
| 157 |
-
```
|
| 158 |
-
|
| 159 |
-
## ποΈ Project Structure
|
| 160 |
-
|
| 161 |
-
```
|
| 162 |
-
image_embedder/
|
| 163 |
-
βββ app/
|
| 164 |
-
β βββ __init__.py
|
| 165 |
-
β βββ main.py # FastAPI app entry point
|
| 166 |
-
β βββ database.py # SQLModel engine + session
|
| 167 |
-
β βββ models.py # Photo database model
|
| 168 |
-
β βββ schemas.py # Pydantic response models
|
| 169 |
-
β βββ ai_utils.py # AI generation (tags, captions, embeddings)
|
| 170 |
-
β βββ search_engine.py # FAISS index manager
|
| 171 |
-
β β
|
| 172 |
-
β βββ routes/
|
| 173 |
-
β β βββ __init__.py
|
| 174 |
-
β β βββ upload.py # POST /upload endpoint
|
| 175 |
-
β β βββ photo.py # GET /photo/:id and /photos endpoints
|
| 176 |
-
β β βββ search.py # GET /search and image-to-image endpoints
|
| 177 |
-
β β
|
| 178 |
-
β βββ utils/
|
| 179 |
-
β βββ __init__.py
|
| 180 |
-
β βββ file_utils.py # File saving and management
|
| 181 |
-
β
|
| 182 |
-
βββ uploads/ # Stored images (created at runtime)
|
| 183 |
-
βββ faiss_index.bin # FAISS index file (created at runtime)
|
| 184 |
-
βββ photos.db # SQLite database (created at runtime)
|
| 185 |
-
β
|
| 186 |
-
βββ requirements.txt # Python dependencies
|
| 187 |
-
βββ Dockerfile
|
| 188 |
-
βββ docker-compose.yml
|
| 189 |
-
βββ README.md
|
| 190 |
-
```
|
| 191 |
-
|
| 192 |
-
## π Development Workflow
|
| 193 |
-
|
| 194 |
-
### Test Upload
|
| 195 |
-
```bash
|
| 196 |
-
# Use curl
|
| 197 |
-
curl -X POST -F "file=@/path/to/image.jpg" http://localhost:8000/upload
|
| 198 |
-
|
| 199 |
-
# Or use Python
|
| 200 |
-
import requests
|
| 201 |
-
with open("image.jpg", "rb") as f:
|
| 202 |
-
response = requests.post(
|
| 203 |
-
"http://localhost:8000/upload",
|
| 204 |
-
files={"file": f}
|
| 205 |
-
)
|
| 206 |
-
print(response.json())
|
| 207 |
-
```
|
| 208 |
-
|
| 209 |
-
### Test Search
|
| 210 |
-
```bash
|
| 211 |
-
# Query-based search
|
| 212 |
-
curl "http://localhost:8000/search?q=tree&top_k=5"
|
| 213 |
-
|
| 214 |
-
# Image-to-image search
|
| 215 |
-
curl -X POST "http://localhost:8000/search/image-to-image?reference_photo_id=1&top_k=5"
|
| 216 |
-
```
|
| 217 |
-
|
| 218 |
-
### View Database
|
| 219 |
-
```bash
|
| 220 |
-
# Install sqlite3 CLI and view database
|
| 221 |
-
sqlite3 photos.db
|
| 222 |
-
> .tables
|
| 223 |
-
> SELECT * FROM photo;
|
| 224 |
-
> .quit
|
| 225 |
-
```
|
| 226 |
-
|
| 227 |
-
## π§ AI Features (Placeholder Phase)
|
| 228 |
-
|
| 229 |
-
Currently, AI functions use placeholder implementations:
|
| 230 |
-
|
| 231 |
-
- **Tags**: Generated from filename patterns + random selection from common tags
|
| 232 |
-
- **Captions**: Template-based generation from tags
|
| 233 |
-
- **Embeddings**: Deterministic random vectors (reproducible from filename)
|
| 234 |
-
|
| 235 |
-
### Upgrade Path (Production)
|
| 236 |
-
|
| 237 |
-
1. **CLIP Integration** (Recommended)
|
| 238 |
-
- Zero-shot image understanding
|
| 239 |
-
- Excellent for tagging and search
|
| 240 |
-
- ~1-2 sec per image on GPU
|
| 241 |
-
|
| 242 |
-
2. **BLIP Integration** (Alternative)
|
| 243 |
-
- Visual question answering
|
| 244 |
-
- Better captions
|
| 245 |
-
- ~2-3 sec per image on GPU
|
| 246 |
-
|
| 247 |
-
3. **Fine-tuned Models**
|
| 248 |
-
- Train on domain-specific data
|
| 249 |
-
- Improved accuracy
|
| 250 |
-
- Higher latency/complexity
|
| 251 |
-
|
| 252 |
-
## π Performance Considerations
|
| 253 |
-
|
| 254 |
-
- **FAISS Index**: Supports millions of embeddings
|
| 255 |
-
- **Database**: SQLite suitable for 100k+ photos; PostgreSQL for larger scale
|
| 256 |
-
- **Embeddings**: 512-dim vectors (adjustable)
|
| 257 |
-
- **Search**: <100ms for 100k+ embeddings on CPU
|
| 258 |
-
|
| 259 |
-
## π¨ Troubleshooting
|
| 260 |
-
|
| 261 |
-
### FAISS Installation Issues
|
| 262 |
-
```bash
|
| 263 |
-
# If faiss-cpu fails, try:
|
| 264 |
-
pip install faiss-cpu==1.7.4 --no-cache-dir
|
| 265 |
-
```
|
| 266 |
-
|
| 267 |
-
### SQLite Lock Error
|
| 268 |
-
```bash
|
| 269 |
-
# Restart the application or remove locked database
|
| 270 |
-
rm photos.db
|
| 271 |
-
```
|
| 272 |
-
|
| 273 |
-
### Docker Build Issues
|
| 274 |
-
```bash
|
| 275 |
-
# Rebuild without cache
|
| 276 |
-
docker compose build --no-cache
|
| 277 |
-
```
|
| 278 |
-
|
| 279 |
-
## π Security Notes
|
| 280 |
-
|
| 281 |
-
- β οΈ Currently no authentication - add for production
|
| 282 |
-
- β οΈ CORS allows all origins - restrict for production
|
| 283 |
-
- β οΈ File upload validation needed - add size limits
|
| 284 |
-
- β οΈ Use PostgreSQL + proper secrets management for production
|
| 285 |
-
|
| 286 |
-
## π Next Steps
|
| 287 |
-
|
| 288 |
-
1. β
Core backend working
|
| 289 |
-
2. β¬ Add authentication (JWT)
|
| 290 |
-
3. β¬ Implement real AI models (CLIP/BLIP)
|
| 291 |
-
4. β¬ Add background job processing (Celery)
|
| 292 |
-
5. β¬ Frontend dashboard
|
| 293 |
-
6. β¬ Production deployment (Railway/AWS)
|
| 294 |
-
|
| 295 |
-
## π License
|
| 296 |
-
|
| 297 |
-
MIT License
|
| 298 |
-
|
| 299 |
-
## π€ Contributing
|
| 300 |
-
|
| 301 |
-
Contributions welcome! Please test thoroughly before submitting.
|
| 302 |
-
|
| 303 |
---
|
| 304 |
|
| 305 |
-
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Cloudzy Ai Challenge
|
| 3 |
+
emoji: π¦
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π§ Cloudzy AI - Cloud Photo Management Service
|
| 2 |
+
|
| 3 |
+
A FastAPI-based cloud photo management service with AI tagging, captioning, and semantic search using FAISS.
|
| 4 |
+
|
| 5 |
+
## π― Features
|
| 6 |
+
|
| 7 |
+
- **Photo Upload** - Upload images with automatic metadata generation
|
| 8 |
+
- **AI Analysis** - Automatic tag and caption generation
|
| 9 |
+
- **Semantic Search** - FAISS-powered similarity search on embeddings
|
| 10 |
+
- **Image-to-Image Search** - Find similar photos to a reference image
|
| 11 |
+
- **RESTful API** - Full REST API with automatic documentation
|
| 12 |
+
- **Docker Support** - Production-ready Docker and Docker Compose setup
|
| 13 |
+
|
| 14 |
+
## π οΈ Tech Stack
|
| 15 |
+
|
| 16 |
+
- **Backend**: FastAPI
|
| 17 |
+
- **Database**: SQLModel + SQLite (PostgreSQL ready)
|
| 18 |
+
- **Search Engine**: FAISS (Fast Approximate Nearest Neighbors)
|
| 19 |
+
- **Image Processing**: Pillow
|
| 20 |
+
- **ORM**: SQLModel
|
| 21 |
+
- **API Documentation**: Swagger/OpenAPI
|
| 22 |
+
|
| 23 |
+
## π Prerequisites
|
| 24 |
+
|
| 25 |
+
- Python 3.10+
|
| 26 |
+
- Docker & Docker Compose (optional)
|
| 27 |
+
- 2GB+ RAM for FAISS index
|
| 28 |
+
|
| 29 |
+
## βοΈ Installation
|
| 30 |
+
|
| 31 |
+
### Local Development
|
| 32 |
+
|
| 33 |
+
1. **Clone and setup**
|
| 34 |
+
```bash
|
| 35 |
+
cd image_embedder
|
| 36 |
+
python -m venv venv
|
| 37 |
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
2. **Install dependencies**
|
| 41 |
+
```bash
|
| 42 |
+
pip install -r requirements.txt
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
3. **Create uploads directory**
|
| 46 |
+
```bash
|
| 47 |
+
mkdir -p uploads
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
4. **Run the server**
|
| 51 |
+
```bash
|
| 52 |
+
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
Server will start at `http://localhost:8000`
|
| 56 |
+
|
| 57 |
+
### Docker
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
# Build and run
|
| 61 |
+
docker compose up --build
|
| 62 |
+
|
| 63 |
+
# Run in background
|
| 64 |
+
docker compose up -d
|
| 65 |
+
|
| 66 |
+
# View logs
|
| 67 |
+
docker compose logs -f cloudzy_api
|
| 68 |
+
|
| 69 |
+
# Stop
|
| 70 |
+
docker compose down
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## π API Endpoints
|
| 74 |
+
|
| 75 |
+
### Upload Photo
|
| 76 |
+
```bash
|
| 77 |
+
POST /upload
|
| 78 |
+
Content-Type: multipart/form-data
|
| 79 |
+
|
| 80 |
+
# Returns:
|
| 81 |
+
{
|
| 82 |
+
"id": 1,
|
| 83 |
+
"filename": "photo_20231023_120000.jpg",
|
| 84 |
+
"tags": ["nature", "landscape", "mountain"],
|
| 85 |
+
"caption": "A beautiful nature photograph",
|
| 86 |
+
"message": "Photo uploaded successfully with ID 1"
|
| 87 |
+
}
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
### Get Photo Metadata
|
| 91 |
+
```bash
|
| 92 |
+
GET /photo/{id}
|
| 93 |
+
|
| 94 |
+
# Returns:
|
| 95 |
+
{
|
| 96 |
+
"id": 1,
|
| 97 |
+
"filename": "photo_20231023_120000.jpg",
|
| 98 |
+
"tags": ["nature", "landscape"],
|
| 99 |
+
"caption": "A beautiful landscape",
|
| 100 |
+
"embedding": [0.123, -0.456, ...], # 512-dim vector
|
| 101 |
+
"created_at": "2023-10-23T12:00:00"
|
| 102 |
+
}
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
### List All Photos
|
| 106 |
+
```bash
|
| 107 |
+
GET /photos?skip=0&limit=10
|
| 108 |
+
|
| 109 |
+
# Returns: List of photo objects with pagination
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
### Semantic Search
|
| 113 |
+
```bash
|
| 114 |
+
GET /search?q=mountain&top_k=5
|
| 115 |
+
|
| 116 |
+
# Returns:
|
| 117 |
+
{
|
| 118 |
+
"query": "mountain",
|
| 119 |
+
"results": [
|
| 120 |
+
{
|
| 121 |
+
"photo_id": 1,
|
| 122 |
+
"filename": "photo_1.jpg",
|
| 123 |
+
"tags": ["nature", "mountain"],
|
| 124 |
+
"caption": "Mountain landscape",
|
| 125 |
+
"distance": 0.123
|
| 126 |
+
},
|
| 127 |
+
...
|
| 128 |
+
],
|
| 129 |
+
"total_results": 5
|
| 130 |
+
}
|
| 131 |
+
```
|
| 132 |
+
|
| 133 |
+
### Image-to-Image Search
|
| 134 |
+
```bash
|
| 135 |
+
POST /search/image-to-image?reference_photo_id=1&top_k=5
|
| 136 |
+
|
| 137 |
+
# Returns similar photos to reference photo 1
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
### Health Check
|
| 141 |
+
```bash
|
| 142 |
+
GET /health
|
| 143 |
+
|
| 144 |
+
# Returns service status and FAISS index stats
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
## π API Documentation
|
| 148 |
+
|
| 149 |
+
**Interactive Docs (Swagger UI)**:
|
| 150 |
+
```
|
| 151 |
+
http://localhost:8000/docs
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
**Alternative Docs (ReDoc)**:
|
| 155 |
+
```
|
| 156 |
+
http://localhost:8000/redoc
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
## ποΈ Project Structure
|
| 160 |
+
|
| 161 |
+
```
|
| 162 |
+
image_embedder/
|
| 163 |
+
βββ app/
|
| 164 |
+
β βββ __init__.py
|
| 165 |
+
β βββ main.py # FastAPI app entry point
|
| 166 |
+
β βββ database.py # SQLModel engine + session
|
| 167 |
+
β βββ models.py # Photo database model
|
| 168 |
+
β βββ schemas.py # Pydantic response models
|
| 169 |
+
β βββ ai_utils.py # AI generation (tags, captions, embeddings)
|
| 170 |
+
β βββ search_engine.py # FAISS index manager
|
| 171 |
+
β β
|
| 172 |
+
β βββ routes/
|
| 173 |
+
β β βββ __init__.py
|
| 174 |
+
β β βββ upload.py # POST /upload endpoint
|
| 175 |
+
β β βββ photo.py # GET /photo/:id and /photos endpoints
|
| 176 |
+
β β βββ search.py # GET /search and image-to-image endpoints
|
| 177 |
+
β β
|
| 178 |
+
β βββ utils/
|
| 179 |
+
β βββ __init__.py
|
| 180 |
+
β βββ file_utils.py # File saving and management
|
| 181 |
+
β
|
| 182 |
+
βββ uploads/ # Stored images (created at runtime)
|
| 183 |
+
βββ faiss_index.bin # FAISS index file (created at runtime)
|
| 184 |
+
βββ photos.db # SQLite database (created at runtime)
|
| 185 |
+
β
|
| 186 |
+
βββ requirements.txt # Python dependencies
|
| 187 |
+
βββ Dockerfile
|
| 188 |
+
βββ docker-compose.yml
|
| 189 |
+
βββ README.md
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
## π Development Workflow
|
| 193 |
+
|
| 194 |
+
### Test Upload
|
| 195 |
+
```bash
|
| 196 |
+
# Use curl
|
| 197 |
+
curl -X POST -F "file=@/path/to/image.jpg" http://localhost:8000/upload
|
| 198 |
+
|
| 199 |
+
# Or use Python
|
| 200 |
+
import requests
|
| 201 |
+
with open("image.jpg", "rb") as f:
|
| 202 |
+
response = requests.post(
|
| 203 |
+
"http://localhost:8000/upload",
|
| 204 |
+
files={"file": f}
|
| 205 |
+
)
|
| 206 |
+
print(response.json())
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
### Test Search
|
| 210 |
+
```bash
|
| 211 |
+
# Query-based search
|
| 212 |
+
curl "http://localhost:8000/search?q=tree&top_k=5"
|
| 213 |
+
|
| 214 |
+
# Image-to-image search
|
| 215 |
+
curl -X POST "http://localhost:8000/search/image-to-image?reference_photo_id=1&top_k=5"
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
### View Database
|
| 219 |
+
```bash
|
| 220 |
+
# Install sqlite3 CLI and view database
|
| 221 |
+
sqlite3 photos.db
|
| 222 |
+
> .tables
|
| 223 |
+
> SELECT * FROM photo;
|
| 224 |
+
> .quit
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
## π§ AI Features (Placeholder Phase)
|
| 228 |
+
|
| 229 |
+
Currently, AI functions use placeholder implementations:
|
| 230 |
+
|
| 231 |
+
- **Tags**: Generated from filename patterns + random selection from common tags
|
| 232 |
+
- **Captions**: Template-based generation from tags
|
| 233 |
+
- **Embeddings**: Deterministic random vectors (reproducible from filename)
|
| 234 |
+
|
| 235 |
+
### Upgrade Path (Production)
|
| 236 |
+
|
| 237 |
+
1. **CLIP Integration** (Recommended)
|
| 238 |
+
- Zero-shot image understanding
|
| 239 |
+
- Excellent for tagging and search
|
| 240 |
+
- ~1-2 sec per image on GPU
|
| 241 |
+
|
| 242 |
+
2. **BLIP Integration** (Alternative)
|
| 243 |
+
- Visual question answering
|
| 244 |
+
- Better captions
|
| 245 |
+
- ~2-3 sec per image on GPU
|
| 246 |
+
|
| 247 |
+
3. **Fine-tuned Models**
|
| 248 |
+
- Train on domain-specific data
|
| 249 |
+
- Improved accuracy
|
| 250 |
+
- Higher latency/complexity
|
| 251 |
+
|
| 252 |
+
## π Performance Considerations
|
| 253 |
+
|
| 254 |
+
- **FAISS Index**: Supports millions of embeddings
|
| 255 |
+
- **Database**: SQLite suitable for 100k+ photos; PostgreSQL for larger scale
|
| 256 |
+
- **Embeddings**: 512-dim vectors (adjustable)
|
| 257 |
+
- **Search**: <100ms for 100k+ embeddings on CPU
|
| 258 |
+
|
| 259 |
+
## π¨ Troubleshooting
|
| 260 |
+
|
| 261 |
+
### FAISS Installation Issues
|
| 262 |
+
```bash
|
| 263 |
+
# If faiss-cpu fails, try:
|
| 264 |
+
pip install faiss-cpu==1.7.4 --no-cache-dir
|
| 265 |
+
```
|
| 266 |
+
|
| 267 |
+
### SQLite Lock Error
|
| 268 |
+
```bash
|
| 269 |
+
# Restart the application or remove locked database
|
| 270 |
+
rm photos.db
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
### Docker Build Issues
|
| 274 |
+
```bash
|
| 275 |
+
# Rebuild without cache
|
| 276 |
+
docker compose build --no-cache
|
| 277 |
+
```
|
| 278 |
+
|
| 279 |
+
## π Security Notes
|
| 280 |
+
|
| 281 |
+
- β οΈ Currently no authentication - add for production
|
| 282 |
+
- β οΈ CORS allows all origins - restrict for production
|
| 283 |
+
- β οΈ File upload validation needed - add size limits
|
| 284 |
+
- β οΈ Use PostgreSQL + proper secrets management for production
|
| 285 |
+
|
| 286 |
+
## π Next Steps
|
| 287 |
+
|
| 288 |
+
1. β
Core backend working
|
| 289 |
+
2. β¬ Add authentication (JWT)
|
| 290 |
+
3. β¬ Implement real AI models (CLIP/BLIP)
|
| 291 |
+
4. β¬ Add background job processing (Celery)
|
| 292 |
+
5. β¬ Frontend dashboard
|
| 293 |
+
6. β¬ Production deployment (Railway/AWS)
|
| 294 |
+
|
| 295 |
+
## π License
|
| 296 |
+
|
| 297 |
+
MIT License
|
| 298 |
+
|
| 299 |
+
## π€ Contributing
|
| 300 |
+
|
| 301 |
+
Contributions welcome! Please test thoroughly before submitting.
|
| 302 |
+
|
| 303 |
+
---
|
| 304 |
+
|
| 305 |
+
**Questions?** Check the interactive docs at `/docs` or review the code comments.
|