-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
102 lines (81 loc) · 3.13 KB
/
main.py
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
from fastapi import FastAPI, UploadFile, File, HTTPException
from pdf2image import convert_from_bytes
from PIL import Image
import io
import os
import base64
from app.baml_client import b, reset_baml_env_vars
from baml_py import Image as BamlImage
from typing import List
import logging
from dotenv import load_dotenv
load_dotenv()
os.environ["BAML_LOG"] = "WARN"
reset_baml_env_vars(dict(os.environ))
app = FastAPI()
def convert_pdf_to_images(pdf_bytes: bytes) -> List[Image.Image]:
try:
return convert_from_bytes(pdf_bytes, dpi=300, fmt="png")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error converting PDF: {str(e)}")
def convert_to_images(content_type: str, file_bytes: bytes) -> List[Image.Image]:
if content_type == "application/pdf":
return convert_pdf_to_images(file_bytes)
elif content_type.startswith("image/"):
img = Image.open(io.BytesIO(file_bytes))
return [img]
else:
raise HTTPException(
status_code=415, detail=f"Unsupported file type: {content_type}"
)
def img_to_baml_image(img: Image.Image) -> BamlImage:
buffer = io.BytesIO()
img.save(buffer, format=img.format or "PNG")
base64_str = base64.b64encode(buffer.getvalue()).decode()
return BamlImage.from_base64(
f"image/{img.format.lower() if img.format else 'png'}", base64_str
)
@app.get("/")
async def root():
return {
"name": "PDF and Image Extraction API",
"description": "This API converts PDFs and images to a format suitable for data extraction using BAML.",
"endpoints": {
"/": "This information",
"/extract": "Upload a PDF or image file to extract data using BAML"
}
}
@app.post("/extract")
async def extract(file: UploadFile = File(...)):
try:
# Read file content
content = await file.read()
content_type = file.content_type or "application/pdf"
# Convert to PIL Images
images = convert_to_images(content_type, content)
if not images:
raise HTTPException(status_code=400, detail="No images could be extracted")
# Process each image and extract data
extraction_results = []
image_data = []
for i, img in enumerate(images):
# Convert image to BAML Image for extraction
baml_image = img_to_baml_image(img)
# Call BAML function for this image
result = b.ExtractFromImage(baml_image)
extraction_results.append({
"page": i+1,
"data": result
})
# Convert image to base64 for response
buffer = io.BytesIO()
img.save(buffer, format="PNG")
base64_str = base64.b64encode(buffer.getvalue()).decode()
image_data.append({
"page": i+1,
"image": base64_str
})
return {"results": extraction_results, "images": image_data}
except Exception as e:
logging.error(f"Extraction error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))