File size: 3,718 Bytes
3bfb118 |
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 |
# π MongoDB Logging Deployment Guide for Hugging Face Spaces
## β
What's Ready for Deployment
### π Files Added/Modified:
- β
`mongodb_logging.py` - Custom MongoDB logging handler
- β
`server.py` - Updated with MongoDB logging integration
- β
`requirements.txt` - Already includes `pymongo[srv]`
### π§ New API Endpoints:
- `GET /logs` - View all logs (metadata + application logs)
- `GET /logs/stats` - View logging statistics
- `GET /logs/clear?days_older_than=7` - Clear old logs
## π Hugging Face Spaces Deployment Steps
### 1. Set Environment Variables in Hugging Face Spaces
Go to your Space settings and add these environment variables:
```
MONGO_URI=mongodb+srv://harilogicgo_db_user:SSbZ55jNUsVerWKI@hairswapdb.7r2ghs4.mongodb.net/?retryWrites=true&w=majority&appName=HairSwapDB
```
### 2. Push Changes to Hugging Face
```bash
# Add all changes
git add .
# Commit changes
git commit -m "feat: add MongoDB logging for all application logs
- Add mongodb_logging.py with custom logging handler
- Update server.py to integrate MongoDB logging
- Add new endpoints: /logs, /logs/stats, /logs/clear
- All application logs now stored in MongoDB HairSwapDB.logs collection"
# Push to Hugging Face
git push origin main
```
### 3. Verify Deployment
After deployment, test these endpoints:
1. **Health Check**: `GET /health`
2. **View Logs**: `GET /logs`
3. **Log Statistics**: `GET /logs/stats`
4. **Clear Old Logs**: `GET /logs/clear?days_older_than=7`
## π MongoDB Collections Structure
### `HairSwapDB.logs` Collection:
```json
{
"_id": "ObjectId",
"timestamp": "2025-09-27T12:30:45.123Z",
"level": "INFO|WARNING|ERROR",
"logger": "hair_server",
"message": "Log message content",
"module": "server",
"function": "get_hairswap",
"line": 168,
"thread": 12345,
"process": 6789,
"exception": "Exception traceback (if error)"
}
```
### `HairSwapDB.uploads` Collection:
```json
{
"_id": "image-uuid",
"filename": "image.png",
"path": "/data/uploads/image-uuid.png"
}
```
### `HairSwapDB.results` Collection:
```json
{
"_id": "result-uuid",
"filename": "result.png",
"path": "/data/results/result-uuid.png",
"source_id": "source-uuid",
"reference_id": "reference-uuid"
}
```
## π Testing the Deployment
### 1. Test Log Generation:
```bash
# Upload an image
curl -X POST "https://your-space-url/upload" \
-F "image=@test_image.jpg"
# Perform hair swap
curl -X POST "https://your-space-url/get-hairswap" \
-H "Content-Type: application/json" \
-d '{"source_id": "source-uuid", "reference_id": "reference-uuid"}'
```
### 2. Check Logs:
```bash
# View all logs
curl "https://your-space-url/logs"
# View log statistics
curl "https://your-space-url/logs/stats"
# Clear logs older than 7 days
curl "https://your-space-url/logs/clear?days_older_than=7"
```
## π― What Gets Logged
### β
Application Logs (NEW):
- Model loading messages
- Hair transfer progress
- Error messages and stack traces
- API request/response logs
- System status messages
### β
Metadata Logs (EXISTING):
- Image uploads
- Hair swap results
- File paths and IDs
## π¨ Important Notes
1. **MongoDB Connection**: Make sure `MONGO_URI` is set in Space environment variables
2. **Log Rotation**: Use `/logs/clear` endpoint to manage log storage
3. **Performance**: MongoDB logging is asynchronous and won't slow down the API
4. **Fallback**: If MongoDB fails, logs still go to console
5. **Security**: MongoDB URI contains credentials - keep it secure
## π Ready to Deploy!
Your MongoDB logging system is ready for Hugging Face Spaces deployment. All application logs will be automatically stored in MongoDB and accessible via the new API endpoints.
|