File size: 5,592 Bytes
44cdbab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# πŸš€ Quick Deployment Reference

## Local Testing

### Using Docker Compose (Recommended)
```bash
cd /Volumes/WorkSpace/Project/REMB/algorithms

# Build images
make build

# Start services
make up

# View logs
make logs

# Check health
make health

# Access services
# Backend:  http://localhost:8000
# Frontend: http://localhost:8501
# API Docs: http://localhost:8000/docs

# Stop services
make down
```

### Manual (Without Docker)
```bash
# Terminal 1: Backend
cd /Volumes/WorkSpace/Project/REMB/algorithms/backend
pip install -r requirements.txt
uvicorn main:app --reload --port 8000

# Terminal 2: Frontend
cd /Volumes/WorkSpace/Project/REMB/algorithms/frontend
pip install -r requirements.txt
export API_URL=http://localhost:8000
streamlit run app.py --server.port 8501
```

---

## Production Deployment

### Backend β†’ Hugging Face Spaces

```bash
cd /Volumes/WorkSpace/Project/REMB/algorithms/backend

# 1. Rename README for HF
cp README_HF.md README.md

# 2. Initialize git (if needed)
git init

# 3. Add HF remote (replace with your details)
git remote add hf https://huggingface.co/spaces/<USERNAME>/<SPACE_NAME>

# 4. Commit and push
git add .
git commit -m "Deploy to Hugging Face Spaces"
git push hf main

# 5. Monitor build at:
# https://huggingface.co/spaces/<USERNAME>/<SPACE_NAME>
```

**Your API will be at**: `https://<USERNAME>-<SPACE_NAME>.hf.space`

### Frontend β†’ Streamlit Cloud

```bash
cd /Volumes/WorkSpace/Project/REMB/algorithms/frontend

# 1. Push to GitHub
git init
git remote add origin https://github.com/<USERNAME>/<REPO_NAME>.git
git add .
git commit -m "Initial commit"
git branch -M main
git push -u origin main

# 2. Go to Streamlit Cloud
# https://streamlit.io/cloud

# 3. Create new app:
# - Repository: <USERNAME>/<REPO_NAME>
# - Branch: main
# - Main file: app.py

# 4. Add secrets in Streamlit Cloud settings:
# API_URL = "https://<HF_USERNAME>-<SPACE_NAME>.hf.space"

# 5. Deploy!
```

**Your app will be at**: `https://<APP_NAME>.streamlit.app`

---

## Environment Variables

### Backend (.env or HF Secrets)
```bash
API_HOST=0.0.0.0
API_PORT=7860
CORS_ORIGINS=*
LOG_LEVEL=INFO
```

### Frontend (.env or Streamlit Secrets)
```bash
# Development
API_URL=http://localhost:8000

# Production
API_URL=https://<HF_USERNAME>-<SPACE_NAME>.hf.space
```

---

## Troubleshooting

### Docker Build Fails
```bash
# Test build locally
cd backend
docker build -t test .

# Check logs
docker logs <container_id>

# Run interactively
docker run -it test /bin/bash
```

### Frontend Can't Connect
1. Check `API_URL` environment variable
2. Verify backend is running
3. Check CORS settings in backend
4. Test backend directly: `curl <API_URL>/health`

### Port Already in Use
```bash
# Find process
lsof -i :8000
lsof -i :8501

# Kill process
kill -9 <PID>
```

---

## Testing Deployed Services

### Test Backend API
```bash
# Health check
curl https://<USERNAME>-<SPACE_NAME>.hf.space/health

# View API docs
open https://<USERNAME>-<SPACE_NAME>.hf.space/docs

# Test optimization endpoint
curl -X POST https://<USERNAME>-<SPACE_NAME>.hf.space/api/optimize \
  -H "Content-Type: application/json" \
  -d '{
    "config": {
      "spacing_min": 20,
      "spacing_max": 30,
      "population_size": 20,
      "generations": 50
    },
    "land_plots": [{
      "type": "Polygon",
      "coordinates": [[[0,0],[100,0],[100,100],[0,100],[0,0]]]
    }]
  }'
```

### Test Frontend
1. Open `https://<APP_NAME>.streamlit.app`
2. Select "Sample" β†’ "Rectangle 100x100"
3. Click "πŸš€ Run Optimization"
4. Wait for results
5. Download GeoJSON

---

## Useful Makefile Commands

```bash
make help           # Show all available commands
make build          # Build Docker images
make up             # Start services
make down           # Stop services
make logs           # View all logs
make logs-backend   # View backend logs only
make logs-frontend  # View frontend logs only
make restart        # Restart all services
make clean          # Stop and remove volumes
make test-backend   # Test backend container
make dev-backend    # Run backend without Docker
make dev-frontend   # Run frontend without Docker
make status         # Show service status
make health         # Check service health
```

---

## File Checklist

Before deploying, ensure these files exist:

**Backend**:
- [x] `Dockerfile`
- [x] `.dockerignore`
- [x] `requirements.txt` (with production deps)
- [x] `README_HF.md` (will become README.md)

**Frontend**:
- [x] `app.py` (with environment support)
- [x] `requirements.txt` (with python-dotenv)
- [x] `.streamlit/config.toml`

**Root**:
- [x] `docker-compose.yml`
- [x] `Makefile`
- [x] `.env.production` (template)
- [x] `DEPLOYMENT.md`
- [x] `README.md` (updated)

---

## Quick Links

- πŸ“– **Full Guide**: [DEPLOYMENT.md](file:///Volumes/WorkSpace/Project/REMB/algorithms/DEPLOYMENT.md)
- 🏠 **Main README**: [README.md](file:///Volumes/WorkSpace/Project/REMB/algorithms/README.md)
- 🐳 **Backend Dockerfile**: [Dockerfile](file:///Volumes/WorkSpace/Project/REMB/algorithms/backend/Dockerfile)
- 🎨 **Frontend App**: [app.py](file:///Volumes/WorkSpace/Project/REMB/algorithms/frontend/app.py)
- πŸ”§ **Docker Compose**: [docker-compose.yml](file:///Volumes/WorkSpace/Project/REMB/algorithms/docker-compose.yml)

---

## Support

- **Hugging Face Spaces**: https://huggingface.co/docs/hub/spaces
- **Streamlit Cloud**: https://docs.streamlit.io/streamlit-community-cloud
- **Docker**: https://docs.docker.com/

**Next Step**: Follow the [DEPLOYMENT.md](file:///Volumes/WorkSpace/Project/REMB/algorithms/DEPLOYMENT.md) guide!