-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_header.txt
More file actions
100 lines (84 loc) · 2.95 KB
/
api_header.txt
File metadata and controls
100 lines (84 loc) · 2.95 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
#!/usr/bin/env python3
"""
ENHANCED SELF-HEALING API WITH ULTIMATE SEARCH SYSTEM
Complete working API with FTS5 search and validation
"""
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, lru_cache
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"""
try:
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,