Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -73,46 +73,42 @@ async def pdf_question_answering(
|
|
73 |
# Read the uploaded file as bytes
|
74 |
contents = await file.read()
|
75 |
|
76 |
-
# Initialize an empty
|
77 |
-
|
78 |
|
79 |
-
# Use PyMuPDF to process the PDF and
|
80 |
pdf_document = fitz.open_from_bytes(contents)
|
81 |
|
82 |
-
# Loop through each page and perform OCR
|
83 |
for page_num in range(pdf_document.page_count):
|
84 |
page = pdf_document.load_page(page_num)
|
85 |
-
print(f"
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
#
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
# Store the question and answer in the dictionary
|
109 |
-
qa_dict[question] = answer
|
110 |
|
111 |
-
return
|
112 |
|
113 |
except Exception as e:
|
114 |
return JSONResponse(content=f"Error processing PDF file: {str(e)}", status_code=500)
|
115 |
-
|
116 |
# Set up CORS middleware
|
117 |
origins = ["*"] # or specify your list of allowed origins
|
118 |
app.add_middleware(
|
|
|
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_from_bytes(contents)
|
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,
|
101 |
+
question.strip()
|
102 |
+
)
|
103 |
+
answer = result[0]['answer']
|
104 |
+
formatted_question = f"{question.strip('[]')} (Page {idx + 1})"
|
105 |
+
answers_dict[formatted_question] = answer
|
|
|
|
|
|
|
106 |
|
107 |
+
return answers_dict
|
108 |
|
109 |
except Exception as e:
|
110 |
return JSONResponse(content=f"Error processing PDF file: {str(e)}", status_code=500)
|
111 |
+
|
112 |
# Set up CORS middleware
|
113 |
origins = ["*"] # or specify your list of allowed origins
|
114 |
app.add_middleware(
|