Spaces:
Sleeping
Sleeping
Download app/parser.py.bak from vamsi1103/flask-ocr-api: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/spaces/vamsi1103/flask-ocr-api/resolve/main/app/parser.py.bak
- Command line
-
hf download hf://spaces/vamsi1103/flask-ocr-api/app/parser.py.bak
-
curl -L -o parser.py.bak https://huggingface.co/spaces/vamsi1103/flask-ocr-api/resolve/main/app/parser.py.bak
4.19 kB
| import re | |
| from datetime import datetime | |
| def parse_ocr_results(ocr_data): | |
| """ | |
| Parses raw OCR extracted text into a structured JSON format with items and rates. | |
| ocr_data is a list of dicts: [{"text": "Tomato", "confidence": 0.98}, ...] | |
| """ | |
| items = [] | |
| date_str = datetime.now().strftime("%Y-%m-%d") | |
| if not ocr_data: | |
| return { | |
| "success": True, | |
| "date": date_str, | |
| "items": [], | |
| "debug_raw_text": [] | |
| } | |
| current_item = None | |
| for data in ocr_data: | |
| text = data["text"].strip() | |
| conf = data["confidence"] | |
| # Try to find a date in the text | |
| date_match = re.search(r'(\d{2,4}[-/]\d{1,2}[-/]\d{2,4})', text) | |
| if date_match: | |
| date_str = date_match.group(1) | |
| continue | |
| # Ignore purely serial numbers (e.g. just "1", "2") | |
| if re.match(r'^\d+$', text): | |
| continue | |
| # Check if item and rate are on the same line (e.g. "Tomato 40.00", "Tomato40.00", or "వంకాయలు 40.00") | |
| same_line_match = re.search(r'^(.*?[^\d\s/])\s*([\d\./]+)$', text) | |
| if same_line_match: | |
| name = same_line_match.group(1).strip() | |
| rate_text = same_line_match.group(2).strip() | |
| # Remove leading serial numbers from name (e.g. "1. Tomato" -> "Tomato") | |
| name = re.sub(r'^\d+[\.\-\s]+', '', name).strip() | |
| # Ensure name has some non-digit characters | |
| if re.search(r'[^\d\.\s/]', name): | |
| _add_item(items, name, rate_text, conf) | |
| current_item = None | |
| continue | |
| # Check if it's purely a rate (e.g., "40.00" or "100/150") | |
| rate_match = re.match(r'^([\d\.]+)(?:/([\d\.]+))?$', text.replace(" ", "")) | |
| if rate_match and current_item: | |
| rate_text = text | |
| _add_item(items, current_item["name"], rate_text, min(current_item["confidence"], conf)) | |
| current_item = None | |
| continue | |
| # Otherwise, if it has letters/non-digits, treat it as an item name | |
| if re.search(r'[^\d\.\s/]', text): | |
| # Ignore known header words that appear before the table starts | |
| if any(h in text for h in ["రైతుబజార", "ధరలు", "కూరగాయలురకం", "కేజ", "ధర", "సంఖత"]): | |
| current_item = None | |
| continue | |
| # Remove leading serial numbers | |
| clean_name = re.sub(r'^\d+[\.\-\s]+', '', text).strip() | |
| if clean_name: | |
| if current_item: | |
| # If the name is getting suspiciously long (like a header), reset it! | |
| if len(current_item["name"]) > 40: | |
| current_item = {"name": clean_name, "confidence": conf} | |
| else: | |
| current_item["name"] += " " + clean_name | |
| current_item["confidence"] = min(current_item["confidence"], conf) | |
| else: | |
| current_item = {"name": clean_name, "confidence": conf} | |
| return { | |
| "success": True, | |
| "date": date_str, | |
| "items": items, | |
| "debug_raw_text": [d["text"] for d in ocr_data] | |
| } | |
| def _add_item(items_list, name, rate_text, conf): | |
| clean_rate = rate_text.replace(" ", "") | |
| if "/" in clean_rate: | |
| try: | |
| rates = [float(x) for x in clean_rate.split("/") if x] | |
| items_list.append({ | |
| "name": name, | |
| "rate_type": "multiple", | |
| "rate": None, | |
| "available_rates": rates, | |
| "rate_text": rate_text, | |
| "confidence": round(conf, 2) | |
| }) | |
| except ValueError: | |
| pass | |
| else: | |
| try: | |
| rate_val = float(clean_rate) | |
| items_list.append({ | |
| "name": name, | |
| "rate_type": "single", | |
| "rate": rate_val, | |
| "rate_text": rate_text, | |
| "confidence": round(conf, 2) | |
| }) | |
| except ValueError: | |
| pass | |