-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1B_main.py
More file actions
559 lines (431 loc) · 17.7 KB
/
Copy path1B_main.py
File metadata and controls
559 lines (431 loc) · 17.7 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
import os
import json
import pymupdf
import numpy as np
from llama_cpp import Llama
from sentence_transformers import SentenceTransformer, util
from tqdm import tqdm
import time
import re
from collections import defaultdict
from operator import itemgetter
MODEL_PATH = "./models/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf"
INPUT_DIR = "./input_pdfs"
OUTPUT_DIR = "./output_json"
TOP_N_RANKED_SECTIONS = 6
TOP_N_REFINED_SECTIONS = 6
slm = Llama(model_path=MODEL_PATH, n_ctx=2048, verbose=False)
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
print("Models loaded.")
def extract_structure_from_pdf(pdf_path):
doc = pymupdf.open(pdf_path)
all_elements = []
for page_num, page in enumerate(doc):
blocks = page.get_text("dict", sort=True)["blocks"]
page_elements = []
for block in blocks:
if "lines" not in block:
continue
for line in block["lines"]:
for span in line["spans"]:
text = span["text"].strip()
if not text or len(text) < 2:
continue
element = {
"text": text,
"page": page_num + 1,
"font_size": span["size"],
"is_bold": bool(span["flags"] & 2**4),
"bbox": span["bbox"],
"page_width": page.rect.width,
"page_height": page.rect.height
}
page_elements.append(element)
sorted_page_elements = sort_reading_order(page_elements)
all_elements.extend(sorted_page_elements)
doc.close()
return classify_complete_document_structure(all_elements, pdf_path)
def sort_reading_order(elements):
if not elements:
return elements
lines = group_by_lines(elements, y_tolerance=10)
sorted_elements = []
for line in lines:
sorted_line = sorted(line, key=lambda e: e["bbox"][0])
sorted_elements.extend(sorted_line)
return sorted_elements
def group_by_lines(elements, y_tolerance=10):
if not elements:
return []
elements = sorted(elements, key=lambda e: e["bbox"][1])
lines = []
current_line = []
last_y = None
for element in elements:
y_top = element["bbox"][1]
if last_y is None or abs(y_top - last_y) <= y_tolerance:
current_line.append(element)
last_y = y_top if last_y is None else (last_y + y_top) / 2
else:
if current_line:
lines.append(current_line)
current_line = [element]
last_y = y_top
if current_line:
lines.append(current_line)
return lines
def classify_complete_document_structure(elements, pdf_path):
if not elements:
return []
doc_stats = get_document_stats(elements)
classified = []
title_found = False
h1_count = 0
for i, element in enumerate(elements):
classification = classify_element_complete(element, elements, i, doc_stats, title_found, h1_count)
if classification["level"] == "TITLE":
title_found = True
elif classification["level"] == "H1":
h1_count += 1
classified.append({
"doc_path": pdf_path,
"page": element["page"],
"level": classification["level"],
"title": element["text"],
"content": element["text"],
"font_size": element["font_size"],
"is_bold": element["is_bold"],
"confidence": classification["confidence"]
})
return classified
def classify_element_complete(element, all_elements, index, doc_stats, title_found, h1_count):
text = element["text"]
if not title_found and is_document_title(element, all_elements, index, doc_stats):
return {"level": "TITLE", "confidence": 0.9}
heading_level = classify_heading(element, all_elements, index, h1_count)
if heading_level:
confidence = calculate_heading_confidence(element, heading_level, all_elements, index)
return {"level": heading_level, "confidence": confidence}
return {"level": "BODY", "confidence": 0.8}
def is_document_title(element, all_elements, index, doc_stats):
text = element["text"]
if element["page"] != 1:
return False
word_count = len(text.split())
if word_count < 2 or word_count > 12:
return False
score = 0
top_position = element["bbox"][1] / element["page_height"]
if top_position < 0.15:
score += 6
elif top_position < 0.3:
score += 4
elif top_position < 0.5:
score += 2
text_width = element["bbox"][2] - element["bbox"][0]
page_center = element["page_width"] / 2
text_center = element["bbox"][0] + text_width / 2
center_deviation = abs(text_center - page_center) / element["page_width"]
if center_deviation < 0.1:
score += 4
elif center_deviation < 0.2:
score += 2
if element["is_bold"]:
score += 3
if 3 <= word_count <= 8:
score += 3
elif word_count <= 2:
score += 1
if is_isolated_element(element, all_elements, index):
score += 2
if re.match(r'^\d+\.', text) or text.endswith(':'):
score -= 3
title_words = ["report", "analysis", "study", "guide", "manual", "document", "overview"]
if any(word in text.lower() for word in title_words):
score += 2
return score >= 8
def classify_heading(element, all_elements, index, h1_count):
text = element["text"]
word_count = len(text.split())
if word_count > 15:
return None
h1_score = 0
h2_score = 0
h3_score = 0
h4_score = 0
if re.match(r'^\d+\.\s+[A-Z]', text):
h1_score += 6
elif re.match(r'^\d+\.\d+\s+[A-Z]', text):
h2_score += 6
elif re.match(r'^\d+\.\d+\.\d+', text):
h3_score += 6
elif re.match(r'^[A-Z]\.\s+[A-Z]', text):
h2_score += 4
elif re.match(r'^\d+\)\s+[A-Z]', text):
h2_score += 4
major_sections = ["introduction", "background", "methodology", "results", "conclusion", "references", "abstract", "summary"]
if any(text.lower().strip() == section or text.lower().startswith(section + " ") for section in major_sections):
h1_score += 5
subsection_keywords = ["overview", "definition", "scope", "design", "evaluation", "analysis", "discussion"]
if any(keyword in text.lower() for keyword in subsection_keywords):
h2_score += 3
if element["is_bold"]:
h1_score += 2
h2_score += 3
h3_score += 2
if word_count <= 3:
h1_score += 2
h2_score += 1
elif word_count <= 6:
h1_score += 1
h2_score += 3
h3_score += 2
elif word_count <= 8:
h2_score += 1
h3_score += 3
h4_score += 2
elif word_count <= 10:
h3_score += 1
h4_score += 2
if is_isolated_element(element, all_elements, index):
h1_score += 2
h2_score += 3
h3_score += 2
h4_score += 1
if h1_count >= 5:
h1_score -= 2
if text.endswith('?') and word_count <= 8:
h2_score += 2
h3_score += 3
if text.endswith(':') and word_count <= 6:
h3_score += 3
h4_score += 2
if text.isupper() and len(text) > 3:
h1_score += 1
h2_score += 2
h3_score += 1
scores = {"H1": h1_score, "H2": h2_score, "H3": h3_score, "H4": h4_score}
max_score = max(scores.values())
if max_score < 4:
return None
return max(scores.keys(), key=lambda k: scores[k])
def is_isolated_element(element, all_elements, index):
if index == 0 or index == len(all_elements) - 1:
return True
current = element
isolation_threshold = 12
if index > 0:
prev = all_elements[index - 1]
if prev["page"] == current["page"]:
vertical_gap = current["bbox"][1] - prev["bbox"][3]
if vertical_gap > isolation_threshold:
return True
if index < len(all_elements) - 1:
next_elem = all_elements[index + 1]
if next_elem["page"] == current["page"]:
vertical_gap = next_elem["bbox"][1] - current["bbox"][3]
if vertical_gap > isolation_threshold:
return True
return False
def calculate_heading_confidence(element, level, all_elements, index):
base_confidence = 0.7
if re.match(r'^\d+\.\s+[A-Z]', element["text"]):
base_confidence += 0.2
if is_isolated_element(element, all_elements, index):
base_confidence += 0.1
if element["is_bold"]:
base_confidence += 0.1
return min(base_confidence, 1.0)
def get_document_stats(elements):
return {
"total_elements": len(elements),
"bold_count": sum(1 for e in elements if e["is_bold"]),
"avg_length": sum(len(e["text"]) for e in elements) / len(elements) if elements else 0
}
def process_all_pdfs_in_folder():
pdf_files = []
for filename in sorted(os.listdir(INPUT_DIR)):
if filename.lower().endswith('.pdf'):
pdf_path = os.path.join(INPUT_DIR, filename)
pdf_files.append(pdf_path)
if not pdf_files:
print(f"No PDF files found in '{INPUT_DIR}'.")
return
print(f"Found {len(pdf_files)} PDF files to process.")
start_time = time.time()
all_results = []
for pdf_path in pdf_files:
print(f"Processing: {os.path.basename(pdf_path)}")
extracted_sections = extract_structure_from_pdf(pdf_path)
for section in extracted_sections:
all_results.append({
"document": os.path.basename(section["doc_path"]),
"page": section["page"],
"level": section["level"],
"text": section["title"],
"font_size": section["font_size"],
"is_bold": section["is_bold"],
"confidence": section["confidence"]
})
pdf_order = [os.path.basename(path) for path in pdf_files]
pdf_pos_map = {pdf_name: i for i, pdf_name in enumerate(pdf_order)}
sorted_results = sorted(
all_results,
key=lambda x: (pdf_pos_map.get(x['document'], float('inf')), x['page'])
)
output_data = {
"metadata": {
"total_pdfs_processed": len(pdf_files),
"total_elements_classified": len(sorted_results),
"processing_time_seconds": round(time.time() - start_time, 2),
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
},
"extracted_structure": sorted_results
}
output_filename = f"complete_structure_{time.strftime('%Y%m%d_%H%M%S')}.json"
output_path = os.path.join(OUTPUT_DIR, output_filename)
with open(output_path, 'w') as f:
json.dump(output_data, f, indent=2)
level_counts = defaultdict(int)
for result in sorted_results:
level_counts[result["level"]] += 1
print(f"\nPDFs processed: {len(pdf_files)}")
print(f"Total elements classified: {len(sorted_results)}")
print(f"Processing time: {output_data['metadata']['processing_time_seconds']} seconds")
print(f"Results saved to: {output_path}")
for level in ["TITLE", "H1", "H2", "H3", "H4", "BODY"]:
if level in level_counts:
print(f"{level}: {level_counts[level]} elements")
for i, result in enumerate(sorted_results[:20]):
if i == 0 or result["document"] != sorted_results[i-1]["document"]:
print(f"\n{result['document']}")
print(f"[{result['level']}] {result['text'][:60]}... (p{result['page']})")
def ranking(sections, persona, jtbd, embedding_model_instance):
if not sections:
return []
query = f"As a {persona}, I need to {jtbd}."
query_embedding = embedding_model_instance.encode(query, convert_to_tensor=True)
section_contents = [section['content'] for section in sections]
section_emb = embedding_model_instance.encode(section_contents, convert_to_tensor=True)
similarity = util.cos_sim(query_embedding, section_emb)[0].cpu().numpy()
for i, section in enumerate(sections):
section['similarity'] = float(similarity[i])
return sorted(sections, key=lambda x: x['similarity'], reverse=True)
def refine_sections(ranked_sections, persona, jtbd, slm_instance, n):
if not ranked_sections:
return []
prompt_template = """You are a {persona}. Your goal is to {jtbd}.
Based on this context, provide a concise, one-sentence summary of the key insight from the following text that would be most relevant for your goal.
Text: {section_text}
Key insight:"""
result = []
for i, section in enumerate(tqdm(ranked_sections[:n], desc="Refining Sections", leave=False)):
try:
section_text = section.get("content", section.get("title", "")).strip()
if not section_text:
print(f"Warning: Empty content for section {i+1}")
result.append({
"document": os.path.basename(section["doc_path"]),
"page_number": section["page"],
"refined_text": "No content available for analysis."
})
continue
prompt = prompt_template.format(
persona=persona,
jtbd=jtbd,
section_text=section_text[:1000]
)
output = slm_instance(
prompt,
max_tokens=200,
stop=["\n\n", "Key insight:", "Text:"],
echo=False,
temperature=0.7,
top_p=0.9
)
refined_text = output['choices'][0]['text'].strip()
if not refined_text:
output_fallback = slm_instance(
prompt,
max_tokens=100,
echo=False,
temperature=0.5
)
refined_text = output_fallback['choices'][0]['text'].strip()
refined_text = clean_response(refined_text)
if not refined_text:
refined_text = f"This section contains information about {section.get('title', 'the topic')} that may be relevant for trip planning."
result.append({
"document": os.path.basename(section["doc_path"]),
"page_number": section["page"],
"refined_text": refined_text
})
except Exception as e:
print(f"Error processing section {i+1}: {str(e)}")
result.append({
"document": os.path.basename(section["doc_path"]),
"page_number": section["page"],
"refined_text": f"Error processing this section: {str(e)}"
})
return result
def clean_response(text):
if not text:
return ""
text = text.replace("Key insight:", "").strip()
text = text.replace("Summary:", "").strip()
text = re.sub(r'^[-•*]\s*', '', text)
if text and not text.endswith(('.', '!', '?')):
text += "."
if text:
text = text[0].upper() + text[1:]
return text
def main():
input_json_path = ""
for f in os.listdir(INPUT_DIR):
if f.lower().endswith(".json"):
input_json_path = os.path.join(INPUT_DIR, f)
break
if not input_json_path:
print(f"No input JSON file found in '{INPUT_DIR}'.")
return
with open(input_json_path, 'r') as f:
input_data = json.load(f)
persona = input_data.get("persona", {}).get("role", "user")
job_to_be_done = input_data.get("job_to_be_done", {}).get("task", "analyze documents")
documents = input_data.get("documents", [])
pdf_files = [os.path.join(INPUT_DIR, doc["filename"]) for doc in documents]
start_time = time.time()
all_extracted_sections = []
for pdf_path in pdf_files:
all_extracted_sections.extend(extract_structure_from_pdf(pdf_path))
ranked_sections = ranking(all_extracted_sections, persona, job_to_be_done, embedding_model)
extracted_sections_output = []
for i, section in enumerate(ranked_sections[:TOP_N_RANKED_SECTIONS]):
extracted_sections_output.append({
"document": os.path.basename(section["doc_path"]),
"section_title": section["title"],
"importance_rank": i + 1,
"page_number": section["page"]
})
subsection_analysis = refine_sections(ranked_sections, persona, job_to_be_done, slm, TOP_N_REFINED_SECTIONS)
output_data = {
"metadata": {
"input_documents": [doc["filename"] for doc in documents],
"persona": persona,
"job_to_be_done": job_to_be_done,
"processing_timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
},
"extracted_sections": extracted_sections_output,
"subsection_analysis": subsection_analysis
}
output_filename = f"{input_data['challenge_info']['test_case_name']}.json"
output_path = os.path.join(OUTPUT_DIR, output_filename)
with open(output_path, 'w') as f:
json.dump(output_data, f, indent=4)
total_time = time.time() - start_time
print(f"Total processing time: {total_time:.2f} seconds.")
print(f"Results saved to: {output_path}")
if __name__ == "__main__":
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
main()