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
|
|
|
| 9 |
short_description: The Bridge of The API's
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# GambitFlow Bridge API
|
| 13 |
+
|
| 14 |
+
Unified API gateway for all GambitFlow chess engines with Firebase analytics and caching.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
|
| 18 |
+
- **Unified Endpoint**: Single API for all models (Nano, Core, Base)
|
| 19 |
+
- **Firebase Analytics**: Real-time tracking of moves and matches
|
| 20 |
+
- **Intelligent Caching**: 5-minute cache for repeated positions
|
| 21 |
+
- **Batch Predictions**: Process multiple positions in one request
|
| 22 |
+
- **Model Health Checks**: Monitor all engine statuses
|
| 23 |
+
- **No Rate Limiting**: Unrestricted access for all users
|
| 24 |
+
|
| 25 |
+
## Quick Start
|
| 26 |
+
|
| 27 |
+
### Basic Request
|
| 28 |
+
|
| 29 |
+
```bash
|
| 30 |
+
curl -X POST https://YOUR-SPACE.hf.space/predict \
|
| 31 |
+
-H "Content-Type: application/json" \
|
| 32 |
+
-d '{
|
| 33 |
+
"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
| 34 |
+
"model": "core",
|
| 35 |
+
"depth": 5,
|
| 36 |
+
"time_limit": 3000
|
| 37 |
+
}'
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
### Response
|
| 41 |
+
|
| 42 |
+
```json
|
| 43 |
+
{
|
| 44 |
+
"best_move": "e2e4",
|
| 45 |
+
"evaluation": 0.25,
|
| 46 |
+
"depth_searched": 5,
|
| 47 |
+
"nodes_evaluated": 125000,
|
| 48 |
+
"time_taken": 1500,
|
| 49 |
+
"pv": ["e2e4", "e7e5", "Ng1f3"],
|
| 50 |
+
"from_cache": false,
|
| 51 |
+
"model": "core"
|
| 52 |
+
}
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
## Endpoints
|
| 56 |
+
|
| 57 |
+
### POST /predict
|
| 58 |
+
Get best move for a position
|
| 59 |
+
|
| 60 |
+
**Request Body:**
|
| 61 |
+
```json
|
| 62 |
+
{
|
| 63 |
+
"fen": "string", // Required: FEN notation
|
| 64 |
+
"model": "core", // Optional: nano|core|base (default: core)
|
| 65 |
+
"depth": 5, // Optional: 1-10 (default: 5)
|
| 66 |
+
"time_limit": 3000, // Optional: ms (default: 3000)
|
| 67 |
+
"track_stats": true // Optional: track in analytics (default: true)
|
| 68 |
+
}
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
### POST /batch
|
| 72 |
+
Batch predictions (max 10 positions)
|
| 73 |
+
|
| 74 |
+
**Request Body:**
|
| 75 |
+
```json
|
| 76 |
+
{
|
| 77 |
+
"model": "core",
|
| 78 |
+
"positions": [
|
| 79 |
+
{
|
| 80 |
+
"fen": "...",
|
| 81 |
+
"depth": 5,
|
| 82 |
+
"time_limit": 3000
|
| 83 |
+
},
|
| 84 |
+
// ... more positions
|
| 85 |
+
]
|
| 86 |
+
}
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
### POST /match/start
|
| 90 |
+
Track match start (increments match counter)
|
| 91 |
+
|
| 92 |
+
**Request Body:**
|
| 93 |
+
```json
|
| 94 |
+
{
|
| 95 |
+
"model": "core"
|
| 96 |
+
}
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
### GET /stats
|
| 100 |
+
Get usage statistics
|
| 101 |
+
|
| 102 |
+
**Response:**
|
| 103 |
+
```json
|
| 104 |
+
{
|
| 105 |
+
"total": {
|
| 106 |
+
"moves": 15420,
|
| 107 |
+
"matches": 532
|
| 108 |
+
},
|
| 109 |
+
"models": {
|
| 110 |
+
"nano": {"moves": 4200, "matches": 150},
|
| 111 |
+
"core": {"moves": 8500, "matches": 280},
|
| 112 |
+
"base": {"moves": 2720, "matches": 102}
|
| 113 |
+
},
|
| 114 |
+
"last_updated": 1704724800
|
| 115 |
+
}
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
### GET /models
|
| 119 |
+
List all available models
|
| 120 |
+
|
| 121 |
+
### GET /health
|
| 122 |
+
Health check
|
| 123 |
+
|
| 124 |
+
## Configuration
|
| 125 |
+
|
| 126 |
+
### Environment Variables
|
| 127 |
+
|
| 128 |
+
Set these in your HuggingFace Space settings:
|
| 129 |
+
|
| 130 |
+
```bash
|
| 131 |
+
# Model Endpoints
|
| 132 |
+
NANO_ENDPOINT=https://gambitflow-nexus-nano-inference-api.hf.space
|
| 133 |
+
CORE_ENDPOINT=https://gambitflow-nexus-core-inference-api.hf.space
|
| 134 |
+
BASE_ENDPOINT=https://gambitflow-synapse-base-inference-api.hf.space
|
| 135 |
+
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
### Adding New Models
|
| 139 |
+
|
| 140 |
+
To add a new model:
|
| 141 |
+
|
| 142 |
+
1. **Deploy the model inference API** to HuggingFace Spaces
|
| 143 |
+
|
| 144 |
+
2. **Update `app.py`** - Add model configuration:
|
| 145 |
+
|
| 146 |
+
```python
|
| 147 |
+
MODELS = {
|
| 148 |
+
'nano': {...},
|
| 149 |
+
'core': {...},
|
| 150 |
+
'base': {...},
|
| 151 |
+
'new_model': { # Add here
|
| 152 |
+
'name': 'New-Model',
|
| 153 |
+
'endpoint': 'https://your-new-model-api.hf.space',
|
| 154 |
+
'timeout': 40
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
3. **Set environment variable** (optional, if URL is dynamic):
|
| 160 |
+
|
| 161 |
+
```bash
|
| 162 |
+
NEW_MODEL_ENDPOINT=https://your-new-model-api.hf.space
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
Then update the endpoint loading:
|
| 166 |
+
```python
|
| 167 |
+
'new_model': {
|
| 168 |
+
'name': 'New-Model',
|
| 169 |
+
'endpoint': os.getenv('NEW_MODEL_ENDPOINT', 'https://fallback-url.hf.space'),
|
| 170 |
+
'timeout': 40
|
| 171 |
+
}
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
4. **Update Firebase structure** - Initialize stats for new model in Firebase:
|
| 175 |
+
|
| 176 |
+
```json
|
| 177 |
+
{
|
| 178 |
+
"stats": {
|
| 179 |
+
"models": {
|
| 180 |
+
"new_model": {
|
| 181 |
+
"moves": 0,
|
| 182 |
+
"matches": 0
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
5. **Restart the Space** - Changes will take effect immediately
|
| 190 |
+
|
| 191 |
+
## Firebase Setup
|
| 192 |
+
|
| 193 |
+
### 1. Create Firebase Project
|
| 194 |
+
- Go to [Firebase Console](https://console.firebase.google.com/)
|
| 195 |
+
- Create new project
|
| 196 |
+
- Enable Realtime Database
|
| 197 |
+
|
| 198 |
+
### 2. Get Service Account
|
| 199 |
+
- Go to Project Settings → Service Accounts
|
| 200 |
+
- Generate new private key
|
| 201 |
+
- Copy JSON content
|
| 202 |
+
|
| 203 |
+
### 3. Configure Space
|
| 204 |
+
Add to HuggingFace Space secrets:
|
| 205 |
+
|
| 206 |
+
**FIREBASE_DATABASE_URL:**
|
| 207 |
+
```
|
| 208 |
+
https://YOUR-PROJECT.firebaseio.com
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
**FIREBASE_CREDENTIALS:**
|
| 212 |
+
```json
|
| 213 |
+
{
|
| 214 |
+
"type": "service_account",
|
| 215 |
+
"project_id": "your-project-id",
|
| 216 |
+
"private_key_id": "...",
|
| 217 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
|
| 218 |
+
"client_email": "firebase-adminsdk-xxxxx@your-project.iam.gserviceaccount.com",
|
| 219 |
+
"client_id": "...",
|
| 220 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 221 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 222 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 223 |
+
"client_x509_cert_url": "..."
|
| 224 |
+
}
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
### 4. Database Rules
|
| 228 |
+
Set Realtime Database rules:
|
| 229 |
+
|
| 230 |
+
```json
|
| 231 |
+
{
|
| 232 |
+
"rules": {
|
| 233 |
+
"stats": {
|
| 234 |
+
".read": true,
|
| 235 |
+
".write": false
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
```
|
| 240 |
+
|
| 241 |
+
## Integration Examples
|
| 242 |
+
|
| 243 |
+
### Python
|
| 244 |
+
|
| 245 |
+
```python
|
| 246 |
+
import requests
|
| 247 |
+
|
| 248 |
+
API_URL = "https://YOUR-SPACE.hf.space"
|
| 249 |
+
|
| 250 |
+
# Single prediction
|
| 251 |
+
response = requests.post(f"{API_URL}/predict", json={
|
| 252 |
+
"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
| 253 |
+
"model": "core",
|
| 254 |
+
"depth": 5
|
| 255 |
+
})
|
| 256 |
+
result = response.json()
|
| 257 |
+
print(f"Best move: {result['best_move']}")
|
| 258 |
+
|
| 259 |
+
# Get statistics
|
| 260 |
+
stats = requests.get(f"{API_URL}/stats").json()
|
| 261 |
+
print(f"Total moves: {stats['total']['moves']}")
|
| 262 |
+
```
|
| 263 |
+
|
| 264 |
+
### JavaScript
|
| 265 |
+
|
| 266 |
+
```javascript
|
| 267 |
+
const API_URL = "https://YOUR-SPACE.hf.space";
|
| 268 |
+
|
| 269 |
+
async function getBestMove(fen, model = 'core') {
|
| 270 |
+
const response = await fetch(`${API_URL}/predict`, {
|
| 271 |
+
method: 'POST',
|
| 272 |
+
headers: { 'Content-Type': 'application/json' },
|
| 273 |
+
body: JSON.stringify({
|
| 274 |
+
fen: fen,
|
| 275 |
+
model: model,
|
| 276 |
+
depth: 5
|
| 277 |
+
})
|
| 278 |
+
});
|
| 279 |
+
return await response.json();
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
// Usage
|
| 283 |
+
const result = await getBestMove('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
|
| 284 |
+
console.log(`Best move: ${result.best_move}`);
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
### Lichess Bot Integration
|
| 288 |
+
|
| 289 |
+
```python
|
| 290 |
+
import berserk
|
| 291 |
+
import requests
|
| 292 |
+
|
| 293 |
+
API_URL = "https://YOUR-SPACE.hf.space"
|
| 294 |
+
|
| 295 |
+
def get_engine_move(board_fen):
|
| 296 |
+
response = requests.post(f"{API_URL}/predict", json={
|
| 297 |
+
"fen": board_fen,
|
| 298 |
+
"model": "base", # Use strongest model
|
| 299 |
+
"depth": 6,
|
| 300 |
+
"time_limit": 5000
|
| 301 |
+
})
|
| 302 |
+
return response.json()['best_move']
|
| 303 |
+
|
| 304 |
+
# Use in Lichess bot
|
| 305 |
+
client = berserk.Client(berserk.TokenSession(API_TOKEN))
|
| 306 |
+
for event in client.bots.stream_incoming_events():
|
| 307 |
+
# ... handle game
|
| 308 |
+
move = get_engine_move(game.fen())
|
| 309 |
+
client.bots.make_move(game_id, move)
|
| 310 |
+
```
|
| 311 |
+
|
| 312 |
+
## Performance
|
| 313 |
+
|
| 314 |
+
- **Cache Hit Rate**: ~40-50% for common positions
|
| 315 |
+
- **Average Latency**: 2-5 seconds depending on model
|
| 316 |
+
- **Throughput**: Unlimited (no rate limits)
|
| 317 |
+
|
| 318 |
+
## Monitoring
|
| 319 |
+
|
| 320 |
+
View real-time statistics:
|
| 321 |
+
```bash
|
| 322 |
+
curl https://YOUR-SPACE.hf.space/stats
|
| 323 |
+
```
|
| 324 |
+
|
| 325 |
+
## Support
|
| 326 |
+
|
| 327 |
+
- **Space URL**: [Your HuggingFace Space](https://huggingface.co/spaces/GambitFlow/Bridge-API)
|
| 328 |
+
- **Main Site**: [gambitflow.onrender.com](https://gambitflow.onrender.com)
|
| 329 |
+
- **Issues**: GitHub Issues
|
| 330 |
+
|
| 331 |
+
---
|
| 332 |
+
|
| 333 |
+
**GambitFlow Bridge API** - Unified gateway for chess AI engines
|