Spaces:
Sleeping
Sleeping
Upload 11 files
Browse files- .dockerignore +23 -0
- .gitignore +34 -0
- CHECKLIST.md +131 -0
- DEPLOYMENT_GUIDE.md +161 -0
- Dockerfile +32 -0
- LICENSE +21 -0
- QUICK_START.md +154 -0
- README.md +107 -0
- START_HERE.md +199 -0
- main.py +15 -0
- requirements.txt +9 -0
.dockerignore
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
.Python
|
| 6 |
+
*.so
|
| 7 |
+
*.egg
|
| 8 |
+
*.egg-info/
|
| 9 |
+
dist/
|
| 10 |
+
build/
|
| 11 |
+
.env
|
| 12 |
+
.venv
|
| 13 |
+
venv/
|
| 14 |
+
ENV/
|
| 15 |
+
.git/
|
| 16 |
+
.gitignore
|
| 17 |
+
*.md
|
| 18 |
+
!README.md
|
| 19 |
+
.DS_Store
|
| 20 |
+
media/
|
| 21 |
+
*.db
|
| 22 |
+
*.db-journal
|
| 23 |
+
*.log
|
.gitignore
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
*$py.class
|
| 4 |
+
*.so
|
| 5 |
+
.Python
|
| 6 |
+
build/
|
| 7 |
+
develop-eggs/
|
| 8 |
+
dist/
|
| 9 |
+
downloads/
|
| 10 |
+
eggs/
|
| 11 |
+
.eggs/
|
| 12 |
+
lib/
|
| 13 |
+
lib64/
|
| 14 |
+
parts/
|
| 15 |
+
sdist/
|
| 16 |
+
var/
|
| 17 |
+
wheels/
|
| 18 |
+
*.egg-info/
|
| 19 |
+
.installed.cfg
|
| 20 |
+
*.egg
|
| 21 |
+
|
| 22 |
+
.env
|
| 23 |
+
.venv
|
| 24 |
+
env/
|
| 25 |
+
venv/
|
| 26 |
+
ENV/
|
| 27 |
+
env.bak/
|
| 28 |
+
venv.bak/
|
| 29 |
+
|
| 30 |
+
media/
|
| 31 |
+
*.db
|
| 32 |
+
*.db-journal
|
| 33 |
+
*.log
|
| 34 |
+
.DS_Store
|
CHECKLIST.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π― Deployment Checklist
|
| 2 |
+
|
| 3 |
+
Use this checklist to ensure a smooth deployment to Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## β Pre-Deployment
|
| 6 |
+
|
| 7 |
+
- [ ] Have a HuggingFace account (create at https://huggingface.co/join)
|
| 8 |
+
- [ ] Git is installed on your machine
|
| 9 |
+
- [ ] All files in `huggingface_deployment` folder are present:
|
| 10 |
+
- [ ] `app/` folder with all subfolders
|
| 11 |
+
- [ ] `main.py`
|
| 12 |
+
- [ ] `Dockerfile`
|
| 13 |
+
- [ ] `requirements.txt`
|
| 14 |
+
- [ ] `README.md` (with YAML frontmatter)
|
| 15 |
+
- [ ] `.dockerignore`
|
| 16 |
+
- [ ] `.gitignore`
|
| 17 |
+
- [ ] `LICENSE`
|
| 18 |
+
|
| 19 |
+
## β Create Space
|
| 20 |
+
|
| 21 |
+
- [ ] Go to https://huggingface.co/new-space
|
| 22 |
+
- [ ] Choose a name for your Space
|
| 23 |
+
- [ ] Select **Docker** as SDK
|
| 24 |
+
- [ ] Choose license: **MIT**
|
| 25 |
+
- [ ] Select visibility: Public or Private
|
| 26 |
+
- [ ] Click "Create Space"
|
| 27 |
+
|
| 28 |
+
## β Upload Files
|
| 29 |
+
|
| 30 |
+
### Option A: Web UI (Drag & Drop)
|
| 31 |
+
- [ ] Open your new Space page
|
| 32 |
+
- [ ] Click "Files" tab
|
| 33 |
+
- [ ] Drag and drop ALL files from `huggingface_deployment` folder
|
| 34 |
+
- [ ] Wait for upload to complete
|
| 35 |
+
|
| 36 |
+
### Option B: Git (Recommended)
|
| 37 |
+
- [ ] Clone your Space: `git clone https://huggingface.co/spaces/USERNAME/SPACENAME`
|
| 38 |
+
- [ ] Copy all files from `huggingface_deployment` to cloned folder
|
| 39 |
+
- [ ] `git add .`
|
| 40 |
+
- [ ] `git commit -m "Initial deployment"`
|
| 41 |
+
- [ ] `git push`
|
| 42 |
+
|
| 43 |
+
## β Monitor Build
|
| 44 |
+
|
| 45 |
+
- [ ] Go to your Space page
|
| 46 |
+
- [ ] Click "Logs" tab
|
| 47 |
+
- [ ] Watch the build progress
|
| 48 |
+
- [ ] Wait for "Running" status (~5-10 minutes)
|
| 49 |
+
- [ ] Check for any error messages
|
| 50 |
+
|
| 51 |
+
## β Test Deployment
|
| 52 |
+
|
| 53 |
+
- [ ] Note your Space URL: `https://USERNAME-SPACENAME.hf.space`
|
| 54 |
+
- [ ] Test health endpoint:
|
| 55 |
+
```bash
|
| 56 |
+
curl https://USERNAME-SPACENAME.hf.space/
|
| 57 |
+
```
|
| 58 |
+
- [ ] Expected response: `{"status":"ok"}`
|
| 59 |
+
- [ ] Test with a simple upload (use Postman, curl, or your frontend)
|
| 60 |
+
|
| 61 |
+
## β Configure (Optional)
|
| 62 |
+
|
| 63 |
+
### Hardware Upgrade
|
| 64 |
+
- [ ] Go to Space Settings
|
| 65 |
+
- [ ] Click "Change hardware"
|
| 66 |
+
- [ ] Select desired tier (T4 GPU recommended for production)
|
| 67 |
+
- [ ] Confirm upgrade
|
| 68 |
+
|
| 69 |
+
### Persistent Storage
|
| 70 |
+
- [ ] Go to Space Settings
|
| 71 |
+
- [ ] Click "Upgrade storage"
|
| 72 |
+
- [ ] Select storage tier (Small = $5/month for 20GB)
|
| 73 |
+
- [ ] Confirm upgrade
|
| 74 |
+
|
| 75 |
+
### Environment Variables
|
| 76 |
+
- [ ] Go to Space Settings
|
| 77 |
+
- [ ] Add any required secrets/variables
|
| 78 |
+
- [ ] Save changes
|
| 79 |
+
|
| 80 |
+
## β Connect Frontend
|
| 81 |
+
|
| 82 |
+
- [ ] Update frontend API URL to: `https://USERNAME-SPACENAME.hf.space`
|
| 83 |
+
- [ ] Test end-to-end flow:
|
| 84 |
+
- [ ] Upload images
|
| 85 |
+
- [ ] Start processing
|
| 86 |
+
- [ ] Check progress
|
| 87 |
+
- [ ] Download results
|
| 88 |
+
|
| 89 |
+
## β Final Checks
|
| 90 |
+
|
| 91 |
+
- [ ] Space is publicly accessible (if intended)
|
| 92 |
+
- [ ] API responds to all endpoints
|
| 93 |
+
- [ ] Image upload works
|
| 94 |
+
- [ ] Processing completes successfully
|
| 95 |
+
- [ ] Download returns ZIP file
|
| 96 |
+
- [ ] Cleanup happens after download
|
| 97 |
+
|
| 98 |
+
## β Documentation
|
| 99 |
+
|
| 100 |
+
- [ ] Update your frontend repo with new API URL
|
| 101 |
+
- [ ] Share your Space link with users
|
| 102 |
+
- [ ] Add Space badge to your GitHub README (optional):
|
| 103 |
+
```markdown
|
| 104 |
+
[](https://huggingface.co/spaces/USERNAME/SPACENAME)
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
## β Monitoring
|
| 108 |
+
|
| 109 |
+
- [ ] Check Space analytics (views, usage)
|
| 110 |
+
- [ ] Monitor logs for errors
|
| 111 |
+
- [ ] Watch resource usage in Settings
|
| 112 |
+
- [ ] Set up billing alerts (if using paid tier)
|
| 113 |
+
|
| 114 |
+
---
|
| 115 |
+
|
| 116 |
+
## π Deployment Complete!
|
| 117 |
+
|
| 118 |
+
Once all items are checked, your Image Selector Backend is live on HuggingFace Spaces!
|
| 119 |
+
|
| 120 |
+
**Your API**: `https://USERNAME-SPACENAME.hf.space`
|
| 121 |
+
|
| 122 |
+
**Your Space**: `https://huggingface.co/spaces/USERNAME/SPACENAME`
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## π Need Help?
|
| 127 |
+
|
| 128 |
+
- Check `DEPLOYMENT_GUIDE.md` for detailed instructions
|
| 129 |
+
- Check `QUICK_START.md` for overview
|
| 130 |
+
- HF Community: https://huggingface.co/spaces
|
| 131 |
+
- HF Discord: https://discord.gg/hugging-face
|
DEPLOYMENT_GUIDE.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π Deploying to Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
This guide walks you through deploying the Image Selector Backend to Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## π Prerequisites
|
| 6 |
+
|
| 7 |
+
- A Hugging Face account (free): https://huggingface.co/join
|
| 8 |
+
- Git installed on your machine
|
| 9 |
+
|
| 10 |
+
## π― Deployment Steps
|
| 11 |
+
|
| 12 |
+
### 1. Create a New Space
|
| 13 |
+
|
| 14 |
+
1. Go to https://huggingface.co/new-space
|
| 15 |
+
2. Fill in the details:
|
| 16 |
+
- **Space name**: `image-selector-backend` (or your preferred name)
|
| 17 |
+
- **License**: MIT
|
| 18 |
+
- **Select the Space SDK**: Choose **Docker**
|
| 19 |
+
- **Space hardware**: Start with **CPU basic** (free), upgrade to **GPU** later if needed
|
| 20 |
+
- **Visibility**: Public or Private (your choice)
|
| 21 |
+
3. Click **Create Space**
|
| 22 |
+
|
| 23 |
+
### 2. Clone Your New Space Repository
|
| 24 |
+
|
| 25 |
+
After creating the Space, you'll see instructions to clone it. Run:
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 29 |
+
cd YOUR_SPACE_NAME
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
### 3. Copy Files from This Folder
|
| 33 |
+
|
| 34 |
+
Copy **ALL** files and folders from this `huggingface_deployment` directory to your cloned Space repository:
|
| 35 |
+
|
| 36 |
+
```bash
|
| 37 |
+
# Windows PowerShell
|
| 38 |
+
Copy-Item -Path ".\huggingface_deployment\*" -Destination "PATH_TO_YOUR_CLONED_SPACE\" -Recurse -Force
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
Or manually copy these files/folders:
|
| 42 |
+
- `app/` (entire folder with all subfolders)
|
| 43 |
+
- `main.py`
|
| 44 |
+
- `requirements.txt`
|
| 45 |
+
- `Dockerfile`
|
| 46 |
+
- `README.md`
|
| 47 |
+
- `.dockerignore`
|
| 48 |
+
- `.gitignore`
|
| 49 |
+
|
| 50 |
+
### 4. Push to Hugging Face
|
| 51 |
+
|
| 52 |
+
```bash
|
| 53 |
+
cd PATH_TO_YOUR_CLONED_SPACE
|
| 54 |
+
git add .
|
| 55 |
+
git commit -m "Initial deployment of Image Selector Backend"
|
| 56 |
+
git push
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### 5. Wait for Build
|
| 60 |
+
|
| 61 |
+
- Hugging Face will automatically build your Docker container
|
| 62 |
+
- Go to your Space page to see the build logs
|
| 63 |
+
- First build takes ~5-10 minutes (downloads Python packages and models)
|
| 64 |
+
- Once built, your API will be live! π
|
| 65 |
+
|
| 66 |
+
### 6. Test Your Deployment
|
| 67 |
+
|
| 68 |
+
Your API will be available at:
|
| 69 |
+
```
|
| 70 |
+
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
Test the health endpoint:
|
| 74 |
+
```bash
|
| 75 |
+
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
Should return: `{"status":"ok"}`
|
| 79 |
+
|
| 80 |
+
## π§ Configuration Options
|
| 81 |
+
|
| 82 |
+
### Hardware Upgrades
|
| 83 |
+
|
| 84 |
+
For better performance, upgrade your Space hardware:
|
| 85 |
+
- **CPU Basic** (Free): Good for testing, slow processing
|
| 86 |
+
- **T4 Small** ($0.60/hr): 16GB GPU, much faster processing
|
| 87 |
+
- **A10G Small** ($1.05/hr): 24GB GPU, even faster
|
| 88 |
+
|
| 89 |
+
To upgrade:
|
| 90 |
+
1. Go to your Space Settings
|
| 91 |
+
2. Click "Change hardware"
|
| 92 |
+
3. Select desired tier
|
| 93 |
+
|
| 94 |
+
### Persistent Storage
|
| 95 |
+
|
| 96 |
+
By default, Spaces use ephemeral storage (resets on restart).
|
| 97 |
+
|
| 98 |
+
To keep data across restarts:
|
| 99 |
+
1. Go to Space Settings
|
| 100 |
+
2. Upgrade storage tier (starting at $5/month for 20GB)
|
| 101 |
+
3. Your app will automatically use `/data` directory
|
| 102 |
+
|
| 103 |
+
### Environment Variables / Secrets
|
| 104 |
+
|
| 105 |
+
If you need API keys or secrets:
|
| 106 |
+
1. Go to Space Settings
|
| 107 |
+
2. Add Variables (public) or Secrets (private)
|
| 108 |
+
3. Access in code via `os.environ.get("SECRET_NAME")`
|
| 109 |
+
|
| 110 |
+
## π¨ Connect Your Frontend
|
| 111 |
+
|
| 112 |
+
Update your frontend to point to your Space URL:
|
| 113 |
+
|
| 114 |
+
```javascript
|
| 115 |
+
const API_URL = "https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space";
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
Frontend repo: https://github.com/basilbenny1002/image-selector-front-end
|
| 119 |
+
|
| 120 |
+
## π Monitor Your Space
|
| 121 |
+
|
| 122 |
+
- View logs: Click "Logs" tab in your Space
|
| 123 |
+
- See resource usage: Settings β Hardware
|
| 124 |
+
- Track visitors: Your Space page shows view count
|
| 125 |
+
|
| 126 |
+
## π Troubleshooting
|
| 127 |
+
|
| 128 |
+
### Build Fails
|
| 129 |
+
- Check logs for specific error
|
| 130 |
+
- Verify all files copied correctly
|
| 131 |
+
- Ensure Dockerfile syntax is correct
|
| 132 |
+
|
| 133 |
+
### API Not Responding
|
| 134 |
+
- Check if Space is running (not sleeping)
|
| 135 |
+
- Free tier Spaces sleep after inactivity
|
| 136 |
+
- Upgrade to paid tier for always-on
|
| 137 |
+
|
| 138 |
+
### Out of Memory
|
| 139 |
+
- Large images or many simultaneous users
|
| 140 |
+
- Upgrade to larger CPU tier or add GPU
|
| 141 |
+
|
| 142 |
+
### Models Not Downloading
|
| 143 |
+
- First request is slow (downloads ~500MB models)
|
| 144 |
+
- Subsequent requests use cached models
|
| 145 |
+
- Consider using persistent storage to keep models
|
| 146 |
+
|
| 147 |
+
## π Useful Resources
|
| 148 |
+
|
| 149 |
+
- [HF Spaces Docs](https://huggingface.co/docs/hub/spaces)
|
| 150 |
+
- [Docker Spaces Guide](https://huggingface.co/docs/hub/spaces-sdks-docker)
|
| 151 |
+
- [FastAPI Docs](https://fastapi.tiangolo.com/)
|
| 152 |
+
|
| 153 |
+
## π You're Done!
|
| 154 |
+
|
| 155 |
+
Your Image Selector Backend is now live on Hugging Face Spaces!
|
| 156 |
+
|
| 157 |
+
Share your Space:
|
| 158 |
+
- Direct link: `https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME`
|
| 159 |
+
- API endpoint: `https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space`
|
| 160 |
+
|
| 161 |
+
Happy deploying! π
|
Dockerfile
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python 3.10 slim image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set up a new user named "user" with user ID 1000
|
| 5 |
+
RUN useradd -m -u 1000 user
|
| 6 |
+
|
| 7 |
+
# Switch to the "user" user
|
| 8 |
+
USER user
|
| 9 |
+
|
| 10 |
+
# Set home to the user's home directory
|
| 11 |
+
ENV HOME=/home/user \
|
| 12 |
+
PATH=/home/user/.local/bin:$PATH \
|
| 13 |
+
PYTHONUNBUFFERED=1
|
| 14 |
+
|
| 15 |
+
# Set the working directory to the user's home directory
|
| 16 |
+
WORKDIR $HOME/app
|
| 17 |
+
|
| 18 |
+
# Copy requirements first for better caching
|
| 19 |
+
COPY --chown=user requirements.txt .
|
| 20 |
+
|
| 21 |
+
# Upgrade pip and install dependencies
|
| 22 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 23 |
+
pip install --no-cache-dir -r requirements.txt
|
| 24 |
+
|
| 25 |
+
# Copy the application code
|
| 26 |
+
COPY --chown=user . .
|
| 27 |
+
|
| 28 |
+
# Expose port 7860 (HuggingFace Spaces default)
|
| 29 |
+
EXPOSE 7860
|
| 30 |
+
|
| 31 |
+
# Run the FastAPI app with uvicorn
|
| 32 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Basil Benny
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
QUICK_START.md
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π¦ HuggingFace Deployment Package - Ready to Upload!
|
| 2 |
+
|
| 3 |
+
## β
What's Been Created
|
| 4 |
+
|
| 5 |
+
This folder contains everything you need to deploy your Image Selector Backend to Hugging Face Spaces.
|
| 6 |
+
|
| 7 |
+
### π Folder Structure
|
| 8 |
+
|
| 9 |
+
```
|
| 10 |
+
huggingface_deployment/
|
| 11 |
+
βββ app/ # Application code
|
| 12 |
+
β βββ __init__.py
|
| 13 |
+
β βββ api/
|
| 14 |
+
β β βββ __init__.py
|
| 15 |
+
β β βββ routes.py # FastAPI endpoints
|
| 16 |
+
β βββ core/
|
| 17 |
+
β β βββ __init__.py
|
| 18 |
+
β β βββ config.py # Settings (modified for HF)
|
| 19 |
+
β βββ repositories/
|
| 20 |
+
β β βββ __init__.py
|
| 21 |
+
β β βββ embeddings.py # SQLite operations
|
| 22 |
+
β βββ services/
|
| 23 |
+
β βββ __init__.py
|
| 24 |
+
β βββ image_selector.py # ML logic
|
| 25 |
+
βββ main.py # FastAPI app entry point
|
| 26 |
+
βββ Dockerfile # Docker container config
|
| 27 |
+
βββ requirements.txt # Python dependencies
|
| 28 |
+
βββ README.md # HF Space description (with YAML!)
|
| 29 |
+
βββ LICENSE # MIT License
|
| 30 |
+
βββ .dockerignore # Files to exclude from Docker
|
| 31 |
+
βββ .gitignore # Git ignore rules
|
| 32 |
+
βββ DEPLOYMENT_GUIDE.md # Step-by-step deployment instructions
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
## π Key Modifications for HuggingFace Spaces
|
| 36 |
+
|
| 37 |
+
### 1. **config.py** - Smart Storage
|
| 38 |
+
- Auto-detects `/data` for persistent storage (paid tier)
|
| 39 |
+
- Falls back to `/tmp` for ephemeral storage (free tier)
|
| 40 |
+
- Creates directories automatically
|
| 41 |
+
|
| 42 |
+
### 2. **Dockerfile** - HF Optimized
|
| 43 |
+
- Uses non-root user (user ID 1000) as required by HF
|
| 44 |
+
- Exposes port 7860 (HF default)
|
| 45 |
+
- Proper file ownership with `--chown=user`
|
| 46 |
+
- Optimized layer caching
|
| 47 |
+
|
| 48 |
+
### 3. **README.md** - HF Format
|
| 49 |
+
- Includes YAML frontmatter with Space metadata:
|
| 50 |
+
```yaml
|
| 51 |
+
title: Image Selector Backend
|
| 52 |
+
emoji: πΌοΈ
|
| 53 |
+
sdk: docker
|
| 54 |
+
app_port: 7860
|
| 55 |
+
license: mit
|
| 56 |
+
```
|
| 57 |
+
- Full API documentation
|
| 58 |
+
- Usage examples
|
| 59 |
+
|
| 60 |
+
### 4. **requirements.txt**
|
| 61 |
+
- Added `aesthetics-predictor` package
|
| 62 |
+
- All dependencies pinned for reproducibility
|
| 63 |
+
|
| 64 |
+
## π Quick Deploy Steps
|
| 65 |
+
|
| 66 |
+
### Option 1: Upload via Web UI (Easiest)
|
| 67 |
+
|
| 68 |
+
1. Create a new Space on HuggingFace (Docker SDK)
|
| 69 |
+
2. Drag and drop ALL files from this folder
|
| 70 |
+
3. Wait for build (~5-10 minutes)
|
| 71 |
+
4. Done! β¨
|
| 72 |
+
|
| 73 |
+
### Option 2: Git Clone & Push (Recommended)
|
| 74 |
+
|
| 75 |
+
```bash
|
| 76 |
+
# 1. Create Space on HuggingFace.co (choose Docker SDK)
|
| 77 |
+
|
| 78 |
+
# 2. Clone your new Space
|
| 79 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 80 |
+
cd YOUR_SPACE_NAME
|
| 81 |
+
|
| 82 |
+
# 3. Copy all files from huggingface_deployment folder
|
| 83 |
+
# (Copy everything from this folder to your cloned space folder)
|
| 84 |
+
|
| 85 |
+
# 4. Push to HuggingFace
|
| 86 |
+
git add .
|
| 87 |
+
git commit -m "Initial deployment"
|
| 88 |
+
git push
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
See **DEPLOYMENT_GUIDE.md** for detailed instructions!
|
| 92 |
+
|
| 93 |
+
## π― What Happens on HuggingFace
|
| 94 |
+
|
| 95 |
+
1. **Build Phase**:
|
| 96 |
+
- Reads Dockerfile
|
| 97 |
+
- Installs Python 3.10
|
| 98 |
+
- Installs all pip packages
|
| 99 |
+
- Downloads ML models (~500MB)
|
| 100 |
+
- Takes ~5-10 minutes
|
| 101 |
+
|
| 102 |
+
2. **Runtime**:
|
| 103 |
+
- Container starts with user ID 1000
|
| 104 |
+
- FastAPI runs on port 7860
|
| 105 |
+
- Your API is accessible at: `https://USERNAME-SPACENAME.hf.space`
|
| 106 |
+
|
| 107 |
+
3. **Storage**:
|
| 108 |
+
- Free tier: Uses `/tmp` (ephemeral, resets on restart)
|
| 109 |
+
- Paid tier: Uses `/data` (persistent across restarts)
|
| 110 |
+
|
| 111 |
+
## π§ͺ Testing Your Deployment
|
| 112 |
+
|
| 113 |
+
Once deployed, test with:
|
| 114 |
+
|
| 115 |
+
```bash
|
| 116 |
+
# Health check
|
| 117 |
+
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/
|
| 118 |
+
|
| 119 |
+
# Should return: {"status":"ok"}
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
Then use your frontend or API client to upload/process images!
|
| 123 |
+
|
| 124 |
+
## π‘ Tips
|
| 125 |
+
|
| 126 |
+
### For Free Tier (CPU Basic)
|
| 127 |
+
- β
Perfect for testing and demos
|
| 128 |
+
- β οΈ Processing will be slow (CPU-only)
|
| 129 |
+
- β οΈ Space sleeps after inactivity
|
| 130 |
+
- β οΈ Storage resets on restart
|
| 131 |
+
|
| 132 |
+
### For Production
|
| 133 |
+
- β
Upgrade to GPU (T4 Small at $0.60/hr)
|
| 134 |
+
- β
Add persistent storage ($5/month for 20GB)
|
| 135 |
+
- β
Keeps your Space always-on
|
| 136 |
+
|
| 137 |
+
## π Links
|
| 138 |
+
|
| 139 |
+
- **Create a Space**: https://huggingface.co/new-space
|
| 140 |
+
- **HF Spaces Docs**: https://huggingface.co/docs/hub/spaces
|
| 141 |
+
- **Docker Spaces Guide**: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 142 |
+
- **Frontend Repo**: https://github.com/basilbenny1002/image-selector-front-end
|
| 143 |
+
|
| 144 |
+
## β Questions?
|
| 145 |
+
|
| 146 |
+
Check DEPLOYMENT_GUIDE.md for:
|
| 147 |
+
- Detailed step-by-step instructions
|
| 148 |
+
- Troubleshooting common issues
|
| 149 |
+
- Hardware upgrade guides
|
| 150 |
+
- Monitoring and logging
|
| 151 |
+
|
| 152 |
+
---
|
| 153 |
+
|
| 154 |
+
**You're all set! Just upload this folder to HuggingFace Spaces and you're live! π**
|
README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Image Selector Backend
|
| 3 |
+
emoji: πΌοΈ
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Image Selector Backend API
|
| 13 |
+
|
| 14 |
+
A FastAPI backend for intelligent image deduplication and selection. Upload images, group similar ones using ResNet50 embeddings, and keep only the best image from each group based on aesthetics scoring.
|
| 15 |
+
|
| 16 |
+
## π Features
|
| 17 |
+
|
| 18 |
+
- **Smart Deduplication**: Groups similar images using deep learning embeddings
|
| 19 |
+
- **Aesthetics Scoring**: Ranks images within each group using a CLIP-based aesthetics model
|
| 20 |
+
- **REST API**: Simple endpoints for upload, process, progress tracking, and download
|
| 21 |
+
- **Per-User Sessions**: Each user gets isolated processing and temporary storage
|
| 22 |
+
- **Auto Cleanup**: Temporary files are removed after download
|
| 23 |
+
|
| 24 |
+
## π‘ API Endpoints
|
| 25 |
+
|
| 26 |
+
### Health Check
|
| 27 |
+
```
|
| 28 |
+
GET /
|
| 29 |
+
```
|
| 30 |
+
Returns: `{"status": "ok"}`
|
| 31 |
+
|
| 32 |
+
### Upload Images
|
| 33 |
+
```
|
| 34 |
+
POST /upload
|
| 35 |
+
```
|
| 36 |
+
Form fields:
|
| 37 |
+
- `user_id` (text): Your unique session ID
|
| 38 |
+
- `file` (file): Image file to upload
|
| 39 |
+
|
| 40 |
+
### Start Processing
|
| 41 |
+
```
|
| 42 |
+
POST /process
|
| 43 |
+
```
|
| 44 |
+
Form fields:
|
| 45 |
+
- `user_id` (text): Your session ID
|
| 46 |
+
- `similarity` (float, optional): Similarity threshold (default: 0.87)
|
| 47 |
+
- `use_aesthetics` (bool, optional): Enable aesthetics scoring (default: true)
|
| 48 |
+
|
| 49 |
+
### Check Progress
|
| 50 |
+
```
|
| 51 |
+
GET /progress/{user_id}
|
| 52 |
+
```
|
| 53 |
+
Returns processing status, percentage, and ETA.
|
| 54 |
+
|
| 55 |
+
### Download Results
|
| 56 |
+
```
|
| 57 |
+
GET /download/{user_id}
|
| 58 |
+
```
|
| 59 |
+
Returns a ZIP file containing the best images from each group.
|
| 60 |
+
|
| 61 |
+
## π§ How It Works
|
| 62 |
+
|
| 63 |
+
1. **Upload**: Send images with a unique `user_id`
|
| 64 |
+
2. **Process**: Trigger processing - the backend will:
|
| 65 |
+
- Generate embeddings for all images
|
| 66 |
+
- Group similar images (similarity threshold configurable)
|
| 67 |
+
- Score each image using an aesthetics model
|
| 68 |
+
- Keep the best image from each group
|
| 69 |
+
3. **Download**: Retrieve a ZIP of selected images
|
| 70 |
+
4. **Auto-cleanup**: All temporary files are deleted after download
|
| 71 |
+
|
| 72 |
+
## π» Local Development
|
| 73 |
+
|
| 74 |
+
```bash
|
| 75 |
+
git clone <this-repo>
|
| 76 |
+
cd <repo-folder>
|
| 77 |
+
pip install -r requirements.txt
|
| 78 |
+
uvicorn main:app --host 0.0.0.0 --port 7860
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
## π¨ Frontend
|
| 82 |
+
|
| 83 |
+
Use with the web UI:
|
| 84 |
+
- [Image Selector Frontend](https://github.com/basilbenny1002/image-selector-front-end)
|
| 85 |
+
|
| 86 |
+
## π¦ Tech Stack
|
| 87 |
+
|
| 88 |
+
- **FastAPI**: Modern async web framework
|
| 89 |
+
- **PyTorch**: Deep learning for embeddings
|
| 90 |
+
- **ResNet50**: Feature extraction model
|
| 91 |
+
- **CLIP**: Aesthetics prediction model
|
| 92 |
+
- **SQLite**: Temporary embeddings storage
|
| 93 |
+
|
| 94 |
+
## β οΈ Notes for HuggingFace Spaces
|
| 95 |
+
|
| 96 |
+
- Uses `/tmp` for ephemeral storage (free tier)
|
| 97 |
+
- For persistent storage, upgrade to a paid tier and data will use `/data`
|
| 98 |
+
- Model weights download on first run (~500MB for ResNet50 + aesthetics model)
|
| 99 |
+
- GPU recommended for faster processing (upgrade to T4 or better)
|
| 100 |
+
|
| 101 |
+
## π License
|
| 102 |
+
|
| 103 |
+
MIT License - see LICENSE file
|
| 104 |
+
|
| 105 |
+
## π Project Origin
|
| 106 |
+
|
| 107 |
+
Based on [Image-Selecter](https://github.com/basilbenny1002/Image-Selecter)
|
START_HERE.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π DEPLOYMENT READY!
|
| 2 |
+
|
| 3 |
+
## β
Success! Your HuggingFace Spaces deployment package is ready.
|
| 4 |
+
|
| 5 |
+
### π¦ What You Have
|
| 6 |
+
|
| 7 |
+
The `huggingface_deployment` folder contains **everything** needed to deploy your Image Selector Backend to Hugging Face Spaces. Just upload this entire folder!
|
| 8 |
+
|
| 9 |
+
```
|
| 10 |
+
huggingface_deployment/
|
| 11 |
+
β
|
| 12 |
+
βββ π³ Docker Configuration
|
| 13 |
+
β βββ Dockerfile # Container setup optimized for HF Spaces
|
| 14 |
+
β βββ .dockerignore # Exclude unnecessary files from build
|
| 15 |
+
β βββ requirements.txt # All Python dependencies
|
| 16 |
+
β
|
| 17 |
+
βββ π Documentation
|
| 18 |
+
β βββ README.md # Space description (with HF YAML frontmatter!)
|
| 19 |
+
β βββ DEPLOYMENT_GUIDE.md # Detailed step-by-step deployment guide
|
| 20 |
+
β βββ QUICK_START.md # Quick overview and tips
|
| 21 |
+
β βββ CHECKLIST.md # Deployment checklist
|
| 22 |
+
β
|
| 23 |
+
βββ βοΈ Application Code
|
| 24 |
+
β βββ main.py # FastAPI entry point
|
| 25 |
+
β βββ app/ # Your application
|
| 26 |
+
β βββ api/routes.py # REST API endpoints
|
| 27 |
+
β βββ core/config.py # Settings (HF-optimized paths)
|
| 28 |
+
β βββ repositories/ # Database operations
|
| 29 |
+
β βββ services/ # ML processing logic
|
| 30 |
+
β
|
| 31 |
+
βββ π Legal
|
| 32 |
+
βββ LICENSE # MIT License
|
| 33 |
+
βββ .gitignore # Git ignore rules
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## π Next Steps (2 Minutes!)
|
| 39 |
+
|
| 40 |
+
### 1οΈβ£ Create Your Space
|
| 41 |
+
Go to: **https://huggingface.co/new-space**
|
| 42 |
+
- Name: `image-selector-backend` (or your choice)
|
| 43 |
+
- SDK: **Docker** β Important!
|
| 44 |
+
- License: MIT
|
| 45 |
+
- Click "Create Space"
|
| 46 |
+
|
| 47 |
+
### 2οΈβ£ Upload Files
|
| 48 |
+
**Either:**
|
| 49 |
+
- **Drag & Drop**: Open your Space β Files tab β Drag the entire `huggingface_deployment` folder contents
|
| 50 |
+
|
| 51 |
+
**Or:**
|
| 52 |
+
```bash
|
| 53 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 54 |
+
cd YOUR_SPACE_NAME
|
| 55 |
+
# Copy all files from huggingface_deployment folder here
|
| 56 |
+
git add .
|
| 57 |
+
git commit -m "Deploy Image Selector Backend"
|
| 58 |
+
git push
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### 3οΈβ£ Wait for Build
|
| 62 |
+
- HuggingFace automatically builds your container (~5-10 minutes)
|
| 63 |
+
- Watch the "Logs" tab for progress
|
| 64 |
+
- Status changes to "Running" when ready
|
| 65 |
+
|
| 66 |
+
### 4οΈβ£ You're Live! π
|
| 67 |
+
Your API will be at:
|
| 68 |
+
```
|
| 69 |
+
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
Test it:
|
| 73 |
+
```bash
|
| 74 |
+
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/
|
| 75 |
+
# Returns: {"status":"ok"}
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
---
|
| 79 |
+
|
| 80 |
+
## π Read These First
|
| 81 |
+
|
| 82 |
+
1. **QUICK_START.md** - Overview of what's included
|
| 83 |
+
2. **DEPLOYMENT_GUIDE.md** - Detailed deployment instructions
|
| 84 |
+
3. **CHECKLIST.md** - Step-by-step checklist
|
| 85 |
+
|
| 86 |
+
---
|
| 87 |
+
|
| 88 |
+
## π Key Features Configured
|
| 89 |
+
|
| 90 |
+
β
**Docker SDK** - Full container control
|
| 91 |
+
β
**Port 7860** - HuggingFace Spaces default
|
| 92 |
+
β
**Non-root user** - Security best practices
|
| 93 |
+
β
**Smart storage** - Auto-detects `/data` or `/tmp`
|
| 94 |
+
β
**CORS enabled** - Works with any frontend
|
| 95 |
+
β
**Auto cleanup** - Deletes files after download
|
| 96 |
+
β
**Per-user isolation** - Multiple users supported
|
| 97 |
+
β
**Progress tracking** - Real-time processing updates
|
| 98 |
+
|
| 99 |
+
---
|
| 100 |
+
|
| 101 |
+
## π° Cost Estimates
|
| 102 |
+
|
| 103 |
+
### Free Tier (CPU Basic)
|
| 104 |
+
- **Cost**: $0
|
| 105 |
+
- **Good for**: Testing, demos, light usage
|
| 106 |
+
- **Limitations**: Slow processing, sleeps when idle, no GPU
|
| 107 |
+
|
| 108 |
+
### Production Tier (T4 Small GPU)
|
| 109 |
+
- **Cost**: ~$0.60/hour (only when running)
|
| 110 |
+
- **Good for**: Real users, fast processing
|
| 111 |
+
- **Benefits**: GPU acceleration, always-on, faster processing
|
| 112 |
+
|
| 113 |
+
### With Persistent Storage
|
| 114 |
+
- **Add**: $5/month for 20GB
|
| 115 |
+
- **Benefit**: Data persists across restarts
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## π¨ Connect Your Frontend
|
| 120 |
+
|
| 121 |
+
Update your frontend code to use your new API:
|
| 122 |
+
|
| 123 |
+
```javascript
|
| 124 |
+
const API_URL = "https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space";
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
Frontend repo: https://github.com/basilbenny1002/image-selector-front-end
|
| 128 |
+
|
| 129 |
+
---
|
| 130 |
+
|
| 131 |
+
## β‘ Quick Tips
|
| 132 |
+
|
| 133 |
+
- **First run is slow**: Downloads ~500MB of ML models
|
| 134 |
+
- **Subsequent runs are fast**: Models are cached
|
| 135 |
+
- **GPU recommended**: 10-20x faster than CPU
|
| 136 |
+
- **Free tier sleeps**: Upgrade to paid for always-on
|
| 137 |
+
- **Logs are your friend**: Check them if issues occur
|
| 138 |
+
|
| 139 |
+
---
|
| 140 |
+
|
| 141 |
+
## π Troubleshooting
|
| 142 |
+
|
| 143 |
+
| Problem | Solution |
|
| 144 |
+
|---------|----------|
|
| 145 |
+
| Build fails | Check Logs tab for errors |
|
| 146 |
+
| API not responding | Verify Space is "Running" not "Sleeping" |
|
| 147 |
+
| Slow processing | Upgrade to GPU hardware |
|
| 148 |
+
| Out of memory | Upgrade to larger CPU/GPU tier |
|
| 149 |
+
| Models not loading | Wait for first download (~5 mins) |
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
## π Support Resources
|
| 154 |
+
|
| 155 |
+
- **HF Spaces Docs**: https://huggingface.co/docs/hub/spaces
|
| 156 |
+
- **Docker Spaces**: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 157 |
+
- **HF Discord**: https://discord.gg/hugging-face
|
| 158 |
+
- **FastAPI Docs**: https://fastapi.tiangolo.com/
|
| 159 |
+
|
| 160 |
+
---
|
| 161 |
+
|
| 162 |
+
## π― Success Criteria
|
| 163 |
+
|
| 164 |
+
Your deployment is successful when:
|
| 165 |
+
|
| 166 |
+
β
Space status shows "Running"
|
| 167 |
+
β
Health endpoint returns `{"status":"ok"}`
|
| 168 |
+
β
You can upload an image via API
|
| 169 |
+
β
Processing completes without errors
|
| 170 |
+
β
Download returns a ZIP file
|
| 171 |
+
β
Files are cleaned up after download
|
| 172 |
+
|
| 173 |
+
---
|
| 174 |
+
|
| 175 |
+
## π What's Next?
|
| 176 |
+
|
| 177 |
+
After successful deployment:
|
| 178 |
+
|
| 179 |
+
1. **Share your Space** with the world
|
| 180 |
+
2. **Connect your frontend** to the new API
|
| 181 |
+
3. **Monitor usage** in Space analytics
|
| 182 |
+
4. **Upgrade hardware** if needed for production
|
| 183 |
+
5. **Add to your portfolio** - you deployed ML to production!
|
| 184 |
+
|
| 185 |
+
---
|
| 186 |
+
|
| 187 |
+
# π You're Ready to Deploy!
|
| 188 |
+
|
| 189 |
+
Everything in the `huggingface_deployment` folder is configured and ready to go.
|
| 190 |
+
|
| 191 |
+
**Just upload to HuggingFace Spaces and you're live!**
|
| 192 |
+
|
| 193 |
+
Good luck! π
|
| 194 |
+
|
| 195 |
+
---
|
| 196 |
+
|
| 197 |
+
*Created: November 2025*
|
| 198 |
+
*Based on: [Image-Selecter](https://github.com/basilbenny1002/Image-Selecter)*
|
| 199 |
+
*License: MIT*
|
main.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from app.api.routes import router
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="Image Selector Backend")
|
| 6 |
+
|
| 7 |
+
# Allow all origins, methods, and headers for HuggingFace Spaces
|
| 8 |
+
app.add_middleware(
|
| 9 |
+
CORSMiddleware,
|
| 10 |
+
allow_origins=["*"],
|
| 11 |
+
allow_methods=["*"],
|
| 12 |
+
allow_headers=["*"],
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
app.include_router(router)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.0
|
| 2 |
+
uvicorn[standard]==0.30.6
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
transformers
|
| 6 |
+
Pillow
|
| 7 |
+
numpy
|
| 8 |
+
python-multipart
|
| 9 |
+
aesthetics-predictor
|