File size: 11,061 Bytes
3a461b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8af6f0
3a461b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
GambitFlow Bridge API - HuggingFace Space
Unified API gateway with Firebase analytics and rate limiting
"""

from flask import Flask, request, jsonify, Response
from flask_cors import CORS
import requests
import time
import os
from functools import wraps
import firebase_admin
from firebase_admin import credentials, db
import json

app = Flask(__name__)
CORS(app)

# ==================== FIREBASE SETUP ====================

def initialize_firebase():
    """Initialize Firebase Admin SDK"""
    try:
        # Load credentials from environment variable
        firebase_creds = os.getenv('FIREBASE_CREDENTIALS')
        if firebase_creds:
            cred_dict = json.loads(firebase_creds)
            cred = credentials.Certificate(cred_dict)
        else:
            # Fallback to service account file
            cred = credentials.Certificate('firebase-credentials.json')
        
        firebase_admin.initialize_app(cred, {
            'databaseURL': os.getenv('FIREBASE_DATABASE_URL', 'https://chess-web-78351-default-rtdb.asia-southeast1.firebasedatabase.app')
        })
        print("✅ Firebase initialized successfully")
    except Exception as e:
        print(f"⚠️ Firebase initialization failed: {e}")

# Initialize Firebase
initialize_firebase()

# ==================== MODEL CONFIGURATION ====================

MODELS = {
    'nano': {
        'name': 'Nexus-Nano',
        'endpoint': os.getenv('NANO_ENDPOINT', 'https://gambitflow-nexus-nano-inference-api.hf.space'),
        'timeout': 30
    },
    'core': {
        'name': 'Nexus-Core',
        'endpoint': os.getenv('CORE_ENDPOINT', 'https://gambitflow-nexus-core-inference-api.hf.space'),
        'timeout': 40
    },
    'base': {
        'name': 'Synapse-Base',
        'endpoint': os.getenv('BASE_ENDPOINT', 'https://gambitflow-synapse-base-inference-api.hf.space'),
        'timeout': 60
    }
}

# ==================== FIREBASE ANALYTICS ====================

def increment_stats(model_name, stat_type='moves'):
    """
    Increment statistics in Firebase
    stat_type: 'moves' or 'matches'
    """
    try:
        ref = db.reference('stats')
        
        # Increment total stats
        total_ref = ref.child('total').child(stat_type)
        current = total_ref.get() or 0
        total_ref.set(current + 1)
        
        # Increment model-specific stats
        model_ref = ref.child('models').child(model_name).child(stat_type)
        current = model_ref.get() or 0
        model_ref.set(current + 1)
        
        # Update last_updated timestamp
        ref.child('last_updated').set(int(time.time()))
        
    except Exception as e:
        print(f"Firebase stats update error: {e}")

def get_all_stats():
    """Get all statistics from Firebase"""
    try:
        ref = db.reference('stats')
        stats = ref.get() or {}
        
        if not stats:
            # Initialize default structure
            stats = {
                'total': {'moves': 0, 'matches': 0},
                'models': {
                    'nano': {'moves': 0, 'matches': 0},
                    'core': {'moves': 0, 'matches': 0},
                    'base': {'moves': 0, 'matches': 0}
                },
                'last_updated': int(time.time())
            }
            ref.set(stats)
        
        return stats
    except Exception as e:
        print(f"Firebase stats fetch error: {e}")
        return {
            'total': {'moves': 0, 'matches': 0},
            'models': {
                'nano': {'moves': 0, 'matches': 0},
                'core': {'moves': 0, 'matches': 0},
                'base': {'moves': 0, 'matches': 0}
            },
            'last_updated': int(time.time())
        }

# ==================== CACHE ====================

class SimpleCache:
    def __init__(self, ttl=300):
        self.cache = {}
        self.ttl = ttl
    
    def get(self, key):
        if key in self.cache:
            value, timestamp = self.cache[key]
            if time.time() - timestamp < self.ttl:
                return value
            del self.cache[key]
        return None
    
    def set(self, key, value):
        self.cache[key] = (value, time.time())
    
    def clear_old(self):
        current_time = time.time()
        expired = [k for k, (_, t) in self.cache.items() if current_time - t >= self.ttl]
        for k in expired:
            del self.cache[k]

cache = SimpleCache(ttl=300)

# ==================== ROUTES ====================

@app.route('/')
def index():
    """API documentation"""
    return jsonify({
        'name': 'GambitFlow Bridge API',
        'version': '1.0.0',
        'description': 'Unified gateway for all GambitFlow chess engines',
        'endpoints': {
            '/predict': 'POST - Get best move prediction',
            '/health': 'GET - Health check',
            '/stats': 'GET - Get usage statistics',
            '/models': 'GET - List available models'
        },
        'models': list(MODELS.keys())
    })

@app.route('/health')
def health():
    """Health check endpoint"""
    return jsonify({
        'status': 'healthy',
        'timestamp': int(time.time()),
        'models': len(MODELS),
        'cache_size': len(cache.cache)
    })

@app.route('/stats')
def get_stats():
    """Get usage statistics from Firebase"""
    stats = get_all_stats()
    return jsonify(stats)

@app.route('/models')
def list_models():
    """List all available models"""
    models_info = {}
    for key, config in MODELS.items():
        models_info[key] = {
            'name': config['name'],
            'endpoint': config['endpoint'],
            'timeout': config['timeout']
        }
    return jsonify({'models': models_info})

@app.route('/predict', methods=['POST'])
def predict():
    """
    Main prediction endpoint
    Forwards request to appropriate model and tracks statistics
    """
    try:
        data = request.get_json()
        
        if not data:
            return jsonify({'error': 'No data provided'}), 400
        
        # Extract parameters
        fen = data.get('fen')
        model = data.get('model', 'core')
        depth = data.get('depth', 5)
        time_limit = data.get('time_limit', 3000)
        track_stats = data.get('track_stats', True)  # Allow disabling stats tracking
        
        if not fen:
            return jsonify({'error': 'FEN position required'}), 400
        
        if model not in MODELS:
            return jsonify({'error': f'Invalid model: {model}'}), 400
        
        # Check cache
        cache_key = f"{model}:{fen}:{depth}:{time_limit}"
        cached = cache.get(cache_key)
        if cached:
            cached['from_cache'] = True
            if track_stats:
                increment_stats(model, 'moves')
            return jsonify(cached)
        
        # Forward to model API
        model_config = MODELS[model]
        endpoint = f"{model_config['endpoint']}/predict"
        
        response = requests.post(
            endpoint,
            json={
                'fen': fen,
                'depth': depth,
                'time_limit': time_limit
            },
            timeout=model_config['timeout']
        )
        
        if response.status_code == 200:
            result = response.json()
            
            # Cache the result
            cache.set(cache_key, result)
            
            # Track statistics in Firebase
            if track_stats:
                increment_stats(model, 'moves')
            
            result['from_cache'] = False
            result['model'] = model
            
            return jsonify(result)
        else:
            return jsonify({
                'error': 'Model API error',
                'status_code': response.status_code,
                'details': response.text
            }), response.status_code
    
    except requests.Timeout:
        return jsonify({'error': 'Request timeout'}), 504
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/match/start', methods=['POST'])
def start_match():
    """Track match start"""
    try:
        data = request.get_json()
        model = data.get('model', 'core')
        
        if model not in MODELS:
            return jsonify({'error': 'Invalid model'}), 400
        
        increment_stats(model, 'matches')
        
        return jsonify({
            'success': True,
            'model': model,
            'message': 'Match started'
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/batch', methods=['POST'])
def batch_predict():
    """
    Batch prediction endpoint for multiple positions
    """
    try:
        data = request.get_json()
        positions = data.get('positions', [])
        model = data.get('model', 'core')
        
        if not positions:
            return jsonify({'error': 'No positions provided'}), 400
        
        if len(positions) > 10:
            return jsonify({'error': 'Maximum 10 positions per batch'}), 400
        
        results = []
        for pos in positions:
            fen = pos.get('fen')
            depth = pos.get('depth', 5)
            time_limit = pos.get('time_limit', 3000)
            
            # Make individual request
            pred_data = {
                'fen': fen,
                'model': model,
                'depth': depth,
                'time_limit': time_limit,
                'track_stats': False  # Don't track for batch
            }
            
            result = predict_single(pred_data)
            results.append(result)
        
        # Track batch as single operation
        increment_stats(model, 'moves')
        
        return jsonify({
            'success': True,
            'count': len(results),
            'results': results
        })
    
    except Exception as e:
        return jsonify({'error': str(e)}), 500

def predict_single(data):
    """Helper function for single prediction"""
    try:
        fen = data.get('fen')
        model = data.get('model', 'core')
        depth = data.get('depth', 5)
        time_limit = data.get('time_limit', 3000)
        
        model_config = MODELS[model]
        endpoint = f"{model_config['endpoint']}/predict"
        
        response = requests.post(
            endpoint,
            json={
                'fen': fen,
                'depth': depth,
                'time_limit': time_limit
            },
            timeout=model_config['timeout']
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            return {'error': 'Prediction failed'}
    except:
        return {'error': 'Request failed'}

# ==================== CLEANUP ====================

@app.before_request
def before_request():
    """Clean old cache entries before each request"""
    cache.clear_old()

# ==================== RUN ====================

if __name__ == '__main__':
    port = int(os.getenv('PORT', 7860))
    app.run(host='0.0.0.0', port=port, debug=False)