74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Parse PDF document using unstructured-api on 192.168.1.103.
|
|
Uses maximum quality settings (hi_res + GPU).
|
|
Saves parsed JSON to /tmp/pol177_parsed.json.
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
import sys
|
|
|
|
PDF_PATH = "/opt/documents_xk/АОХКСибцем/07 Служба Вице-президента по экономике и финансам/ДИТ/02 Регламенты деятельности/10 Положение Хранение электронных документов от 03.04.2024 № ПОЛ-177.pdf"
|
|
API_URL = "http://192.168.1.103:8005/general/v0/general"
|
|
|
|
def parse_pdf():
|
|
print("Parsing PDF with maximum quality (hi_res + GPU)...")
|
|
|
|
# Maximum quality parameters
|
|
form_data = {
|
|
'strategy': 'hi_res',
|
|
'hi_res_model_name': 'chipper',
|
|
'pdf_infer_table_structure': 'true',
|
|
'extract_images': 'true',
|
|
'extract_image_block_types': '["image", "table"]',
|
|
}
|
|
|
|
with open(PDF_PATH, 'rb') as f:
|
|
# Note: 'files' (plural) - API expects list of UploadFile
|
|
files = {'files': f}
|
|
response = requests.post(API_URL, files=files, data=form_data)
|
|
|
|
if response.status_code != 200:
|
|
print(f"ERROR: API returned {response.status_code}: {response.text[:500]}")
|
|
sys.exit(1)
|
|
|
|
result = response.json()
|
|
|
|
# API returns array directly, not {"elements": [...]}
|
|
if isinstance(result, list):
|
|
elements = result
|
|
else:
|
|
elements = result.get('elements', [])
|
|
|
|
print(f"Extracted {len(elements)} elements")
|
|
|
|
# Add page numbers
|
|
for i, el in enumerate(elements):
|
|
if 'page_number' in el.get('metadata', {}):
|
|
el['page_number'] = el['metadata']['page_number']
|
|
else:
|
|
el['page_number'] = i + 1
|
|
|
|
# Save to JSON
|
|
output = {
|
|
'filename': '10 Положение Хранение электронных документов от 03.04.2024 № ПОЛ-177.pdf',
|
|
'total_elements': len(elements),
|
|
'elements': elements
|
|
}
|
|
|
|
with open('/tmp/pol177_parsed.json', 'w', encoding='utf-8') as f:
|
|
json.dump(output, f, ensure_ascii=False, indent=2)
|
|
|
|
print(f"Saved {len(elements)} elements to /tmp/pol177_parsed.json")
|
|
|
|
# Stats
|
|
types = {}
|
|
for el in elements:
|
|
t = el.get('type', 'Unknown')
|
|
types[t] = types.get(t, 0) + 1
|
|
print(f"Element types: {types}")
|
|
|
|
if __name__ == '__main__':
|
|
parse_pdf()
|