vkumartr commited on
Commit
a767e64
·
verified ·
1 Parent(s): 3cacceb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -107,13 +107,13 @@ def extract_invoice_data(file_data, content_type, json_schema):
107
  images = convert_from_bytes(file_data) # Convert PDF to images
108
 
109
  if len(images) > 2:
110
- raise ValueError("PDF contains more than 2 pages. Only PDFs with 1 or 2 pages are supported.")
111
 
112
  for img in images[:2]:
113
  img_byte_arr = io.BytesIO()
114
  img.save(img_byte_arr, format="PNG", dpi=(300, 300))
115
  base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
116
- base64_images.append(f"data:{content_type};base64,{base64_encoded}")
117
 
118
  except Exception as e:
119
  logger.error(f"Error converting PDF to image: {e}")
@@ -122,7 +122,8 @@ def extract_invoice_data(file_data, content_type, json_schema):
122
  else:
123
  # Handle direct image files
124
  base64_encoded = base64.b64encode(file_data).decode('utf-8')
125
- base64_images.append(f"data:{content_type};base64,{base64_encoded}")
 
126
 
127
  # Prepare OpenAI request
128
  openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]
@@ -140,11 +141,11 @@ def extract_invoice_data(file_data, content_type, json_schema):
140
  )
141
 
142
  parsed_content = json.loads(response.choices[0].message.content.strip())
143
- return parsed_content, base64_images
144
 
145
  except Exception as e:
146
  logger.error(f"Error in OpenAI processing: {e}")
147
- return {"error": str(e)}, base64_images
148
 
149
  def get_content_type_from_s3(file_key):
150
  """Fetch the content type (MIME type) of a file stored in S3."""
@@ -214,7 +215,7 @@ def extract_text_from_file(
214
  "message": "Document successfully stored in MongoDB",
215
  "document_id": document_id,
216
  "entityrefkey": entity_ref_key,
217
- "base64DataResp": base64_images,
218
  "extracted_data": extracted_data
219
  }
220
 
 
107
  images = convert_from_bytes(file_data) # Convert PDF to images
108
 
109
  if len(images) > 2:
110
+ raise ValueError("PDF contains more than 2 pages.")
111
 
112
  for img in images[:2]:
113
  img_byte_arr = io.BytesIO()
114
  img.save(img_byte_arr, format="PNG", dpi=(300, 300))
115
  base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
116
+ base64_images.append(f"data:{content_type.lower()};base64,{base64_encoded}")
117
 
118
  except Exception as e:
119
  logger.error(f"Error converting PDF to image: {e}")
 
122
  else:
123
  # Handle direct image files
124
  base64_encoded = base64.b64encode(file_data).decode('utf-8')
125
+ base64_images.append(f"data:{content_type.lower()};base64,{base64_encoded}")
126
+ base64dataresp = f"data:{content_type.lower()};base64,{base64_encoded}
127
 
128
  # Prepare OpenAI request
129
  openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]
 
141
  )
142
 
143
  parsed_content = json.loads(response.choices[0].message.content.strip())
144
+ return parsed_content, base64dataresp
145
 
146
  except Exception as e:
147
  logger.error(f"Error in OpenAI processing: {e}")
148
+ return {"error": str(e)}, base64dataresp
149
 
150
  def get_content_type_from_s3(file_key):
151
  """Fetch the content type (MIME type) of a file stored in S3."""
 
215
  "message": "Document successfully stored in MongoDB",
216
  "document_id": document_id,
217
  "entityrefkey": entity_ref_key,
218
+ "base64DataResp": base64dataresp,
219
  "extracted_data": extracted_data
220
  }
221