MJobe commited on
Commit
f660b8b
1 Parent(s): 0f0bcd2

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -31
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 string to store the text content of the PDF
77
- all_text = ""
78
 
79
- # Use PyMuPDF to process the PDF and extract text
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"Processing page {page_num + 1}...")
86
- text = page.get_text()
87
- all_text += text + '\n'
88
-
89
- # Print or do something with the collected text
90
- print(all_text)
91
-
92
- # List of questions
93
- question_list = questions.split(',')
94
-
95
- # Initialize an empty dictionary to store questions and answers
96
- qa_dict = {}
97
-
98
- # Get answers for each question with the same context
99
- for question in question_list:
100
- result = nlp_qa({
101
- 'question': question,
102
- 'context': all_text
103
- })
104
-
105
- # Access the 'answer' key from the result
106
- answer = result['answer']
107
-
108
- # Store the question and answer in the dictionary
109
- qa_dict[question] = answer
110
 
111
- return qa_dict
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(