File size: 7,753 Bytes
93c19dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Domify Academy Super Bot - Deployment Guide

## Overview

This guide provides step-by-step instructions for deploying the Domify Academy Super Bot to **Hugging Face Spaces** using Docker.

---

## Prerequisites

Before deploying, ensure you have:

1. **Hugging Face Account** - Create one at [huggingface.co](https://huggingface.co)
2. **NVIDIA API Key** - Get from [NVIDIA API Portal](https://build.nvidia.com/)
3. **Database** - MySQL/TiDB database URL
4. **Optional: Google Sheets API Key** - For feedback logging

---

## Step 1: Create a Hugging Face Space

1. Go to [huggingface.co/spaces](https://huggingface.co/spaces)
2. Click **"Create new Space"**
3. Fill in the details:
   - **Space name**: `domify-academy-bot`
   - **License**: Apache 2.0 (or your choice)
   - **SDK**: Select **"Docker"**
   - **Visibility**: Public or Private (your choice)
4. Click **"Create Space"**

---

## Step 2: Prepare Your Repository

Create a `.gitignore` file to exclude sensitive files:

```gitignore
node_modules/
dist/
.env
.env.local
.env.*.local
*.log
.DS_Store
.vscode/
.idea/
```

Initialize a Git repository and push to Hugging Face:

```bash
cd /path/to/domify-academy-bot
git init
git add .
git commit -m "Initial commit: Domify Academy Super Bot"
git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/domify-academy-bot
git push -u origin main
```

---

## Step 3: Set Environment Variables

In your Hugging Face Space settings, add the following secrets:

| Variable | Description | Example |
|----------|-------------|---------|
| `DATABASE_URL` | MySQL connection string | `mysql://user:pass@host:3306/db` |
| `NVIDIA_API_KEY` | NVIDIA API key for LLM/image models | `nvapi-xxxxx` |
| `JWT_SECRET` | Secret for session tokens | Generate with `openssl rand -base64 32` |
| `GOOGLE_SHEETS_API_KEY` | (Optional) Google Sheets API key | `AIzaSyD...` |
| `GOOGLE_SHEETS_ID` | (Optional) Google Sheet ID | `1BxiMVs0XRA5nFMKUVfIKWWY...` |
| `NODE_ENV` | Environment | `production` |

**To set secrets in Hugging Face:**

1. Go to your Space settings
2. Scroll to **"Repository secrets"**
3. Add each variable as a secret

---

## Step 4: Configure Docker Build

The `Dockerfile` is already configured for Hugging Face Spaces. Key features:

- **Multi-stage build** for optimized image size
- **Production dependencies only** to reduce footprint
- **Health check** to monitor application status
- **Non-root user** for security
- **Port 7860** (Hugging Face standard)

---

## Step 5: Deploy

Once you push to the repository, Hugging Face automatically:

1. Detects the `Dockerfile`
2. Builds the Docker image
3. Deploys the container
4. Assigns a public URL

**Monitor the build:**

1. Go to your Space page
2. Click the **"Build"** tab
3. Watch the logs for any errors

---

## Step 6: Verify Deployment

Once deployed, test the application:

```bash
# Check health endpoint
curl https://YOUR_SPACE_URL/api/health

# Expected response:
# {
#   "status": "healthy",
#   "uptime": 123.45,
#   "database": "connected"
# }
```

---

## Environment Variables Reference

### Required Variables

- **`DATABASE_URL`**: MySQL connection string
  - Format: `mysql://user:password@host:port/database`
  - Example: `mysql://admin:secret@db.example.com:3306/domify_bot`

- **`NVIDIA_API_KEY`**: API key for NVIDIA models
  - Get from: [NVIDIA Build Portal](https://build.nvidia.com/)
  - Used for: Llama-3 70B, SDXL, Flux, Video generation

- **`JWT_SECRET`**: Secret for signing session tokens
  - Generate: `openssl rand -base64 32`
  - Keep secure and don't share

### Optional Variables

- **`GOOGLE_SHEETS_API_KEY`**: For feedback logging to Google Sheets
- **`GOOGLE_SHEETS_ID`**: ID of the target Google Sheet
- **`RATE_LIMIT_REQUESTS`**: Requests per minute (default: 30)
- **`RATE_LIMIT_WINDOW`**: Rate limit window in seconds (default: 3600)

---

## Database Setup

### MySQL Schema

The application automatically creates tables on first run. Required tables:

- `users` - User accounts and authentication
- `conversations` - Chat conversations
- `messages` - Individual messages
- `images` - Generated images
- `feedback` - User feedback and ratings

### Connection String Format

```
mysql://username:password@hostname:port/database_name
```

**Example with TiDB (recommended for Hugging Face):**

```
mysql://root:password@tidb-cluster.tidb.cloud:4000/domify_bot?sslMode=REQUIRE
```

---

## Monitoring and Logs

### View Logs

In Hugging Face Space:

1. Go to **"Logs"** tab
2. Filter by date/time
3. Search for errors or specific operations

### Common Issues

| Issue | Solution |
|-------|----------|
| Database connection failed | Verify `DATABASE_URL` and network access |
| NVIDIA API errors | Check `NVIDIA_API_KEY` validity and quota |
| Out of memory | Increase Space compute resources |
| Rate limit errors | Adjust `RATE_LIMIT_REQUESTS` or upgrade tier |

---

## Performance Optimization

### Caching

The application uses in-memory caching for:

- Search results (5 minutes TTL)
- User sessions (30 minutes TTL)
- Generated images (1 hour TTL)

### Database Optimization

- Add indexes on frequently queried columns
- Archive old conversations periodically
- Monitor query performance

### Scaling

For high traffic:

1. **Upgrade Space compute** to more powerful GPU
2. **Use Redis** for distributed caching
3. **Implement database connection pooling**
4. **Enable CDN** for static assets

---

## Backup and Recovery

### Database Backups

Set up automated backups:

```bash
# Manual backup
mysqldump -u user -p database_name > backup.sql

# Restore from backup
mysql -u user -p database_name < backup.sql
```

### Image Backups

Generated images are stored in S3 (via Manus). Configure backup:

1. Enable S3 versioning
2. Set lifecycle policies for old objects
3. Test recovery procedures

---

## Security Best Practices

1. **Never commit secrets** - Use environment variables only
2. **Enable HTTPS** - Hugging Face provides SSL by default
3. **Rate limiting** - Prevents abuse and API quota exhaustion
4. **Input validation** - All user inputs are sanitized
5. **Database encryption** - Use SSL for database connections
6. **Regular updates** - Keep dependencies updated

---

## Troubleshooting

### Application won't start

**Check logs:**
```bash
# In Hugging Face Logs tab, look for:
# - Database connection errors
# - Missing environment variables
# - Port binding issues
```

**Solution:**
1. Verify all required environment variables are set
2. Test database connection separately
3. Check Docker image build logs

### Slow responses

**Causes:**
- Database queries too slow
- LLM model busy or overloaded
- Rate limiting triggered

**Solutions:**
1. Optimize database queries
2. Increase LLM fallback timeout
3. Upgrade Space compute

### Memory leaks

**Monitor:**
- Check `/api/health` endpoint
- Monitor memory usage in logs

**Fix:**
1. Restart the Space
2. Review recent code changes
3. Increase available memory

---

## Maintenance

### Weekly Tasks

- Monitor error logs
- Check API quota usage
- Verify database backups

### Monthly Tasks

- Review performance metrics
- Update dependencies
- Archive old conversations

### Quarterly Tasks

- Security audit
- Database optimization
- Capacity planning

---

## Support and Resources

- **Documentation**: See `ARCHITECTURE.md` and `README.md`
- **NVIDIA API Docs**: [build.nvidia.com/docs](https://build.nvidia.com/docs)
- **Hugging Face Docs**: [huggingface.co/docs](https://huggingface.co/docs)
- **Issues**: Check GitHub issues or contact support

---

## Next Steps

1. Deploy to Hugging Face Spaces
2. Test all features (Ask, Imagine, Search)
3. Monitor logs for errors
4. Optimize based on usage patterns
5. Scale as needed

Good luck! 🚀