-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
426 lines (342 loc) · 15.9 KB
/
server.py
File metadata and controls
426 lines (342 loc) · 15.9 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
from flask import Flask, request, jsonify
from flask_cors import CORS
import fitz # PyMuPDF
import zipfile
import io
import re
from pathlib import Path
import json
import tempfile
import os
from collections import defaultdict
app = Flask(__name__)
CORS(app, origins="*", allow_headers="*", methods="*")
class ConceptExtractor:
"""Extract key concepts and topics from text"""
TECH_KEYWORDS = {
'assembly': ['assembly', 'assembler', 'asm', 'x86', 'x64', 'instruction', 'opcode'],
'performance': ['optimization', 'performance', 'speed', 'cache', 'register', 'cycle'],
'memory': ['memory', 'ram', 'address', 'pointer', 'heap', 'stack', 'segment'],
'optimization': ['optimize', 'efficient', 'fast', 'slow', 'overhead', 'latency'],
'hardware': ['cpu', 'processor', 'gpu', 'core', 'bus', 'bandwidth'],
'machine_code': ['machine code', 'bytecode', 'binary', 'hex', 'encoding'],
'debugging': ['debug', 'breakpoint', 'trace', 'analyze', 'profil'],
'algorithm': ['algorithm', 'complexity', 'recursive', 'loop', 'iterate'],
'system': ['system', 'kernel', 'interrupt', 'io', 'device', 'driver'],
}
@staticmethod
def extract_concepts(text, limit=5):
"""Extract main concepts from text"""
text_lower = text.lower()
found_concepts = {}
for concept_name, keywords in ConceptExtractor.TECH_KEYWORDS.items():
count = sum(text_lower.count(kw) for kw in keywords)
if count > 0:
found_concepts[concept_name] = count
# Return top concepts
sorted_concepts = sorted(found_concepts.items(), key=lambda x: x[1], reverse=True)
return [c[0] for c in sorted_concepts[:limit]]
@staticmethod
def extract_key_quotes(text, limit=3):
"""Extract important sentences as potential quotes"""
sentences = re.split(r'[.!?]+', text)
sentences = [s.strip() for s in sentences if len(s.strip()) > 50 and len(s.strip()) < 200]
# Score sentences by presence of important keywords
scored = []
important_words = ['important', 'key', 'critical', 'essential', 'must', 'always', 'never', 'optimal']
for sent in sentences:
score = sum(1 for word in important_words if word in sent.lower())
if score > 0 or any(keyword in sent.lower() for keywords in ConceptExtractor.TECH_KEYWORDS.values() for keyword in keywords):
scored.append((sent, score))
scored.sort(key=lambda x: x[1], reverse=True)
return [s[0] for s in scored[:limit]]
@staticmethod
def calculate_importance(text):
"""Calculate importance level (high, medium, low)"""
important_indicators = ['critical', 'essential', 'important', 'must', 'key', 'fundamental']
count = sum(text.lower().count(word) for word in important_indicators)
if count >= 3:
return 'high'
elif count >= 1:
return 'medium'
else:
return 'low'
class ChapterDetector:
"""Detect chapter boundaries and structure"""
@staticmethod
def detect_chapters(text, page_markers):
"""Detect chapter boundaries from text"""
lines = text.split('\n')
chapters = []
current_chapter = None
chapter_num = 0
chapter_patterns = [
r'^chapter\s+(\d+)[:\s]',
r'^(\d+)\.\s+([A-Z][^:]+):',
r'^(?:Chapter|CHAPTER)\s+(\w+)',
]
for line_num, line in enumerate(lines):
line_stripped = line.strip()
for pattern in chapter_patterns:
if re.match(pattern, line_stripped, re.IGNORECASE):
if current_chapter:
chapters.append(current_chapter)
chapter_num += 1
current_chapter = {
'num': chapter_num,
'title': line_stripped,
'start_line': line_num,
'sections': []
}
break
if current_chapter:
chapters.append(current_chapter)
return chapters if chapters else [{'num': 1, 'title': 'Main Content', 'start_line': 0, 'sections': []}]
class PDFExtractor:
def __init__(self):
self.markers = 0
self.images = []
self.concepts = []
self.chapters = []
def extract_pdf(self, file_path):
"""Extract text with enhanced structure from PDF"""
try:
doc = fitz.open(file_path)
full_text = ""
self.markers = 0
self.images = []
# Get metadata
metadata = doc.metadata
page_count = doc.page_count
for page_num, page in enumerate(doc, 1):
# Extract text with layout
text = page.get_text("text")
# Detect headings (larger text)
blocks = page.get_text("blocks")
headings = self._extract_headings(blocks)
# Detect images
image_list = page.get_images()
if image_list:
full_text += f"[IMAGE]: {len(image_list)} image(s) on page {page_num}\n"
self.markers += 1
self.images.extend([(page_num, len(image_list))])
if text.strip():
full_text += f"[SECTION]: Page {page_num}\n"
if headings:
full_text += f"[HEADING]: {headings[0]}\n"
self.markers += 1
full_text += text + "\n\n"
self.markers += 1
# Extract concepts from full text
self.concepts = ConceptExtractor.extract_concepts(full_text)
# Detect chapters
self.chapters = ChapterDetector.detect_chapters(full_text, [])
doc.close()
return {
'text': full_text.strip(),
'metadata': {
'title': metadata.get('title', 'Unknown') if metadata else 'Unknown',
'author': metadata.get('author', 'Unknown') if metadata else 'Unknown',
'pages': page_count,
'creation_date': metadata.get('creationDate', 'N/A') if metadata else 'N/A'
},
'markers': self.markers,
'images_found': len(self.images),
'concepts': self.concepts,
'chapters': len(self.chapters)
}
except Exception as e:
return {'error': str(e)}
def _extract_headings(self, blocks):
"""Extract potential headings from text blocks"""
headings = []
for block in blocks:
if len(block) > 4 and isinstance(block[4], str):
text = block[4].strip()
if len(text) < 100 and len(text) > 5:
headings.append(text)
return headings[:1]
class OutputFormatter:
"""Generate multiple output formats optimized for different uses"""
@staticmethod
def generate_enhanced_txt(text, metadata, concepts):
"""Generate enhanced TXT with metadata markers for Claude"""
output = ""
# Header
output += f"[TITLE]: {metadata.get('title', 'Unknown')}\n"
output += f"[AUTHOR]: {metadata.get('author', 'Unknown')}\n"
output += f"[PAGES]: {metadata.get('pages', 'Unknown')}\n"
output += f"[CONCEPTS]: {', '.join(concepts)}\n"
output += f"[FORMAT]: Enhanced for AI Processing\n"
output += "=" * 80 + "\n\n"
# Process sections
section_blocks = re.split(r'\[SECTION\]:', text)
for idx, section in enumerate(section_blocks[1:], 1):
lines = section.split('\n')
if lines:
# Parse page number and heading
page_match = re.match(r'\s*Page (\d+)', lines[0])
page_num = int(page_match.group(1)) if page_match else idx
heading = ""
content_start = 1
if len(lines) > 1 and '[HEADING]' in lines[1]:
heading = lines[1].replace('[HEADING]:', '').strip()
content_start = 2
# Extract concepts from this section
section_text = '\n'.join(lines[content_start:])
section_concepts = ConceptExtractor.extract_concepts(section_text, limit=3)
# Extract key quotes
key_quotes = ConceptExtractor.extract_key_quotes(section_text, limit=2)
# Calculate importance
importance = ConceptExtractor.calculate_importance(section_text)
# Write enhanced section
output += f"[SECTION_ID]: {idx}\n"
output += f"[PAGE]: {page_num}\n"
output += f"[HEADING]: {heading}\n"
output += f"[CONCEPTS]: {', '.join(section_concepts)}\n"
output += f"[IMPORTANCE]: {importance}\n"
if key_quotes:
output += f"[KEY_QUOTES]: {len(key_quotes)}\n"
for quote in key_quotes:
output += f" - {quote[:100]}...\n"
output += "\n"
output += section_text + "\n"
output += "-" * 80 + "\n\n"
return output
@staticmethod
def generate_structured_json(text, metadata, concepts):
"""Generate structured JSON for semantic search and Claude reference"""
sections = []
section_blocks = re.split(r'\[SECTION\]:', text)
for idx, block in enumerate(section_blocks[1:], 1):
lines = block.split('\n')
if lines:
page_match = re.match(r'\s*Page (\d+)', lines[0])
page_num = int(page_match.group(1)) if page_match else idx
heading = ""
content_start = 1
if len(lines) > 1 and '[HEADING]' in lines[1]:
heading = lines[1].replace('[HEADING]:', '').strip()
content_start = 2
content = '\n'.join(lines[content_start:]).strip()
# Extract metadata for this section
section_concepts = ConceptExtractor.extract_concepts(content, limit=3)
key_quotes = ConceptExtractor.extract_key_quotes(content, limit=2)
importance = ConceptExtractor.calculate_importance(content)
section_obj = {
"section_id": idx,
"page": page_num,
"heading": heading,
"content": content,
"metadata": {
"concepts": section_concepts,
"topics": concepts,
"importance": importance,
"key_quotes": key_quotes,
"word_count": len(content.split())
}
}
sections.append(section_obj)
return {
"book_metadata": {
"title": metadata.get('title', 'Unknown'),
"author": metadata.get('author', 'Unknown'),
"pages": metadata.get('pages', 0),
"total_sections": len(sections),
"concepts": concepts,
"creation_date": metadata.get('creation_date', 'N/A')
},
"sections": sections
}
@staticmethod
def generate_jsonl(text, metadata, concepts):
"""Generate JSONL for fine-tuning with enhanced metadata"""
lines = []
section_blocks = re.split(r'\[SECTION\]:', text)
for idx, block in enumerate(section_blocks[1:], 1):
block_lines = block.split('\n')
if block_lines:
page_match = re.match(r'\s*Page (\d+)', block_lines[0])
page_num = int(page_match.group(1)) if page_match else idx
heading = ""
content_start = 1
if len(block_lines) > 1 and '[HEADING]' in block_lines[1]:
heading = block_lines[1].replace('[HEADING]:', '').strip()
content_start = 2
content = '\n'.join(block_lines[content_start:]).strip()
if content:
section_concepts = ConceptExtractor.extract_concepts(content, limit=3)
pair = {
"section_id": idx,
"page": page_num,
"heading": heading,
"instruction": f"Explain this concept from {metadata.get('title', 'the book')}: {heading[:100]}...",
"input": "",
"output": content,
"concepts": section_concepts,
"source": metadata.get('title', 'Unknown'),
"metadata": {
"importance": ConceptExtractor.calculate_importance(content),
"word_count": len(content.split())
}
}
lines.append(json.dumps(pair))
return '\n'.join(lines)
@app.route('/extract', methods=['POST'])
def extract_file():
"""Handle file extraction with multiple output formats - optimized for multi-file aggregation"""
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
file_ext = file.filename.lower().split('.')[-1]
# Save file temporarily
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, file.filename)
file.save(temp_path)
try:
if file_ext == 'pdf':
extractor = PDFExtractor()
result = extractor.extract_pdf(temp_path)
else:
return jsonify({'error': 'Currently only PDF supported (EPUB/MOBI in progress)'}), 400
if 'error' in result:
return jsonify(result), 400
# Generate multiple formats
base_result = result.copy()
text = result['text']
metadata = result['metadata']
concepts = result['concepts']
# Generate all formats
enhanced_txt = OutputFormatter.generate_enhanced_txt(text, metadata, concepts)
structured_json = OutputFormatter.generate_structured_json(text, metadata, concepts)
jsonl = OutputFormatter.generate_jsonl(text, metadata, concepts)
return jsonify({
'success': True,
'filename': file.filename,
'base_info': base_result,
'formats': {
'enhanced_txt': enhanced_txt,
'structured_json': structured_json,
'jsonl': jsonl
},
'stats': {
'total_sections': len(structured_json['sections']),
'concepts_found': len(concepts),
'words': sum(s['metadata']['word_count'] for s in structured_json['sections']),
'pages': metadata.get('pages', 0)
}
})
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
# Cleanup
try:
Path(temp_path).unlink(missing_ok=True)
except:
pass
@app.route('/health', methods=['GET'])
def health():
"""Health check"""
return jsonify({'status': 'ok'})
if __name__ == '__main__':
app.run(debug=True, port=5000)