vkumartr commited on
Commit
cf26152
·
verified ·
1 Parent(s): 9910dd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -3
app.py CHANGED
@@ -114,6 +114,7 @@ def extract_invoice_data(file_data, content_type, json_schema):
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}")
@@ -123,6 +124,7 @@ def extract_invoice_data(file_data, content_type, json_schema):
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
 
127
  # Prepare OpenAI request
128
  openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]
@@ -140,11 +142,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 +216,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
 
 
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
+ base64dataresp = f"data:{content_type};base64,{base64_encoded}"
118
 
119
  except Exception as e:
120
  logger.error(f"Error converting PDF to image: {e}")
 
124
  # Handle direct image files
125
  base64_encoded = base64.b64encode(file_data).decode('utf-8')
126
  base64_images.append(f"data:{content_type.lower()};base64,{base64_encoded}")
127
+ base64dataresp = f"data:{content_type.lower()};base64,{base64_encoded}"
128
 
129
  # Prepare OpenAI request
130
  openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]
 
142
  )
143
 
144
  parsed_content = json.loads(response.choices[0].message.content.strip())
145
+ return parsed_content, base64dataresp
146
 
147
  except Exception as e:
148
  logger.error(f"Error in OpenAI processing: {e}")
149
+ return {"error": str(e)}, base64dataresp
150
 
151
  def get_content_type_from_s3(file_key):
152
  """Fetch the content type (MIME type) of a file stored in S3."""
 
216
  "message": "Document successfully stored in MongoDB",
217
  "document_id": document_id,
218
  "entityrefkey": entity_ref_key,
219
+ "base64DataResp": base64dataresp,
220
  "extracted_data": extracted_data
221
  }
222