pdf_processor.py aktualisiert

Including Antrhopic API
This commit is contained in:
Sebastian Mondial 2025-02-19 21:42:34 +00:00
parent 2a3e445c23
commit d611992eed

View file

@ -4,36 +4,90 @@ import json
from pathlib import Path from pathlib import Path
import tempfile import tempfile
import base64 import base64
import anthropic
from typing import List, Dict
import time
class PDFProcessor: class PDFProcessor:
def __init__(self, input_dir, output_dir): def __init__(self, input_dir: str, output_dir: str, api_key: str):
self.input_dir = Path(input_dir) self.input_dir = Path(input_dir)
self.output_dir = Path(output_dir) self.output_dir = Path(output_dir)
self.output_dir.mkdir(parents=True, exist_ok=True) self.output_dir.mkdir(parents=True, exist_ok=True)
self.temp_dir = Path(tempfile.mkdtemp()) self.temp_dir = Path(tempfile.mkdtemp())
self.batch_size = 5 # Number of images to process at once self.client = anthropic.Client(api_key=api_key)
def encode_image(self, image_path): def encode_image(self, image_path: str) -> str:
"""Convert image to base64 for analysis""" """Convert image to base64 for API"""
with open(image_path, 'rb') as image_file: with open(image_path, 'rb') as image_file:
return base64.b64encode(image_file.read()).decode('utf-8') return base64.b64encode(image_file.read()).decode('utf-8')
def process_pdfs(self): def analyze_image(self, image_path: str) -> Dict:
"""Analyze a single image using Claude Vision API"""
try:
with open(image_path, 'rb') as img:
message = self.client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1000,
messages=[{
"role": "user",
"content": [
{
"type": "text",
"text": """Analyze this magazine cover and extract the following metadata:
1. Magazine Title
2. Issue Date/Publication Date
3. Publisher
4. Issue Number
Format your response as JSON with these exact keys:
{
"title": string,
"date": string,
"publisher": string,
"issue_number": string,
"confidence": "high|medium|low"
}
If any field cannot be determined, use null. Set confidence based on how clear the information is."""
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": self.encode_image(image_path)
}
}
]
}]
)
# Parse the JSON response
response_text = message.content[0].text
metadata = json.loads(response_text)
return metadata
except Exception as e:
print(f"Error analyzing image {image_path}: {str(e)}")
return {
"title": None,
"date": None,
"publisher": None,
"issue_number": None,
"confidence": "error"
}
def process_pdfs(self) -> List[Dict]:
"""Process all PDFs in the input directory""" """Process all PDFs in the input directory"""
pdf_files = list(self.input_dir.glob('*.pdf')) pdf_files = list(self.input_dir.glob('*.pdf'))
results = [] results = []
current_batch = []
for pdf_path in pdf_files: for pdf_path in pdf_files:
try: try:
batch_item = self.prepare_single_pdf(pdf_path) result = self.process_single_pdf(pdf_path)
current_batch.append(batch_item) results.append(result)
# Small delay to respect API rate limits
# Process batch when it reaches batch_size time.sleep(1)
if len(current_batch) >= self.batch_size:
self.process_batch(current_batch, results)
current_batch = []
except Exception as e: except Exception as e:
print(f"Error processing {pdf_path}: {str(e)}") print(f"Error processing {pdf_path}: {str(e)}")
results.append({ results.append({
@ -42,19 +96,15 @@ class PDFProcessor:
'error': str(e) 'error': str(e)
}) })
# Process remaining files in the last batch # Save results to JSON
if current_batch:
self.process_batch(current_batch, results)
# Save final results to JSON
with open(self.output_dir / 'processing_results.json', 'w', encoding='utf-8') as f: with open(self.output_dir / 'processing_results.json', 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4, ensure_ascii=False) json.dump(results, f, indent=4, ensure_ascii=False)
return results return results
def prepare_single_pdf(self, pdf_path): def process_single_pdf(self, pdf_path: Path) -> Dict:
"""Prepare a single PDF file for analysis""" """Process a single PDF file"""
print(f"Preparing: {pdf_path}") print(f"Processing: {pdf_path}")
# Convert first page to image # Convert first page to image
images = convert_from_path(pdf_path, first_page=1, last_page=1) images = convert_from_path(pdf_path, first_page=1, last_page=1)
@ -66,52 +116,26 @@ class PDFProcessor:
image_path = self.temp_dir / f"{pdf_path.stem}_page1.jpg" image_path = self.temp_dir / f"{pdf_path.stem}_page1.jpg"
first_page.save(str(image_path), 'JPEG') first_page.save(str(image_path), 'JPEG')
# Analyze the image
metadata = self.analyze_image(str(image_path))
return { return {
'pdf_path': str(pdf_path), 'pdf_path': str(pdf_path),
'image_path': str(image_path) 'image_path': str(image_path),
'metadata': metadata,
'status': 'completed'
} }
def process_batch(self, batch_items, results):
"""Process a batch of prepared PDFs"""
print(f"\nProcessing batch of {len(batch_items)} files...")
# Here you would interact with me (Claude) to analyze the images
# For each image in the batch:
for item in batch_items:
image_path = item['image_path']
pdf_path = item['pdf_path']
# Convert image to base64
image_data = self.encode_image(image_path)
# You would need to ask me to analyze this image
# For now, we'll save placeholder metadata
metadata = {
'title': None,
'date': None,
'publisher': None,
'issue_number': None,
'confidence': 'pending_analysis'
}
results.append({
'pdf_path': pdf_path,
'image_path': str(image_path),
'metadata': metadata,
'status': 'pending_analysis'
})
def save_metadata(self, results):
"""Save the extracted metadata back to PDFs or to a database"""
# TODO: Implement metadata saving functionality
pass
def main(): def main():
# Example usage # Get API key from environment variable
api_key = os.getenv('ANTHROPIC_API_KEY')
if not api_key:
raise ValueError("ANTHROPIC_API_KEY environment variable not set")
input_dir = "path/to/pdfs" input_dir = "path/to/pdfs"
output_dir = "path/to/output" output_dir = "path/to/output"
processor = PDFProcessor(input_dir, output_dir) processor = PDFProcessor(input_dir, output_dir, api_key)
results = processor.process_pdfs() results = processor.process_pdfs()
print(f"\nProcessed {len(results)} PDF files") print(f"\nProcessed {len(results)} PDF files")