Modify this code to check if any entity that is present in json has received thier respective bounding box or not. If not then try to find their bounding boxes. Any entity that is present in json should have its bounding box. And only process these sections "invoice_details", "Payment Details" and "amounts_and_tax" from json

import json
import pandas as pd
import cv2
from thefuzz import fuzz
from thefuzz import process

def preprocess_entity(entity):
    """Preprocess entities by removing commas in numerical values."""
    return entity.replace(",", "").strip()

def is_nearby(current_box, next_box, max_line_difference=10, max_distance=100):
    """Check if the next bounding box is nearby to the current one.
       Both should be on the same line or the next line and within a certain distance."""
    same_line = abs(current_box["top"] - next_box["top"]) <= max_line_difference
    close_enough = abs(current_box["right"] - next_box["left"]) <= max_distance
    return same_line and close_enough

def merge_boxes(boxes):
    """Merge bounding boxes of a multi-token entity."""
    if not boxes: return None
    min_left = min(box["left"] for box in boxes)
    max_right = max(box["right"] for box in boxes)
    min_top = min(box["top"] for box in boxes)
    max_bottom = max(box["bottom"] for box in boxes)
    return {
        "left": min_left, "right": max_right,
        "top": min_top, "bottom": max_bottom
    }
    
def process_single_token_entity(dataframe, entity, threshold=85):
    """Process a single token entity to find its bounding box using fuzzy matching."""
    best_score = 0
    best_box = None
    entity = preprocess_entity(entity)

    for _, row in dataframe.iterrows():
        ocr_text = preprocess_entity(row['text'])
        score = fuzz.ratio(entity, ocr_text)
        
        if score > best_score and score > threshold:
            best_score = score
            best_box = {
                "left": row["left"], "right": row["right"],
                "top": row["top"], "bottom": row["bottom"]
            }
    
    return best_box

def process_multi_token_entity(dataframe, entity, threshold=85):
    """Process a multi-token entity to find and merge its bounding boxes using fuzzy matching."""
    tokens = entity.split()
    boxes = []
    for token in tokens:
        box = process_single_token_entity(dataframe, token, threshold)
        if box is None:
            return None  # Unable to find all tokens
        boxes.append(box)

    # Verify if boxes are near to each other
    if all(is_nearby(boxes[i], boxes[i + 1]) for i in range(len(boxes) - 1)):
        return merge_boxes(boxes)
    else:
        return None

def draw_bounding_boxes(image_path, bounding_boxes):
    """Draw bounding boxes on the image."""
    image = cv2.imread(image_path)
    for box in bounding_boxes:
        if box is not None:
            top_left = (box["left"], box["top"])
            bottom_right = (box["right"], box["bottom"])
            cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
    cv2.imwrite("/home/ritik1s/Desktop/bbox_issues/temp_GPT/output_image_15_march.jpg", image)

def main(json_path, csv_path, image_path):
    with open(json_path, 'r') as f:
        data = json.load(f)
    
    dataframe = pd.read_csv(csv_path)
    processed_entities = set()  # To track processed entities, ensuring no repetition
    bounding_boxes = []

    for section, entities in data.items():
        # ‘entity’ refers to each key-value pair within the sections of the JSON
        if section == "invoice_details" or section == "Payment Details" or section == "amounts_and_tax":
            for entity_name, value in entities.items():
                if value in processed_entities:
                    continue  # Skipping already processed entities
                
                value_no_comma = preprocess_entity(value)  # Handling numerical values
                if " " in value_no_comma:  # Multi-token entity
                    box = process_multi_token_entity(dataframe, value_no_comma)
                else:  # Single token entity
                    box = process_single_token_entity(dataframe, value_no_comma)
                
                # Check if the box is not None and not already added
                if box:
                    bounding_boxes.append(box)
                    processed_entities.add(value)  # Mark entity as processed
        else:
            pass

    draw_bounding_boxes(image_path, bounding_boxes)

main("/home/ritik1s/Desktop/bbox_issues/temp_GPT/row_skip.json", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.csv", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.jpeg")