Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,73 @@ import pandas as pd
|
|
| 4 |
import re
|
| 5 |
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def extract_invoices(file):
|
| 8 |
|
| 9 |
text = ""
|
|
@@ -14,24 +81,20 @@ def extract_invoices(file):
|
|
| 14 |
if t:
|
| 15 |
text += t + "\n"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
invoice_pattern = r'(?:Invoice\s*No|Invoice\s*Number|Inv\.?\s*No|Invoice\s*#|Bill\s*No|Bill\s*Number)\s*[:#\-]?\s*([A-Za-z0-9\-\/]+)'
|
| 19 |
invoice_matches = list(re.finditer(invoice_pattern, text, re.IGNORECASE))
|
| 20 |
|
| 21 |
rows = []
|
| 22 |
-
|
| 23 |
|
| 24 |
for i, match in enumerate(invoice_matches):
|
| 25 |
|
| 26 |
invoice_number = match.group(1)
|
| 27 |
|
| 28 |
-
if
|
| 29 |
continue
|
| 30 |
|
| 31 |
-
|
| 32 |
-
continue
|
| 33 |
-
|
| 34 |
-
processed_invoices.add(invoice_number)
|
| 35 |
|
| 36 |
start = match.start()
|
| 37 |
end = invoice_matches[i + 1].start() if i + 1 < len(invoice_matches) else len(text)
|
|
@@ -39,72 +102,77 @@ def extract_invoices(file):
|
|
| 39 |
block = text[start:end]
|
| 40 |
|
| 41 |
# -------- DATE -------- #
|
| 42 |
-
date_pattern = r'(\d{1,2}[\/\-.]\d{1,2}[\/\-.]\d{2,4}|
|
| 43 |
|
| 44 |
date_match = re.search(
|
| 45 |
-
r'(Invoice\s*Date|Bill\s*Date|Issue\s*Date)
|
| 46 |
block,
|
| 47 |
re.IGNORECASE
|
| 48 |
)
|
| 49 |
|
| 50 |
-
if date_match
|
| 51 |
-
invoice_date = date_match.group(2)
|
| 52 |
-
else:
|
| 53 |
-
fallback = re.search(date_pattern, block, re.IGNORECASE)
|
| 54 |
-
invoice_date = fallback.group(0) if fallback else ""
|
| 55 |
|
| 56 |
# -------- VENDOR -------- #
|
| 57 |
-
vendor_match = re.search(
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
match = re.search(label + r'.*?([\d,]+\.\d{2})', block, re.IGNORECASE)
|
| 67 |
-
return float(match.group(1).replace(",", "")) if match else 0.0
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# -------- TOTAL -------- #
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
floats = [float(x.replace(",", "")) for x in re.findall(float_pattern, block)]
|
| 79 |
|
| 80 |
-
if total_match
|
| 81 |
-
total_amount = float(total_match.group(2).replace(",", ""))
|
| 82 |
-
else:
|
| 83 |
-
total_amount = max(floats) if floats else 0
|
| 84 |
|
| 85 |
# -------- ROW -------- #
|
| 86 |
-
|
| 87 |
"Invoice Number": invoice_number,
|
| 88 |
"Invoice Date": invoice_date,
|
| 89 |
"Vendor": vendor,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
"CGST": cgst,
|
| 91 |
"SGST": sgst,
|
| 92 |
"IGST": igst,
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
"Narration": f"Invoice from {vendor}",
|
| 95 |
"Debit Account": "Expense",
|
| 96 |
"Credit Account": vendor
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
rows.append(row)
|
| 100 |
|
| 101 |
df = pd.DataFrame(rows)
|
| 102 |
|
| 103 |
-
# -------- SAVE EXCEL -------- #
|
| 104 |
excel_file = "invoice_output.xlsx"
|
| 105 |
-
|
| 106 |
-
with pd.ExcelWriter(excel_file, engine='openpyxl') as writer:
|
| 107 |
-
df.to_excel(writer, index=False, sheet_name='Invoices')
|
| 108 |
|
| 109 |
return df, excel_file
|
| 110 |
|
|
@@ -112,20 +180,16 @@ def extract_invoices(file):
|
|
| 112 |
# -------- UI -------- #
|
| 113 |
with gr.Blocks() as demo:
|
| 114 |
|
| 115 |
-
gr.Markdown("##
|
| 116 |
|
| 117 |
file_input = gr.File(label="Upload Invoice PDF")
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
-
extract_invoices,
|
| 127 |
-
inputs=file_input,
|
| 128 |
-
outputs=[output_table, download_file]
|
| 129 |
-
)
|
| 130 |
|
| 131 |
demo.launch()
|
|
|
|
| 4 |
import re
|
| 5 |
|
| 6 |
|
| 7 |
+
# -------- GST HELPERS -------- #
|
| 8 |
+
|
| 9 |
+
def extract_all_gstins(text):
|
| 10 |
+
gstin_pattern = r'\b\d{2}[A-Z]{5}\d{4}[A-Z][A-Z\d]Z[A-Z\d]\b'
|
| 11 |
+
return list(set(re.findall(gstin_pattern, text)))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def get_state_from_gstin(gstin):
|
| 15 |
+
state_codes = {
|
| 16 |
+
"27": "Maharashtra", "29": "Karnataka", "33": "Tamil Nadu",
|
| 17 |
+
"36": "Telangana", "37": "Andhra Pradesh", "32": "Kerala",
|
| 18 |
+
"07": "Delhi", "24": "Gujarat", "19": "West Bengal"
|
| 19 |
+
}
|
| 20 |
+
return state_codes.get(gstin[:2], "") if gstin else ""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def classify_gstins(block, gstins):
|
| 24 |
+
seller = ""
|
| 25 |
+
buyer = ""
|
| 26 |
+
ship = ""
|
| 27 |
+
|
| 28 |
+
# -------- SELLER -------- #
|
| 29 |
+
seller_match = re.search(
|
| 30 |
+
r'(GSTIN|GST No|Supplier GSTIN|Vendor GSTIN)[^\n]*(' + r'\b\d{2}[A-Z]{5}\d{4}[A-Z][A-Z\d]Z[A-Z\d]\b' + ')',
|
| 31 |
+
block,
|
| 32 |
+
re.IGNORECASE
|
| 33 |
+
)
|
| 34 |
+
if seller_match:
|
| 35 |
+
seller = seller_match.group(2)
|
| 36 |
+
|
| 37 |
+
# -------- BUYER -------- #
|
| 38 |
+
buyer_match = re.search(
|
| 39 |
+
r'(Bill To|Buyer|Billed To)[\s\S]{0,100}?(' + r'\b\d{2}[A-Z]{5}\d{4}[A-Z][A-Z\d]Z[A-Z\d]\b' + ')',
|
| 40 |
+
block,
|
| 41 |
+
re.IGNORECASE
|
| 42 |
+
)
|
| 43 |
+
if buyer_match:
|
| 44 |
+
buyer = buyer_match.group(2)
|
| 45 |
+
|
| 46 |
+
# -------- SHIP TO -------- #
|
| 47 |
+
ship_match = re.search(
|
| 48 |
+
r'(Ship To|Delivery Address)[\s\S]{0,100}?(' + r'\b\d{2}[A-Z]{5}\d{4}[A-Z][A-Z\d]Z[A-Z\d]\b' + ')',
|
| 49 |
+
block,
|
| 50 |
+
re.IGNORECASE
|
| 51 |
+
)
|
| 52 |
+
if ship_match:
|
| 53 |
+
ship = ship_match.group(2)
|
| 54 |
+
|
| 55 |
+
# -------- FALLBACK -------- #
|
| 56 |
+
remaining = [g for g in gstins if g not in [seller, buyer, ship]]
|
| 57 |
+
|
| 58 |
+
if not seller and gstins:
|
| 59 |
+
seller = gstins[0]
|
| 60 |
+
|
| 61 |
+
if not buyer and len(gstins) > 1:
|
| 62 |
+
buyer = gstins[1]
|
| 63 |
+
|
| 64 |
+
if not ship and len(gstins) > 2:
|
| 65 |
+
ship = gstins[2]
|
| 66 |
+
|
| 67 |
+
others = [g for g in gstins if g not in [seller, buyer, ship]]
|
| 68 |
+
|
| 69 |
+
return seller, buyer, ship, ", ".join(others)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# -------- MAIN FUNCTION -------- #
|
| 73 |
+
|
| 74 |
def extract_invoices(file):
|
| 75 |
|
| 76 |
text = ""
|
|
|
|
| 81 |
if t:
|
| 82 |
text += t + "\n"
|
| 83 |
|
| 84 |
+
invoice_pattern = r'(?:Invoice\s*No|Invoice\s*Number|Inv\.?\s*No|Invoice\s*#|Bill\s*No)\s*[:#\-]?\s*([A-Za-z0-9\-\/]+)'
|
|
|
|
| 85 |
invoice_matches = list(re.finditer(invoice_pattern, text, re.IGNORECASE))
|
| 86 |
|
| 87 |
rows = []
|
| 88 |
+
processed = set()
|
| 89 |
|
| 90 |
for i, match in enumerate(invoice_matches):
|
| 91 |
|
| 92 |
invoice_number = match.group(1)
|
| 93 |
|
| 94 |
+
if invoice_number in processed or len(invoice_number) < 5:
|
| 95 |
continue
|
| 96 |
|
| 97 |
+
processed.add(invoice_number)
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
start = match.start()
|
| 100 |
end = invoice_matches[i + 1].start() if i + 1 < len(invoice_matches) else len(text)
|
|
|
|
| 102 |
block = text[start:end]
|
| 103 |
|
| 104 |
# -------- DATE -------- #
|
| 105 |
+
date_pattern = r'(\d{1,2}[\/\-.]\d{1,2}[\/\-.]\d{2,4}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\s+\d{1,2},?\s*\d{2,4})'
|
| 106 |
|
| 107 |
date_match = re.search(
|
| 108 |
+
r'(Invoice\s*Date|Bill\s*Date|Issue\s*Date)[^\n]*(' + date_pattern + ')',
|
| 109 |
block,
|
| 110 |
re.IGNORECASE
|
| 111 |
)
|
| 112 |
|
| 113 |
+
invoice_date = date_match.group(2) if date_match else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# -------- VENDOR -------- #
|
| 116 |
+
vendor_match = re.search(r'(M/s\.?|Supplier|Vendor)[^\n]*', block, re.IGNORECASE)
|
| 117 |
+
vendor = vendor_match.group(0).strip() if vendor_match else "Unknown"
|
| 118 |
+
|
| 119 |
+
# -------- GSTIN -------- #
|
| 120 |
+
gstins = extract_all_gstins(block)
|
| 121 |
+
seller_gstin, buyer_gstin, ship_gstin, other_gstins = classify_gstins(block, gstins)
|
| 122 |
|
| 123 |
+
seller_state = get_state_from_gstin(seller_gstin)
|
| 124 |
+
buyer_state = get_state_from_gstin(buyer_gstin)
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
# -------- TAX -------- #
|
| 127 |
+
def get_tax(label):
|
| 128 |
+
m = re.search(label + r'.*?([\d,]+\.\d{2})', block, re.IGNORECASE)
|
| 129 |
+
return float(m.group(1).replace(",", "")) if m else 0.0
|
| 130 |
+
|
| 131 |
+
cgst = get_tax("CGST")
|
| 132 |
+
sgst = get_tax("SGST")
|
| 133 |
+
igst = get_tax("IGST")
|
| 134 |
|
| 135 |
# -------- TOTAL -------- #
|
| 136 |
+
total_match = re.search(
|
| 137 |
+
r'(Total|Grand Total|Invoice Total)[^\d]*([\d,]+\.\d{2})',
|
| 138 |
+
block,
|
| 139 |
+
re.IGNORECASE
|
| 140 |
+
)
|
| 141 |
|
| 142 |
+
floats = [float(x.replace(",", "")) for x in re.findall(r'[\d,]+\.\d{2}', block)]
|
|
|
|
| 143 |
|
| 144 |
+
total = float(total_match.group(2).replace(",", "")) if total_match else (max(floats) if floats else 0)
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
# -------- ROW -------- #
|
| 147 |
+
rows.append({
|
| 148 |
"Invoice Number": invoice_number,
|
| 149 |
"Invoice Date": invoice_date,
|
| 150 |
"Vendor": vendor,
|
| 151 |
+
|
| 152 |
+
"Seller GSTIN": seller_gstin,
|
| 153 |
+
"Seller State": seller_state,
|
| 154 |
+
|
| 155 |
+
"Buyer GSTIN": buyer_gstin,
|
| 156 |
+
"Buyer State": buyer_state,
|
| 157 |
+
|
| 158 |
+
"Ship GSTIN": ship_gstin,
|
| 159 |
+
"Other GSTINs": other_gstins,
|
| 160 |
+
|
| 161 |
"CGST": cgst,
|
| 162 |
"SGST": sgst,
|
| 163 |
"IGST": igst,
|
| 164 |
+
|
| 165 |
+
"Total Amount": total,
|
| 166 |
+
|
| 167 |
"Narration": f"Invoice from {vendor}",
|
| 168 |
"Debit Account": "Expense",
|
| 169 |
"Credit Account": vendor
|
| 170 |
+
})
|
|
|
|
|
|
|
| 171 |
|
| 172 |
df = pd.DataFrame(rows)
|
| 173 |
|
|
|
|
| 174 |
excel_file = "invoice_output.xlsx"
|
| 175 |
+
df.to_excel(excel_file, index=False)
|
|
|
|
|
|
|
| 176 |
|
| 177 |
return df, excel_file
|
| 178 |
|
|
|
|
| 180 |
# -------- UI -------- #
|
| 181 |
with gr.Blocks() as demo:
|
| 182 |
|
| 183 |
+
gr.Markdown("## 🧠 Smart GST Invoice Extractor (Advanced)")
|
| 184 |
|
| 185 |
file_input = gr.File(label="Upload Invoice PDF")
|
| 186 |
|
| 187 |
+
btn = gr.Button("Extract")
|
| 188 |
|
| 189 |
+
table = gr.Dataframe()
|
| 190 |
|
| 191 |
+
download = gr.File()
|
| 192 |
|
| 193 |
+
btn.click(extract_invoices, inputs=file_input, outputs=[table, download])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
demo.launch()
|