vkumartr commited on
Commit
87dfad3
·
verified ·
1 Parent(s): e0f7bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -2
app.py CHANGED
@@ -101,6 +101,7 @@ def extract_invoice_data(file_data, content_type, json_schema):
101
  """
102
  system_prompt = "You are an expert in document data extraction."
103
  base64_images = []
 
104
 
105
  if content_type == "application/pdf":
106
  try:
@@ -115,7 +116,9 @@ def extract_invoice_data(file_data, content_type, json_schema):
115
  img.save(img_byte_arr, format="PNG", dpi=(300, 300))
116
  base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
117
  base64_images.append(f"data:image/png;base64,{base64_encoded}")
118
- base64DataResp = f"data:image/png;base64,{base64_encoded}"
 
 
119
 
120
  except Exception as e:
121
  logger.error(f"Error converting PDF to image: {e}")
@@ -125,7 +128,7 @@ def extract_invoice_data(file_data, content_type, json_schema):
125
  # Handle direct image files
126
  base64_encoded = base64.b64encode(file_data).decode('utf-8')
127
  base64_images.append(f"data:{content_type};base64,{base64_encoded}")
128
- base64DataResp = f"data:image/png;base64,{base64_encoded}"
129
 
130
  # Prepare OpenAI request
131
  openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]
 
101
  """
102
  system_prompt = "You are an expert in document data extraction."
103
  base64_images = []
104
+ base64DataResp = ""
105
 
106
  if content_type == "application/pdf":
107
  try:
 
116
  img.save(img_byte_arr, format="PNG", dpi=(300, 300))
117
  base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
118
  base64_images.append(f"data:image/png;base64,{base64_encoded}")
119
+
120
+ # Store all images as a single JSON object
121
+ base64DataResp = json.dumps(base64_images)
122
 
123
  except Exception as e:
124
  logger.error(f"Error converting PDF to image: {e}")
 
128
  # Handle direct image files
129
  base64_encoded = base64.b64encode(file_data).decode('utf-8')
130
  base64_images.append(f"data:{content_type};base64,{base64_encoded}")
131
+ base64DataResp = json.dumps(base64_images) # Store as a JSON object
132
 
133
  # Prepare OpenAI request
134
  openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]