-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py.orig
More file actions
949 lines (803 loc) · 33.1 KB
/
api.py.orig
File metadata and controls
949 lines (803 loc) · 33.1 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
#!/usr/bin/env python3
"""
ENHANCED SELF-HEALING API WITH VALIDATION SYSTEM
Includes Stack Overflow ground truth validation
- Tracks search quality
- Provides quality scores
- Identifies accepted answers
- Monitors performance
"""
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import sqlite3
import os
import sys
import re
import logging
import time
import json
from datetime import datetime
from functools import wraps
from pathlib import Path
import traceback
import hashlib
from collections import defaultdict
from typing import Dict, List, Optional
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
# Import YOUR self-healing system
from production_self_healing import self_healer, self_heal
from ultimate_self_healing_system_fixed import UltimateLanguageIntegration
# Initialize Flask
app = Flask(__name__)
CORS(app, origins="*", methods=["GET", "POST", "OPTIONS"],
allow_headers=["Content-Type", "Authorization", "X-API-Key"])
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
logger = logging.getLogger(__name__)
# Configuration
CONFIG = {
'MAIN_DB': os.environ.get('SELF_HEALING_DB', './SELF_HEALING_AGI.db'),
'PORT': int(os.environ.get('PORT', 5000)),
'ENABLE_CACHE': True,
'CACHE_SIZE': 1000
}
# Initialize your language integration system
language_system = UltimateLanguageIntegration()
# SIMPLE API KEYS - Auto-loaded from JSON file
API_KEYS_FILE = 'api_keys.json'
# Load existing keys or create default
if os.path.exists(API_KEYS_FILE):
with open(API_KEYS_FILE, 'r') as f:
API_KEYS = json.load(f)
else:
API_KEYS = {
'demo': {'name': 'Demo User', 'daily_limit': 100, 'created': datetime.now().isoformat()},
}
with open(API_KEYS_FILE, 'w') as f:
json.dump(API_KEYS, f, indent=2)
# Simple key generation
def generate_api_key():
"""Generate a simple API key"""
import random
import string
return 'beta_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=16))
def save_api_keys():
"""Save API keys to file"""
with open(API_KEYS_FILE, 'w') as f:
json.dump(API_KEYS, f, indent=2)
# Simple rate limiting (resets on restart - fine for beta)
rate_limits = defaultdict(lambda: {'requests': 0, 'last_reset': datetime.now()})
request_cache = {}
class StackOverflowGroundTruthValidator:
"""Validates search results against Stack Overflow's accepted answers"""
def __init__(self, db_path: str):
self.db_path = db_path
self.logger = logging.getLogger(__name__)
self._cache = {}
self._init_database()
def _init_database(self):
"""Initialize validation tracking tables"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Create validation metrics table
cursor.execute("""
CREATE TABLE IF NOT EXISTS validation_metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
query TEXT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_results INTEGER,
accepted_found BOOLEAN,
accepted_position INTEGER,
quality_score REAL,
avg_similarity REAL,
threshold_used REAL
)
""")
conn.commit()
conn.close()
def quick_validate(self, query: str, results: List[Dict], threshold_used: float = 0.7) -> Dict:
"""Quickly validate if results contain accepted answers"""
if not results:
return {
'score': 0.0,
'accepted_found': False,
'accepted_position': -1,
'message': 'No results returned'
}
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Check if any result is an accepted answer
accepted_position = -1
accepted_found = False
for idx, result in enumerate(results):
# Check if this result is marked as accepted
is_accepted = result.get('is_accepted', False) or result.get('is_accepted_inferred', False)
if is_accepted and not accepted_found:
accepted_found = True
accepted_position = idx + 1
break
# Calculate quality score
quality_score = self._calculate_quality_score(
results,
accepted_found,
accepted_position
)
# Log validation metrics
avg_similarity = sum(r.get('similarity', 0) for r in results) / len(results) if results else 0
cursor.execute("""
INSERT INTO validation_metrics
(query, total_results, accepted_found, accepted_position, quality_score, avg_similarity, threshold_used)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
query,
len(results),
accepted_found,
accepted_position,
quality_score,
avg_similarity,
threshold_used
))
conn.commit()
conn.close()
return {
'score': quality_score,
'accepted_found': accepted_found,
'accepted_position': accepted_position,
'avg_similarity': avg_similarity,
'message': self._get_validation_message(quality_score, accepted_found)
}
def _calculate_quality_score(self, results: List[Dict], accepted_found: bool, accepted_position: int) -> float:
"""Calculate a quality score for the search results"""
score = 0.0
# Base score from result count
if len(results) > 0:
score += 0.3
# Score based on accepted answer position
if accepted_found:
if accepted_position == 1:
score += 0.5 # Perfect - accepted answer is first
elif accepted_position <= 3:
score += 0.4 # Good - accepted answer in top 3
elif accepted_position <= 5:
score += 0.3 # OK - accepted answer in top 5
else:
score += 0.2 # Found but not highly ranked
# Score based on average similarity
if results:
avg_similarity = sum(r.get('similarity', 0) for r in results) / len(results)
score += min(avg_similarity * 0.2, 0.2) # Up to 0.2 points for high similarity
return min(score, 1.0)
def _get_validation_message(self, score: float, accepted_found: bool) -> str:
"""Generate a human-readable validation message"""
if score >= 0.8:
return "Excellent results - accepted answer found at top"
elif score >= 0.6:
return "Good results - relevant answers found"
elif score >= 0.4:
return "Fair results - some relevant content"
elif accepted_found:
return "Accepted answer found but not well ranked"
else:
return "Poor results - consider adjusting threshold"
def get_problematic_queries(self, min_attempts: int = 3, max_score: float = 0.4) -> List[Dict]:
"""Get queries that consistently return poor results"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
SELECT
query,
COUNT(*) as attempts,
AVG(quality_score) as avg_score,
AVG(CASE WHEN accepted_found THEN 1 ELSE 0 END) as accepted_rate
FROM validation_metrics
GROUP BY query
HAVING attempts >= ? AND avg_score <= ?
ORDER BY avg_score ASC
LIMIT 50
""", (min_attempts, max_score))
problematic = []
for row in cursor.fetchall():
problematic.append({
'query': row[0],
'attempts': row[1],
'avg_score': row[2],
'accepted_rate': row[3]
})
conn.close()
return problematic
def generate_report(self) -> Dict:
"""Generate a comprehensive validation report"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Overall metrics
cursor.execute("""
SELECT
COUNT(DISTINCT query) as unique_queries,
COUNT(*) as total_validations,
AVG(quality_score) as avg_quality_score,
AVG(CASE WHEN accepted_found THEN 1 ELSE 0 END) as accepted_rate,
AVG(CASE WHEN total_results > 0 THEN 1 ELSE 0 END) as success_rate
FROM validation_metrics
WHERE timestamp > datetime('now', '-7 days')
""")
overall = cursor.fetchone()
# Threshold performance
cursor.execute("""
SELECT
threshold_used,
COUNT(*) as uses,
AVG(quality_score) as avg_score
FROM validation_metrics
WHERE timestamp > datetime('now', '-7 days')
GROUP BY threshold_used
ORDER BY avg_score DESC
""")
threshold_performance = []
for row in cursor.fetchall():
threshold_performance.append({
'threshold': row[0],
'uses': row[1],
'avg_score': row[2]
})
conn.close()
return {
'generated_at': datetime.now().isoformat(),
'overall_metrics': {
'unique_queries': overall[0] if overall else 0,
'total_validations': overall[1] if overall else 0,
'avg_quality_score': overall[2] if overall else 0,
'accepted_rate': overall[3] if overall else 0,
'success_rate': overall[4] if overall else 0
},
'threshold_performance': threshold_performance,
'problematic_queries': self.get_problematic_queries()[:10] # Top 10
}
class DatabaseManager:
"""Manages database operations"""
def __init__(self, db_path):
self.db_path = db_path
self.connection = None
self._connect()
def _connect(self):
"""Create database connection"""
try:
self.connection = sqlite3.connect(self.db_path, check_same_thread=False)
self.connection.row_factory = sqlite3.Row
# Enable optimizations
cursor = self.connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.execute("PRAGMA cache_size=10000")
logger.info(f"✅ Connected to database: {self.db_path}")
# Test FTS
try:
cursor.execute("SELECT COUNT(*) FROM solutions_fts LIMIT 1")
logger.info("✅ FTS enabled - searches will be fast!")
CONFIG['FTS_ENABLED'] = True
except:
CONFIG['FTS_ENABLED'] = False
except Exception as e:
logger.error(f"❌ Database connection failed: {e}")
self.connection = None
def search_solutions(self, error_type: str, error_message: str, limit: int = 5, threshold: float = 0.7):
"""Search for solutions with similarity scoring"""
if not self.connection:
return []
try:
cursor = self.connection.cursor()
results = []
# Simple search query
full_query = f"{error_type}: {error_message}"
if CONFIG.get('FTS_ENABLED'):
# Try FTS search
search_query = full_query.replace(":", " ").replace("'", " ")
search_query = ' '.join(search_query.split())
try:
cursor.execute("""
SELECT
s.question_title,
s.question_id,
s.answer_body,
s.answer_score,
s.question_tags,
s.is_accepted,
s.is_accepted_inferred,
rank as similarity_rank
FROM solutions_fts fts
JOIN solutions s ON s.id = fts.rowid
WHERE solutions_fts MATCH ?
AND s.answer_score > 10
ORDER BY rank
LIMIT ?
""", (search_query, limit * 2)) # Get more results for filtering
for row in cursor.fetchall():
# Calculate similarity score (normalize FTS rank)
similarity = min(abs(row['similarity_rank']) / 10.0, 1.0)
if similarity >= threshold:
result = self._format_solution(row)
result['similarity'] = similarity
result['is_accepted'] = row['is_accepted'] if 'is_accepted' in row.keys() else False
result['is_accepted_inferred'] = row['is_accepted_inferred'] if 'is_accepted_inferred' in row.keys() else False
results.append(result)
if len(results) >= limit:
break
except Exception as e:
logger.debug(f"FTS search failed: {e}")
# Fallback to regular search if no results
if not results:
cursor.execute("""
SELECT
question_title,
question_id,
answer_body,
answer_score,
question_tags,
is_accepted,
is_accepted_inferred
FROM solutions
WHERE (question_title LIKE ? OR answer_body LIKE ?)
AND answer_score > 10
ORDER BY answer_score DESC
LIMIT ?
""", (f'%{error_type}%', f'%{error_message}%', limit))
for row in cursor.fetchall():
result = self._format_solution(row)
# Simple similarity calculation for non-FTS results
result['similarity'] = 0.7 # Default similarity for fallback results
result['is_accepted'] = row['is_accepted'] if 'is_accepted' in row.keys() else False
result['is_accepted_inferred'] = row['is_accepted_inferred'] if 'is_accepted_inferred' in row.keys() else False
results.append(result)
return results
except Exception as e:
logger.error(f"Search error: {e}")
return []
def _format_solution(self, row):
"""Format solution for response"""
# Extract code
answer_body = str(row['answer_body'] if 'answer_body' in row.keys() else '')
# Multiple strategies for code extraction
code_snippets = []
# Strategy 1: <pre><code> tags (most common on SO)
code_snippets.extend(re.findall(r'<pre><code>(.*?)</code></pre>', answer_body, re.DOTALL))
# Strategy 2: Just <code> tags for inline code
if not code_snippets:
code_snippets.extend(re.findall(r'<code>(.*?)</code>', answer_body, re.DOTALL))
# Strategy 3: Markdown code blocks
if not code_snippets:
code_snippets.extend(re.findall(r'```(?:python|javascript|java)?\n?(.*?)\n?```', answer_body, re.DOTALL))
# Clean the code snippets
if code_snippets:
# Take the first substantial code block
for snippet in code_snippets:
cleaned = snippet.strip()
# HTML decode common entities
cleaned = cleaned.replace('<', '<').replace('>', '>').replace('&', '&').replace('"', '"')
if len(cleaned) > 20: # Skip tiny snippets
code_snippets = [cleaned]
break
# Clean preview
preview = re.sub(r'<[^>]+>', '', answer_body)
preview = ' '.join(preview.split())[:300] + '...'
return {
'title': row['question_title'],
'question_id': row['question_id'],
'url': f"https://stackoverflow.com/questions/{row['question_id']}",
'preview': preview,
'code': code_snippets[0] if code_snippets else None,
'score': row['answer_score']
}
# Initialize database
db = DatabaseManager(CONFIG['MAIN_DB'])
# Initialize validator
validator = StackOverflowGroundTruthValidator(CONFIG['MAIN_DB'])
# SIMPLE AUTH DECORATOR
def require_api_key(f):
"""Simple API key check"""
@wraps(f)
def decorated_function(*args, **kwargs):
api_key = request.headers.get('X-API-Key') or request.args.get('api_key', 'demo')
# Check if key exists
if api_key not in API_KEYS:
return jsonify({
'error': 'Invalid API key',
'message': 'Use "demo" key or register at /register'
}), 401
# Simple daily rate limit check
today = datetime.now().strftime('%Y-%m-%d')
limit_key = f"{api_key}:{today}"
if rate_limits[limit_key]['requests'] >= API_KEYS[api_key]['daily_limit']:
return jsonify({
'error': 'Daily limit reached',
'limit': API_KEYS[api_key]['daily_limit'],
'resets': 'midnight UTC'
}), 429
# Increment counter
rate_limits[limit_key]['requests'] += 1
# Add key info to request
request.api_key = api_key
request.key_info = API_KEYS[api_key]
return f(*args, **kwargs)
return decorated_function
# SECURITY HEADERS (basic but important)
@app.after_request
def add_security_headers(response):
response.headers['X-Content-Type-Options'] = 'nosniff'
response.headers['X-Frame-Options'] = 'SAMEORIGIN'
return response
# ROUTES
@app.route('/')
def home():
"""Landing page or API docs"""
if os.path.exists('index.html'):
return send_file('index.html')
return jsonify({
'name': 'Talon Self-Healing API (Beta) - Enhanced',
'status': 'operational',
'version': '2.0',
'features': ['Stack Overflow search', 'Quality validation', 'Performance monitoring'],
'demo_key': 'demo',
'registration': {
'endpoint': '/register',
'method': 'POST',
'body': {
'name': 'Your Name',
'email': 'your@email.com'
}
},
'endpoints': {
'/register': 'Get your API key (POST)',
'/forgot-key': 'Retrieve your API key (POST)',
'/health': 'Check API status',
'/search': 'Search Stack Overflow database',
'/fix': 'Get fix for your error',
'/test': 'Test the API',
'/stats': 'Check your usage',
'/validation/report': 'Get search quality report',
'/validation/test': 'Test thresholds for a query',
'/validation/problematic': 'Get problematic queries'
},
'limits': {
'demo': '100 requests/day',
'beta': '500 requests/day'
},
'total_users': len(API_KEYS),
'documentation': 'Available at the root URL'
})
@app.route('/health')
def health():
"""Health check with validation metrics"""
try:
# Get validation metrics
report = validator.generate_report()
metrics = report['overall_metrics']
health_status = {
'status': 'healthy',
'database': db.connection is not None,
'timestamp': datetime.now().isoformat(),
'validation_metrics': {
'avg_quality_score': round(metrics['avg_quality_score'], 2),
'accepted_rate': round(metrics['accepted_rate'], 2),
'total_validations': metrics['total_validations']
}
}
# Determine overall health based on metrics
if metrics['avg_quality_score'] < 0.5:
health_status['status'] = 'degraded'
health_status['message'] = 'Search quality below threshold'
return jsonify(health_status)
except Exception as e:
logger.error(f"Health check error: {e}")
return jsonify({
'status': 'error',
'database': db.connection is not None,
'error': str(e)
}), 500
@app.route('/search', methods=['POST'])
@require_api_key
def search():
"""Enhanced search with validation"""
try:
data = request.get_json() or {}
query = data.get('query', '')
limit = min(data.get('limit', 5), 10) # Max 10 for beta
threshold = float(data.get('threshold', 0.7))
if not query:
return jsonify({'error': 'Query required'}), 400
# Check cache
cache_key = hashlib.md5(f"{query}:{limit}:{threshold}".encode()).hexdigest()
if cache_key in request_cache:
cached_response = request_cache[cache_key]
cached_response['cached'] = True
return jsonify(cached_response)
# Extract error type if present
error_type = ''
error_message = query
if ':' in query:
parts = query.split(':', 1)
error_type = parts[0].strip()
error_message = parts[1].strip() if len(parts) > 1 else query
# Search with threshold
results = db.search_solutions(error_type, error_message, limit, threshold)
# Validate results
validation = validator.quick_validate(query, results, threshold)
response = {
'query': query,
'results': results,
'total': len(results),
'threshold': threshold,
'quality_score': validation['score'],
'has_accepted_answer': validation['accepted_found'],
'validation_message': validation['message'],
'metrics': {
'accepted_position': validation['accepted_position'],
'avg_similarity': round(validation['avg_similarity'], 3)
}
}
# Cache it
if CONFIG['ENABLE_CACHE'] and len(request_cache) < CONFIG['CACHE_SIZE']:
request_cache[cache_key] = response
return jsonify(response)
except Exception as e:
logger.error(f"Search error: {e}")
return jsonify({'error': 'Search failed'}), 500
@app.route('/fix', methods=['POST'])
@require_api_key
@self_heal
def fix_error():
"""Get fix for an error with validation"""
try:
data = request.get_json() or {}
error_type = data.get('error_type', '')
error_message = data.get('error_message', '')
threshold = float(data.get('threshold', 0.7))
if not error_type and not error_message:
return jsonify({'error': 'Provide error_type or error_message'}), 400
# Try to extract error type
if not error_type and ':' in error_message:
error_type = error_message.split(':')[0].strip()
# Search for solutions
solutions = db.search_solutions(error_type, error_message, limit=3, threshold=threshold)
# Validate results
validation = validator.quick_validate(f"{error_type}: {error_message}", solutions, threshold)
if solutions:
return jsonify({
'success': True,
'error_type': error_type,
'best_solution': solutions[0],
'alternatives': solutions[1:],
'confidence': min(solutions[0]['score'] / 100, 0.95),
'quality_score': validation['score'],
'has_accepted_answer': validation['accepted_found']
})
# Try basic fixes for common errors
if error_type == 'AttributeError' and 'NoneType' in error_message:
return jsonify({
'success': True,
'error_type': error_type,
'fix': 'Add null check: if obj is not None:',
'example': 'if obj is not None:\n obj.method()',
'confidence': 0.85,
'quality_score': 0.7
})
return jsonify({
'success': False,
'error_type': error_type,
'message': 'No fix found. Try rephrasing your error or adjusting threshold.',
'quality_score': 0.0
})
except Exception as e:
logger.error(f"Fix error: {e}")
return jsonify({'error': 'Failed to process request'}), 500
@app.route('/test', methods=['GET', 'POST'])
@require_api_key
def test_api():
"""Test endpoint"""
return jsonify({
'message': 'API is working!',
'your_key': request.api_key,
'requests_today': rate_limits[f"{request.api_key}:{datetime.now().strftime('%Y-%m-%d')}"]['requests'],
'daily_limit': request.key_info['daily_limit']
})
@app.route('/stats', methods=['GET'])
@require_api_key
def stats():
"""Get your usage stats"""
today = datetime.now().strftime('%Y-%m-%d')
limit_key = f"{request.api_key}:{today}"
return jsonify({
'api_key': request.api_key,
'name': request.key_info['name'],
'usage_today': rate_limits[limit_key]['requests'],
'daily_limit': request.key_info['daily_limit'],
'remaining': request.key_info['daily_limit'] - rate_limits[limit_key]['requests']
})
@app.route('/register', methods=['POST'])
def register():
"""Self-service API key registration"""
try:
data = request.get_json() or {}
email = data.get('email', '').strip()
name = data.get('name', '').strip()
if not email or '@' not in email:
return jsonify({'error': 'Valid email required'}), 400
if not name:
return jsonify({'error': 'Name required'}), 400
# Check if email already registered (simple check)
for key, info in API_KEYS.items():
if info.get('email') == email:
return jsonify({
'error': 'Email already registered',
'message': 'Check your email for your API key'
}), 409
# Generate new key
new_key = generate_api_key()
# Save to API_KEYS
API_KEYS[new_key] = {
'name': name,
'email': email,
'daily_limit': 500, # Beta limit
'created': datetime.now().isoformat()
}
# Save to file
save_api_keys()
# Log it
logger.info(f"New API key registered: {name} ({email})")
# In production, you'd email this. For beta, return it directly
return jsonify({
'success': True,
'api_key': new_key,
'daily_limit': 500,
'message': 'Save this key - it won\'t be shown again!',
'usage': {
'header': 'X-API-Key',
'example': f'curl -H "X-API-Key: {new_key}" {request.host_url}test'
}
})
except Exception as e:
logger.error(f"Registration error: {e}")
return jsonify({'error': 'Registration failed'}), 500
@app.route('/forgot-key', methods=['POST'])
def forgot_key():
"""Retrieve API key by email"""
try:
data = request.get_json() or {}
email = data.get('email', '').strip()
if not email:
return jsonify({'error': 'Email required'}), 400
# Find key by email
found_keys = []
for key, info in API_KEYS.items():
if info.get('email') == email:
found_keys.append({
'key': key,
'name': info['name'],
'created': info.get('created', 'Unknown')
})
if found_keys:
# In production, email this. For beta, return it
return jsonify({
'success': True,
'message': 'Your API keys:',
'keys': found_keys
})
else:
return jsonify({
'error': 'No API keys found for this email',
'message': 'Try registering at /register'
}), 404
except Exception as e:
logger.error(f"Forgot key error: {e}")
return jsonify({'error': 'Lookup failed'}), 500
# VALIDATION ENDPOINTS
@app.route('/validation/report', methods=['GET'])
@require_api_key
def validation_report():
"""Get validation metrics report"""
try:
report = validator.generate_report()
return jsonify(report)
except Exception as e:
logger.error(f"Report generation error: {e}")
return jsonify({'error': str(e)}), 500
@app.route('/validation/test', methods=['POST'])
@require_api_key
def test_thresholds():
"""Test different thresholds for a query"""
data = request.get_json() or {}
query = data.get('query', '')
if not query:
return jsonify({'error': 'No query provided'}), 400
thresholds_to_test = [0.5, 0.6, 0.7, 0.75, 0.8, 0.85]
results = {}
try:
# Extract error type if present
error_type = ''
error_message = query
if ':' in query:
parts = query.split(':', 1)
error_type = parts[0].strip()
error_message = parts[1].strip() if len(parts) > 1 else query
for threshold in thresholds_to_test:
# Run search with this threshold
search_results = db.search_solutions(error_type, error_message, limit=10, threshold=threshold)
# Validate results
validation = validator.quick_validate(query, search_results, threshold)
results[str(threshold)] = {
'result_count': len(search_results),
'quality_score': round(validation['score'], 2),
'accepted_found': validation['accepted_found'],
'accepted_position': validation['accepted_position'],
'message': validation['message']
}
# Find optimal threshold
best_threshold = max(results.items(),
key=lambda x: x[1]['quality_score'])[0]
return jsonify({
'query': query,
'threshold_tests': results,
'recommended_threshold': float(best_threshold)
})
except Exception as e:
logger.error(f"Threshold test error: {e}")
return jsonify({'error': str(e)}), 500
@app.route('/validation/problematic', methods=['GET'])
@require_api_key
def get_problematic_queries():
"""Get queries that consistently return poor results"""
try:
problematic = validator.get_problematic_queries()
return jsonify({
'problematic_queries': problematic,
'count': len(problematic)
})
except Exception as e:
logger.error(f"Error getting problematic queries: {e}")
return jsonify({'error': str(e)}), 500
# ERROR HANDLERS
@app.errorhandler(404)
def not_found(e):
return jsonify({'error': 'Endpoint not found'}), 404
@app.errorhandler(500)
def server_error(e):
return jsonify({'error': 'Server error'}), 500
# MAIN
if __name__ == '__main__':
print(f"""
🚀 ENHANCED API WITH VALIDATION READY!
================================================================================
✅ Database: {"Connected" if db.connection else "Not connected"}
✅ FTS: {CONFIG.get('FTS_ENABLED', False)}
✅ Validation: Enabled
✅ Port: {CONFIG['PORT']}
================================================================================
🔑 Beta Keys:
- 'demo' (100 requests/day)
- Use /register to get your own key
================================================================================
📊 New Features:
- Quality scoring on all searches
- Accepted answer tracking
- Threshold optimization (/validation/test)
- Search quality reports (/validation/report)
- Problematic query tracking (/validation/problematic)
================================================================================
📧 Questions? Check the docs at the root URL (/)
================================================================================
""")
# Try ngrok if available
try:
from pyngrok import ngrok
public_url = ngrok.connect(
CONFIG['PORT'],
"http",
hostname="terrier-meet-vigorously.ngrok-free.app"
)
print(f"🌐 PUBLIC URL: {public_url}")
print(f"🌐 PUBLIC URL: {public_url}")
print(f"🌐 PUBLIC URL: {public_url}")
print("================================================================================")
except Exception as e:
print(f"Ngrok error: {e}")
pass
app.run(host='0.0.0.0', port=CONFIG['PORT'], debug=False)