# main.py
import os
import sys

# 1. Block hardware-level multi-device discovery loops 
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import torch

# 2. Hard monkeypatch PyTorch's MPS detection before ANY machine learning imports occur
# This stops underlying huggingface models from forcing device="mps" internally
torch.backends.mps.is_available = lambda: False
torch.backends.mps.is_built = lambda: False

# Force standard CPU allocation for safety
torch.set_default_device("cpu")

import time
from docling.document_converter import DocumentConverter

def main():
    source = "https://arxiv.org/pdf/2408.09869" 
    
    print("🚀 Initializing Docling engine with hardware-isolated CPU routing...")
    start_time = time.time()
    
    # This will now natively spin up on standard CPU threads
    converter = DocumentConverter()
    
    print("⏳ Processing document locally (Parsing layout, OCR, and tables)...")
    result = converter.convert(source)
    
    print("✨ Conversion finished! Compiling Markdown structure...")
    markdown_output = result.document.export_to_markdown()
    
    # Save output cleanly to project directory
    output_filename = "parsed_paper.md"
    with open(output_filename, "w", encoding="utf-8") as f:
        f.write(markdown_output)
        
    elapsed_time = time.time() - start_time
    print(f"✅ Success! Processing complete in {elapsed_time:.2f} seconds.")
    print(f"📁 Structured output saved cleanly to: {os.path.abspath(output_filename)}")
    
    print("\n--- Preview of First 300 Characters: ---")
    print(markdown_output[:300])

if __name__ == "__main__":
    main()