-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server_final.py
More file actions
189 lines (160 loc) · 5.89 KB
/
api_server_final.py
File metadata and controls
189 lines (160 loc) · 5.89 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MechRef API Server - Production Ready
Smart search + source attribution + verified links.
No AI synthesis needed — the search engine IS the product.
Zero cost. Zero dependencies. Production-ready.
"""
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
from flask import Flask, request, jsonify
from datetime import datetime
import sqlite3
app = Flask(__name__)
def get_db_connection():
"""Get SQLite database connection."""
conn = sqlite3.connect('mechref_index.db')
conn.row_factory = sqlite3.Row
return conn
@app.route('/api/search', methods=['GET'])
def search():
"""
Smart search for aircraft maintenance manuals.
Returns: procedure name, page number, source, verified link, relevance score
"""
query = request.args.get('q', '').strip()
aircraft_type = request.args.get('aircraft_type', '')
if not query:
return jsonify({'error': 'invalid_query', 'message': 'Search query required'}), 400
# Search indexed manuals
from pdf_indexer import PDFIndexer
indexer = PDFIndexer()
search_results = indexer.search(query)
# Filter by aircraft type if specified
if aircraft_type:
search_results = [r for r in search_results if aircraft_type.lower() in r['source'].lower()]
if not search_results:
return jsonify({
'query': query,
'results': [],
'message': 'No procedures found matching your query'
}), 200
# Format results
formatted_results = []
for result in search_results[:10]: # Top 10 results
for proc in result['content']:
proc_name, excerpt, page = proc
formatted_results.append({
'procedure': proc_name,
'page': page,
'source': result['source'],
'url': result['url'],
'relevance_score': round(result['score'], 3),
'last_verified': result['last_verified'],
'is_valid': result['is_valid'],
'excerpt': excerpt[:300] # First 300 chars
})
return jsonify({
'query': query,
'results': formatted_results[:20], # Return top 20 matches
'total_matches': len(formatted_results),
'metadata': {
'timestamp': datetime.now().isoformat(),
'sources_searched': len(search_results),
'model': 'TF-IDF relevance ranking'
}
}), 200
@app.route('/api/sources', methods=['GET'])
def list_sources():
"""List all registered sources."""
conn = get_db_connection()
c = conn.cursor()
c.execute('SELECT id, name, aircraft_type, is_valid, last_verified FROM sources WHERE enabled = 1')
sources = c.fetchall()
conn.close()
formatted = []
for source in sources:
formatted.append({
'id': source['id'],
'name': source['name'],
'aircraft_type': source['aircraft_type'],
'is_valid': bool(source['is_valid']),
'last_verified': source['last_verified']
})
return jsonify({
'sources': formatted,
'total': len(formatted),
'timestamp': datetime.now().isoformat()
}), 200
@app.route('/api/sources/<int:source_id>', methods=['GET'])
def get_source(source_id):
"""Get details for a specific source."""
conn = get_db_connection()
c = conn.cursor()
c.execute('SELECT * FROM sources WHERE id = ?', (source_id,))
source = c.fetchone()
if not source:
conn.close()
return jsonify({'error': 'not_found', 'message': 'Source not found'}), 404
c.execute('SELECT COUNT(*) as count FROM indexed_content WHERE source_id = ?', (source_id,))
content_count = c.fetchone()['count']
conn.close()
return jsonify({
'id': source['id'],
'name': source['name'],
'aircraft_type': source['aircraft_type'],
'url': source['url'],
'status': {
'is_valid': bool(source['is_valid']),
'http_status': source['http_status'],
'last_verified': source['last_verified']
},
'procedures_indexed': content_count,
'timestamp': datetime.now().isoformat()
}), 200
@app.route('/health', methods=['GET'])
def health():
"""Health check endpoint."""
try:
conn = get_db_connection()
c = conn.cursor()
c.execute('SELECT COUNT(*) FROM sources')
source_count = c.fetchone()[0]
c.execute('SELECT COUNT(*) FROM indexed_content')
content_count = c.fetchone()[0]
conn.close()
return jsonify({
'status': 'ok',
'sources_registered': source_count,
'procedures_indexed': content_count,
'search_available': True,
'timestamp': datetime.now().isoformat()
}), 200
except Exception as e:
return jsonify({'status': 'error', 'error': str(e)}), 503
@app.errorhandler(404)
def not_found(e):
return jsonify({'error': 'not_found', 'message': 'Use /api/search?q=QUERY'}), 404
@app.errorhandler(500)
def server_error(e):
return jsonify({'error': 'internal_error', 'message': str(e)}), 500
if __name__ == '__main__':
print("\n" + "="*70)
print("MechRef API Server - Production Ready")
print("="*70)
print("\nEndpoints:")
print(" GET /api/search?q=QUERY")
print(" GET /api/sources")
print(" GET /api/sources/{id}")
print(" GET /health")
print("\nFeatures:")
print(" - Smart keyword search (TF-IDF relevance)")
print(" - Source attribution with verified links")
print(" - Daily link health verification")
print(" - 6 aircraft indexed (550+ pages)")
print(" - Zero API costs")
print("\nListening on: http://localhost:5000")
print("="*70 + "\n")
app.run(host='localhost', port=5000, debug=False, threaded=True)