Spaces:
Runtime error
Runtime error
Download src/extractor.py from jayanth-2005/output: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/spaces/jayanth-2005/output/resolve/main/src/extractor.py
- Command line
-
hf download hf://spaces/jayanth-2005/output/src/extractor.py
-
curl -L -o extractor.py https://huggingface.co/spaces/jayanth-2005/output/resolve/main/src/extractor.py
1.35 kB
| import re | |
| GST_REGEX = r"\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}" | |
| def extract_amount(line): | |
| nums = re.findall(r"\d+\.\d+|\d+", line.replace(",", "")) | |
| return float(nums[-1]) if nums else 0.0 | |
| def extract_fields(text): | |
| lines = text.split("\n") | |
| data = { | |
| "Vendor": None, | |
| "GSTIN": None, | |
| "Date": None, | |
| "Invoice No": None, | |
| "Taxable": 0.0, | |
| "CGST": 0.0, | |
| "SGST": 0.0, | |
| "IGST": 0.0, | |
| "Total": 0.0 | |
| } | |
| gst_match = re.search(GST_REGEX, text) | |
| if gst_match: | |
| data["GSTIN"] = gst_match.group() | |
| for line in lines: | |
| l = line.lower() | |
| if "invoice" in l and not data["Invoice No"]: | |
| data["Invoice No"] = line.split()[-1] | |
| if "date" in l and not data["Date"]: | |
| data["Date"] = line.split()[-1] | |
| if "cgst" in l: | |
| data["CGST"] = extract_amount(line) | |
| if "sgst" in l: | |
| data["SGST"] = extract_amount(line) | |
| if "igst" in l: | |
| data["IGST"] = extract_amount(line) | |
| if "taxable" in l: | |
| data["Taxable"] = extract_amount(line) | |
| if "total" in l: | |
| data["Total"] = extract_amount(line) | |
| for line in lines: | |
| if len(line.strip()) > 5: | |
| data["Vendor"] = line.strip() | |
| break | |
| return data | |