File size: 22,106 Bytes
c8a8b66
09310e8
44130c7
09310e8
 
f52ac99
4ac61d6
09310e8
f52ac99
d488fb3
012e651
dc65367
 
 
4ccce7a
51db8d9
4ccce7a
51db8d9
4ccce7a
51db8d9
4ccce7a
 
51db8d9
4ccce7a
 
012e651
4ccce7a
44130c7
51db8d9
8c4ca9e
 
 
 
 
 
51db8d9
 
8c4ca9e
 
 
 
 
 
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
51db8d9
8c4ca9e
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
 
 
51db8d9
 
8c4ca9e
 
 
 
 
 
 
 
 
 
 
 
51db8d9
 
 
8c4ca9e
 
51db8d9
8c4ca9e
 
 
 
 
 
51db8d9
8c4ca9e
 
 
51db8d9
8c4ca9e
 
 
 
51db8d9
 
8c4ca9e
51db8d9
8c4ca9e
 
51db8d9
 
8c4ca9e
 
51db8d9
8c4ca9e
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
51db8d9
8c4ca9e
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
51db8d9
 
8c4ca9e
 
 
 
 
 
 
 
 
51db8d9
 
 
8c4ca9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
 
51db8d9
 
8c4ca9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
51db8d9
8c4ca9e
51db8d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c4ca9e
 
 
 
 
 
51db8d9
8c4ca9e
 
 
 
 
 
 
51db8d9
8c4ca9e
51db8d9
8c4ca9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09310e8
 
4ccce7a
 
51db8d9
09310e8
4ccce7a
09310e8
 
 
4ccce7a
09310e8
 
4ccce7a
09310e8
 
 
4ccce7a
09310e8
 
4ccce7a
09310e8
 
4ccce7a
09310e8
 
4ccce7a
09310e8
 
4ccce7a
 
 
 
 
51db8d9
44130c7
51db8d9
4ccce7a
09310e8
 
 
51db8d9
 
 
 
 
09310e8
4ccce7a
09310e8
 
51db8d9
 
 
 
 
 
d938595
51db8d9
 
 
d938595
51db8d9
 
4ccce7a
51db8d9
 
8c4ca9e
51db8d9
 
 
 
 
 
 
 
 
 
4ccce7a
51db8d9
 
d938595
51db8d9
 
 
4ccce7a
51db8d9
 
09310e8
4ccce7a
51db8d9
 
 
 
 
4ccce7a
51db8d9
 
 
 
 
4ccce7a
51db8d9
 
4ccce7a
51db8d9
 
 
 
 
 
 
 
 
 
 
 
 
 
4ccce7a
51db8d9
 
 
 
 
 
09310e8
 
 
 
 
4ccce7a
09310e8
 
 
 
 
51db8d9
09310e8
51db8d9
 
09310e8
51db8d9
44130c7
51db8d9
09310e8
51db8d9
4ccce7a
51db8d9
 
4ccce7a
51db8d9
 
09310e8
51db8d9
 
 
 
 
 
 
 
 
 
4ccce7a
51db8d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ccce7a
44130c7
51db8d9
 
 
 
 
 
4ccce7a
51db8d9
 
 
 
4ccce7a
51db8d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c4ca9e
51db8d9
5798c94
51db8d9
 
 
8c4ca9e
51db8d9
 
 
 
 
 
 
8c4ca9e
 
51db8d9
 
 
 
 
1143358
51db8d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d938595
51db8d9
 
 
 
 
d938595
51db8d9
 
d938595
51db8d9
 
 
d938595
51db8d9
 
d938595
51db8d9
 
 
 
d938595
51db8d9
 
 
 
 
 
 
 
 
d938595
51db8d9
1143358
51db8d9
 
1143358
51db8d9
 
 
 
d681c26
51db8d9
d5b7e45
51db8d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d90c86
51db8d9
 
09310e8
1143358
51db8d9
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import json
import requests
from io import BytesIO
from datetime import datetime
import pandas as pd
import fitz  # PyMuPDF
from collections import defaultdict, Counter
from urllib.parse import urlparse, unquote   
import re
import difflib
import copy
import urllib.parse
import logging
from difflib import SequenceMatcher

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
    ]
)

logger = logging.getLogger(__name__)

# Constants
top_margin = 70
bottom_margin = 85

def getLocation_of_header(doc, headerText, expected_page=None):
    locations = []
    
    expectedpageNorm = expected_page
    page = doc[expectedpageNorm]
    page_height = page.rect.height
    rects = page.search_for(headerText)

    for r in rects:
        y = r.y0

        # Skip headers in top or bottom margin
        if y <= top_margin:
            continue
        if y >= page_height - bottom_margin:
            continue

        locations.append({
            "headerText": headerText,
            "page": expectedpageNorm,
            "x": r.x0,
            "y": y
        })
    return locations

def filter_headers_outside_toc(headers, toc_pages):
    toc_pages_set = set(toc_pages)
    filtered = []
    
    for h in headers:
        page = h[2]
        if page is None:
            continue
        if page in toc_pages_set:
            continue
        filtered.append(h)
    
    return filtered

def headers_with_location(doc, llm_headers):
    headersJson = []

    for h in llm_headers:
        text = h["text"]
        llm_page = h["page"]
        
        locations = getLocation_of_header(doc, text, llm_page)

        if locations:
            for loc in locations:
                page = doc.load_page(loc["page"])
                fontsize = None

                for block in page.get_text("dict")["blocks"]:
                    if block.get("type") != 0:
                        continue
                    for line in block.get("lines", []):
                        line_text = "".join(span["text"] for span in line["spans"]).strip()
                        if normalize(line_text) == normalize(text):
                            if line["spans"]:
                                fontsize = line["spans"][0]["size"]
                                break
                    if fontsize:
                        break
                
                entry = [
                    text,
                    fontsize,
                    loc["page"],
                    loc["y"],
                    h["suggested_level"],
                    loc.get("x", 0),
                ]
                if entry not in headersJson:
                    headersJson.append(entry)
    
    return headersJson

def build_hierarchy_from_llm(headers):
    nodes = []
    
    # Build nodes
    for h in headers:
        if len(h) < 6:
            continue

        text, size, page, y, level, x = h
        
        if level is None:
            continue
        
        try:
            level = int(level)
        except Exception:
            continue

        node = {
            "text": text,
            "page": page if page is not None else -1,
            "x": x if x is not None else -1,
            "y": y if y is not None else -1,
            "size": size,
            "bold": False,
            "color": None,
            "font": None,
            "children": [],
            "is_numbered": is_numbered(text),
            "original_size": size,
            "norm_text": normalize(text),
            "level": level,
        }
        nodes.append(node)

    if not nodes:
        return []

    # Sort top-to-bottom
    nodes.sort(key=lambda x: (x["page"], x["y"]))

    # Normalize levels
    min_level = min(n["level"] for n in nodes)
    for n in nodes:
        n["level"] -= min_level

    # Build hierarchy
    root = []
    stack = []
    added_level0 = set()

    for header in nodes:
        lvl = header["level"]

        if lvl < 0:
            continue

        if lvl == 0:
            key = (header["norm_text"], header["page"])
            if key in added_level0:
                continue
            added_level0.add(key)

        while stack and stack[-1]["level"] >= lvl:
            stack.pop()

        parent = stack[-1] if stack else None

        if parent:
            header["path"] = parent["path"] + [header["norm_text"]]
            parent["children"].append(header)
        else:
            header["path"] = [header["norm_text"]]
            root.append(header)

        stack.append(header)

    # Enforce nesting
    def enforce_nesting(node_list, parent_level=-1):
        for node in node_list:
            if node["level"] <= parent_level:
                node["level"] = parent_level + 1
            enforce_nesting(node["children"], node["level"])

    enforce_nesting(root)

    # Cleanup
    if any(h["level"] == 0 for h in root):
        root = [
            h for h in root
            if not (h["level"] == 0 and not h["children"])
        ]

    return enforce_level_hierarchy(root)

def get_regular_font_size_and_color(doc):
    font_sizes = []
    colors = []
    fonts = []

    # Check only first few pages for efficiency
    for page_num in range(min(len(doc), 10)):
        page = doc.load_page(page_num)
        for span in page.get_text("dict")["blocks"]:
            if "lines" in span:
                for line in span["lines"]:
                    for span in line["spans"]:
                        font_sizes.append(span['size'])
                        colors.append(span['color'])
                        fonts.append(span['font'])

    most_common_font_size = Counter(font_sizes).most_common(1)[0][0] if font_sizes else 12
    most_common_color = Counter(colors).most_common(1)[0][0] if colors else 0
    most_common_font = Counter(fonts).most_common(1)[0][0] if fonts else "Helvetica"

    return most_common_font_size, most_common_color, most_common_font

def normalize_text(text):
    if text is None:
        return ""
    return re.sub(r'\s+', ' ', text.strip().lower())

def get_spaced_text_from_spans(spans):
    return normalize_text(" ".join(span["text"].strip() for span in spans))

def is_numbered(text):
    return bool(re.match(r'^\d', text.strip()))

def is_similar(a, b, threshold=0.85):
    return SequenceMatcher(None, a, b).ratio() > threshold

def normalize(text):
    text = text.lower()
    text = re.sub(r'\.{2,}', '', text)
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

def clean_toc_entry(toc_text):
    return re.sub(r'[\.\s]+\d+.*$', '', toc_text).strip('. ')

def enforce_level_hierarchy(headers):
    def process_node_list(node_list, parent_level=-1):
        i = 0
        while i < len(node_list):
            node = node_list[i]
            if node['level'] == 2 and parent_level != 1:
                node_list.pop(i)
                continue
            process_node_list(node['children'], node['level'])
            i += 1

    process_node_list(headers)
    return headers

def highlight_boxes(doc, highlights, stringtowrite, fixed_width=500):
    for page_num, bbox in highlights.items():
        page = doc.load_page(page_num)
        page_width = page.rect.width

        orig_rect = fitz.Rect(bbox)
        rect_height = orig_rect.height
        
        if rect_height > 30:
            center_x = page_width / 2
            new_x0 = center_x - fixed_width / 2
            new_x1 = center_x + fixed_width / 2
            new_rect = fitz.Rect(new_x0, orig_rect.y0, new_x1, orig_rect.y1)

            annot = page.add_rect_annot(new_rect)
            if stringtowrite.startswith('Not'):
                annot.set_colors(stroke=(0.5, 0.5, 0.5), fill=(0.5, 0.5, 0.5))
            else:
                annot.set_colors(stroke=(1, 1, 0), fill=(1, 1, 0))
            annot.set_opacity(0.3)
            annot.update()

            text = '[' + stringtowrite + ']'
            annot1 = page.add_freetext_annot(
                new_rect,
                text,
                fontsize=15,
                fontname='helv',
                text_color=(1, 0, 0),
                rotate=page.rotation,
                align=2
            )
            annot1.update()

def get_leaf_headers_with_paths(listtoloop, path=None, output=None):
    if path is None:
        path = []
    if output is None:
        output = []
    
    for header in listtoloop:
        current_path = path + [header['text']]
        if not header['children']:
            if header['level'] != 0 and header['level'] != 1:
                output.append((header, current_path))
        else:
            get_leaf_headers_with_paths(header['children'], current_path, output)
    
    return output

def words_match_ratio(text1, text2):
    words1 = set(text1.split())
    words2 = set(text2.split())
    if not words1 or not words2:
        return 0.0
    common_words = words1 & words2
    return len(common_words) / len(words1)

def same_start_word(s1, s2):
    words1 = s1.strip().split()
    words2 = s2.strip().split()
    if words1 and words2:
        return words1[0].lower() == words2[0].lower()
    return False

def get_toc_page_numbers(doc, max_pages_to_check=15):
    toc_pages = []
    logger.debug(f"Starting TOC detection, checking first {max_pages_to_check} pages")
    
    dot_pattern = re.compile(r"\.{2,}")
    title_pattern = re.compile(r"^\s*(table of contents|contents|index)\s*$", re.IGNORECASE)
    
    for page_num in range(min(len(doc), max_pages_to_check)):
        page = doc.load_page(page_num)
        blocks = page.get_text("dict")["blocks"]
        
        dot_line_count = 0
        has_toc_title = False
        
        for block in blocks:
            for line in block.get("lines", []):
                line_text = " ".join([span["text"] for span in line["spans"]]).strip()
                
                if dot_pattern.search(line_text):
                    dot_line_count += 1
                
                if title_pattern.match(line_text):
                    has_toc_title = True
        
        if has_toc_title or dot_line_count >= 1:
            toc_pages.append(page_num)
    
    if toc_pages:
        last_toc_page = toc_pages[0]
        result = list(range(0, last_toc_page + 1))
        logger.info(f"TOC pages found: {result}")
        return result
    
    logger.info("No TOC pages found")
    return []

def openPDF(pdf_path):
    logger.info(f"Opening PDF from URL: {pdf_path}")
    pdf_path = pdf_path.replace('dl=0', 'dl=1')
    response = requests.get(pdf_path)
    
    if response.status_code != 200:
        logger.error(f"Failed to download PDF. Status code: {response.status_code}")
        return None
    
    pdf_content = BytesIO(response.content)
    doc = fitz.open(stream=pdf_content, filetype="pdf")
    logger.info(f"PDF opened successfully, {len(doc)} pages")
    return doc

def is_header(span, regular_font_size, regular_color, regular_font, allheaders_LLM=None):
    """
    Determine if a text span is a header based on font characteristics.
    """
    # Check font size (headers are typically larger than regular text)
    size_ok = span.get('size', 0) > regular_font_size * 1.1
    
    # Check if it's bold (common for headers)
    flags = span.get('flags', 0)
    is_bold = bool(flags & 2)
    
    # Check font family
    font_ok = span.get('font') != regular_font
    
    # Check color
    color_ok = span.get('color') != regular_color
    
    # Check if text matches LLM-identified headers
    text_match = False
    if allheaders_LLM and 'text' in span:
        span_text = span['text'].strip()
        if span_text:
            norm_text = normalize_text(span_text)
            text_match = any(
                normalize_text(header) == norm_text 
                for header in allheaders_LLM
            )
    
    # A span is considered a header if it meets multiple criteria
    return (size_ok and (is_bold or font_ok or color_ok)) or text_match

def identify_headers_with_openrouter(pdf_path, model, LLM_prompt, pages_to_check=None):
    """Simplified version for HuggingFace Spaces"""
    logger.info("Starting header identification")
    
    doc = openPDF(pdf_path)
    if doc is None:
        return []
    
    # Use environment variable for API key
    api_key = os.getenv("OPENROUTER_API_KEY")
    if not api_key:
        logger.warning("No OpenRouter API key found. Using fallback heuristics.")
        return fallback_header_detection(doc)
    
    # Simplified prompt for faster processing
    simplified_prompt = """
    Analyze the following text lines from a PDF document. 
    Identify which lines are headers/titles and suggest a hierarchy level (1 for main headers, 2 for subheaders, etc.).
    Return only a JSON array of objects with keys: text, page, suggested_level.
    
    Example: [{"text": "Introduction", "page": 3, "suggested_level": 1}, ...]
    """
    
    # Collect text from first 20 pages max for HuggingFace
    total_pages = len(doc)
    start_page = 0
    end_page = min(20, total_pages)  # Limit pages for HuggingFace
    
    lines_for_prompt = []
    for pno in range(start_page, end_page):
        page = doc.load_page(pno)
        text = page.get_text()
        if text.strip():
            lines = text.split('\n')
            for line in lines:
                if line.strip():
                    lines_for_prompt.append(f"PAGE {pno+1}: {line.strip()}")
    
    if not lines_for_prompt:
        return fallback_header_detection(doc)
    
    prompt = simplified_prompt + "\n\nLines:\n" + "\n".join(lines_for_prompt[:100])  # Limit lines
    
    # Make API call
    url = "https://openrouter.ai/api/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }
    
    body = {
        "model": model,
        "messages": [
            {
                "role": "user",
                "content": prompt
            }
        ],
        "max_tokens": 2000
    }
    
    try:
        resp = requests.post(url, headers=headers, json=body, timeout=30)
        resp.raise_for_status()
        rj = resp.json()
        
        # Extract response
        text_reply = rj.get('choices', [{}])[0].get('message', {}).get('content', '')
        
        # Parse JSON from response
        import json as json_module
        try:
            # Find JSON array in response
            start = text_reply.find('[')
            end = text_reply.rfind(']') + 1
            if start != -1 and end != -1:
                json_str = text_reply[start:end]
                parsed = json_module.loads(json_str)
            else:
                parsed = []
        except:
            parsed = []
        
        # Format output
        out = []
        for obj in parsed:
            if isinstance(obj, dict):
                t = obj.get('text')
                page = obj.get('page')
                level = obj.get('suggested_level')
                if t and page:
                    out.append({
                        'text': t,
                        'page': page - 1,  # Convert to 0-indexed
                        'suggested_level': level,
                        'confidence': 1.0
                    })
        
        logger.info(f"Identified {len(out)} headers")
        return out
        
    except Exception as e:
        logger.error(f"OpenRouter API error: {e}")
        return fallback_header_detection(doc)

def fallback_header_detection(doc):
    """Fallback header detection using font heuristics"""
    headers = []
    
    # Check only first 30 pages for efficiency
    for page_num in range(min(len(doc), 30)):
        page = doc.load_page(page_num)
        blocks = page.get_text("dict")["blocks"]
        
        for block in blocks:
            if block.get("type") == 0:  # Text block
                for line in block.get("lines", []):
                    if line.get("spans"):
                        span = line["spans"][0]
                        text = span.get("text", "").strip()
                        
                        # Simple heuristics for headers
                        if (text and 
                            len(text) < 100 and  # Headers are usually short
                            not text.endswith('.') and  # Not regular sentences
                            text[0].isupper() and  # Starts with capital
                            any(c.isalpha() for c in text)):  # Contains letters
                            
                            headers.append({
                                'text': text,
                                'page': page_num,
                                'suggested_level': 2 if len(text.split()) < 5 else 3,
                                'confidence': 0.7
                            })
    
    # Deduplicate
    unique_headers = []
    seen = set()
    for h in headers:
        key = (h['text'].lower(), h['page'])
        if key not in seen:
            seen.add(key)
            unique_headers.append(h)
    
    return unique_headers

def process_single_pdf(pdf_path, model="openai/gpt-3.5-turbo", LLM_prompt=None):
    """Process a single PDF for HuggingFace Spaces"""
    logger.info(f"Processing PDF: {pdf_path}")
    
    try:
        # Open PDF
        doc = openPDF(pdf_path)
        if doc is None:
            return None, None
        
        # Get basic document info
        toc_pages = get_toc_page_numbers(doc)
        
        # Identify headers (with fallback)
        if LLM_prompt and os.getenv("OPENROUTER_API_KEY"):
            identified_headers = identify_headers_with_openrouter(pdf_path, model, LLM_prompt)
        else:
            identified_headers = fallback_header_detection(doc)
        
        # Process headers
        headers_json = headers_with_location(doc, identified_headers)
        headers = filter_headers_outside_toc(headers_json, toc_pages)
        hierarchy = build_hierarchy_from_llm(headers)
        
        # Create simple output
        results = []
        for header in hierarchy:
            results.append({
                "text": header.get("text", ""),
                "page": header.get("page", 0) + 1,
                "level": header.get("level", 0),
                "font_size": header.get("size", 0)
            })
        
        # Create DataFrame
        df = pd.DataFrame(results)
        
        # Save to Excel
        output_path = "header_analysis.xlsx"
        df.to_excel(output_path, index=False)
        
        logger.info(f"Processed {len(results)} headers")
        return output_path, df.head(10).to_dict('records')
        
    except Exception as e:
        logger.error(f"Error processing PDF: {e}")
        return None, None

def simple_interface(pdf_path, use_llm=True, model="openai/gpt-3.5-turbo"):
    """
    Simplified interface for HuggingFace Spaces
    """
    logger.info("Starting PDF header extraction")
    
    if not pdf_path:
        return "Please provide a PDF URL", None, None
    
    try:
        # Default prompt
        LLM_prompt = """Analyze the text lines and identify headers with hierarchy levels."""
        
        # Process the PDF
        excel_path, sample_data = process_single_pdf(pdf_path, model, LLM_prompt if use_llm else None)
        
        if excel_path and os.path.exists(excel_path):
            # Read the file content for download
            with open(excel_path, 'rb') as f:
                file_content = f.read()
            
            # Create sample preview
            if sample_data:
                preview_html = "<h3>Sample Headers Found:</h3><table border='1' style='width:100%'>"
                preview_html += "<tr><th>Text</th><th>Page</th><th>Level</th></tr>"
                for item in sample_data:
                    preview_html += f"<tr><td>{item['text'][:50]}...</td><td>{item['page']}</td><td>{item['level']}</td></tr>"
                preview_html += "</table>"
            else:
                preview_html = "<p>No headers found or could not process.</p>"
            
            return preview_html, (excel_path, file_content), "Processing completed successfully!"
        else:
            return "<p>Failed to process the PDF. Please check the URL and try again.</p>", None, "Processing failed."
            
    except Exception as e:
        logger.error(f"Error in interface: {e}")
        return f"<p>Error: {str(e)}</p>", None, "Error occurred during processing."

# Create Gradio interface for HuggingFace
iface = gr.Interface(
    fn=simple_interface,
    inputs=[
        gr.Textbox(
            label="PDF URL",
            placeholder="Enter the URL of a PDF file...",
            info="Make sure the PDF is publicly accessible"
        ),
        gr.Checkbox(
            label="Use AI Analysis (OpenRouter)",
            value=False,
            info="Requires OPENROUTER_API_KEY environment variable"
        ),
        gr.Dropdown(
            label="AI Model",
            choices=["openai/gpt-3.5-turbo", "anthropic/claude-3-haiku", "google/gemini-pro"],
            value="openai/gpt-3.5-turbo",
            visible=False  # Hidden for simplicity
        )
    ],
    outputs=[
        gr.HTML(label="Results Preview"),
        gr.File(label="Download Excel Results"),
        gr.Textbox(label="Status")
    ],
    title="PDF Header Extractor",
    description="Extract headers from PDF documents and analyze their hierarchy. Upload a publicly accessible PDF URL to begin.",
    examples=[
        ["https://arxiv.org/pdf/2305.15334.pdf", False],
        ["https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", False]
    ],
    cache_examples=False,
    allow_flagging="never"
)

# Launch with HuggingFace-friendly settings
if __name__ == "__main__":
    # For HuggingFace Spaces, use launch with specific settings
    iface.launch(
        debug=False,  # Disable debug for production
        show_api=False,
        server_name="0.0.0.0",
        server_port=7860
    )