Namra-Satva commited on
Commit
254ea42
·
verified ·
1 Parent(s): a652976

Update model_utils.py

Browse files
Files changed (1) hide show
  1. model_utils.py +19 -5
model_utils.py CHANGED
@@ -33,13 +33,27 @@ def parse_products(raw_text):
33
  "unit_price": unit_price,
34
  "amount": amount
35
  })
36
- elif line.strip():
 
 
 
37
  structured.append({
38
- "qty": 0,
39
- "description": line.strip(),
40
- "unit_price": 0,
41
- "amount": 0
 
 
 
 
 
 
 
 
 
 
42
  })
 
43
  return structured
44
 
45
  def extract_invoice_data_from_image(image_path: str):
 
33
  "unit_price": unit_price,
34
  "amount": amount
35
  })
36
+ continue
37
+ match_qty_first = re.match(r"^\s*(\d+)\s+(.+?)(?=\s*\d)\s*([\d,.]+(?:\.\d{0,2})?)\s*([\d,.]+(?:\.\d{0,2})?)\s*$",line)
38
+ if match_qty_first:
39
+ qty, desc, unit_price, amount = match_qty_first.groups()
40
  structured.append({
41
+ "qty": int(qty) if qty and qty.strip() else 0,
42
+ "description": desc.strip(),
43
+ "unit_price": float(unit_price.replace(",", "").replace(" ", "")) if unit_price else 0.0,
44
+ "amount": float(amount.replace(",", "").replace(" ", "")) if amount else 0.0
45
+ })
46
+ continue
47
+ match_desc_first = re.match(r"^\s*(.+?)(?=\s*\d)\s*(\d+)\s*([\d,.]+(?:\.\d{0,2})?)\s*([\d,.]+(?:\.\d{0,2})?)\s*$",line)
48
+ if match_desc_first:
49
+ desc, qty, unit_price, amount = match_desc_first.groups()
50
+ structured.append({
51
+ "qty": int(qty) if qty and qty.strip() else 0,
52
+ "description": desc.strip(),
53
+ "unit_price": float(unit_price.replace(",", "").replace(" ", "")) if unit_price else 0.0,
54
+ "amount": float(amount.replace(",", "").replace(" ", "")) if amount else 0.0
55
  })
56
+ continue
57
  return structured
58
 
59
  def extract_invoice_data_from_image(image_path: str):