MJobe commited on
Commit
e36dd54
1 Parent(s): 3302f65

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +4 -19
main.py CHANGED
@@ -6,6 +6,7 @@ from PIL import Image
6
  from io import BytesIO
7
  from starlette.middleware import Middleware
8
  from starlette.middleware.cors import CORSMiddleware
 
9
 
10
  app = FastAPI()
11
 
@@ -73,28 +74,12 @@ async def pdf_question_answering(
73
  # Read the uploaded file as bytes
74
  contents = await file.read()
75
 
76
- # Initialize an empty list to store image bytes
77
- images = []
78
-
79
- # Use PyMuPDF to process the PDF and convert each page to an image
80
- pdf_document = fitz.open_memo(contents, "pdf")
81
-
82
- for page_num in range(pdf_document.page_count):
83
- page = pdf_document.load_page(page_num)
84
- print(f"Converting page {page_num + 1} to image...")
85
-
86
- # Convert the page to an image
87
- image = Image.frombytes("RGB", page.get_size(), page.get_pixmap().samples)
88
-
89
- # Convert the image to bytes
90
- img_byte_array = BytesIO()
91
- image.save(img_byte_array, format='PNG')
92
- images.append(img_byte_array.getvalue())
93
 
94
  # Perform document question answering for each image
95
  answers_dict = {}
96
- for idx, image_bytes in enumerate(images):
97
- image = Image.open(BytesIO(image_bytes))
98
  for question in questions.split(','):
99
  result = nlp_qa(
100
  image,
 
6
  from io import BytesIO
7
  from starlette.middleware import Middleware
8
  from starlette.middleware.cors import CORSMiddleware
9
+ from pdf2image import convert_from_bytes
10
 
11
  app = FastAPI()
12
 
 
74
  # Read the uploaded file as bytes
75
  contents = await file.read()
76
 
77
+ # Convert PDF to images
78
+ images = convert_from_bytes(contents)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Perform document question answering for each image
81
  answers_dict = {}
82
+ for idx, image in enumerate(images):
 
83
  for question in questions.split(','):
84
  result = nlp_qa(
85
  image,