Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -74,12 +74,33 @@ async def pdf_question_answering(
|
|
74 |
# Read the uploaded file as bytes
|
75 |
contents = await file.read()
|
76 |
|
77 |
-
#
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# Perform document question answering for each image
|
81 |
answers_dict = {}
|
82 |
-
for idx,
|
|
|
83 |
for question in questions.split(','):
|
84 |
result = nlp_qa(
|
85 |
image,
|
@@ -89,6 +110,10 @@ async def pdf_question_answering(
|
|
89 |
formatted_question = f"{question.strip('[]')} (Page {idx + 1})"
|
90 |
answers_dict[formatted_question] = answer
|
91 |
|
|
|
|
|
|
|
|
|
92 |
return answers_dict
|
93 |
|
94 |
except Exception as e:
|
|
|
74 |
# Read the uploaded file as bytes
|
75 |
contents = await file.read()
|
76 |
|
77 |
+
# Save the PDF bytes to a temporary file
|
78 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
79 |
+
temp_pdf.write(contents)
|
80 |
+
temp_pdf_path = temp_pdf.name
|
81 |
+
|
82 |
+
# Initialize an empty list to store image bytes
|
83 |
+
images = []
|
84 |
+
|
85 |
+
# Use PyMuPDF to process the PDF and convert each page to an image
|
86 |
+
pdf_document = fitz.open(temp_pdf_path)
|
87 |
+
|
88 |
+
for page_num in range(pdf_document.page_count):
|
89 |
+
page = pdf_document.load_page(page_num)
|
90 |
+
print(f"Converting page {page_num + 1} to image...")
|
91 |
+
|
92 |
+
# Convert the page to an image
|
93 |
+
image = Image.frombytes("RGB", page.get_size(), page.get_pixmap().samples)
|
94 |
+
|
95 |
+
# Convert the image to bytes
|
96 |
+
img_byte_array = BytesIO()
|
97 |
+
image.save(img_byte_array, format='PNG')
|
98 |
+
images.append(img_byte_array.getvalue())
|
99 |
|
100 |
# Perform document question answering for each image
|
101 |
answers_dict = {}
|
102 |
+
for idx, image_bytes in enumerate(images):
|
103 |
+
image = Image.open(BytesIO(image_bytes))
|
104 |
for question in questions.split(','):
|
105 |
result = nlp_qa(
|
106 |
image,
|
|
|
110 |
formatted_question = f"{question.strip('[]')} (Page {idx + 1})"
|
111 |
answers_dict[formatted_question] = answer
|
112 |
|
113 |
+
# Delete the temporary PDF file
|
114 |
+
temp_pdf.close()
|
115 |
+
os.remove(temp_pdf_path)
|
116 |
+
|
117 |
return answers_dict
|
118 |
|
119 |
except Exception as e:
|