Here is my problem statement 
"


# ML Challenge Problem Statement

## Feature Extraction from Images

In this hackathon, the goal is to create a machine learning model that extracts entity values from images. This capability is crucial in fields like healthcare, e-commerce, and content moderation, where precise product information is vital. As digital marketplaces expand, many products lack detailed textual descriptions, making it essential to obtain key details directly from images. These images provide important information such as weight, volume, voltage, wattage, dimensions, and many more, which are critical for digital stores.

### Data Description: 

The dataset consists of the following columns: 

1. **index:** An unique identifier (ID) for the data sample
2. **image_link**: Public URL where the product image is available for download. Example link - https://m.media-amazon.com/images/I/71XfHPR36-L.jpg
    To download images use `download_images` function from `src/utils.py`. See sample code in `src/test.ipynb`.
3. **group_id**: Category code of the product
4. **entity_name:** Product entity name. For eg: “item_weight” 
5. **entity_value:** Product entity value. For eg: “34 gram” 
    Note: For test.csv, you will not see the column `entity_value` as it is the target variable.

### Output Format:

The output file should be a csv with 2 columns:

1. **index:** The unique identifier (ID) of the data sample. Note the index should match the test record index.
2. **prediction:** A string which should have the following format: “x unit” where x is a float number in standard formatting and unit is one of the allowed units (allowed units are mentioned in the Appendix). The two values should be concatenated and have a space between them. For eg: “2 gram”, “12.5 centimetre”, “2.56 ounce” are valid. Few invalid cases: “2 gms”, “60 ounce/1.7 kilogram”, “2.2e2 kilogram” etc.
    Note: Make sure to output a prediction for all indices. If no value is found in the image for any test sample, return empty string, i.e, `“”`. If you have less/more number of output samples in the output file as compared to test.csv, your output won’t be evaluated. 

### File Descriptions:

*source files*

1. **src/sanity.py**: Sanity checker to ensure that the final output file passes all formatting checks. Note: the script will not check if less/more number of predictions are present compared to the test file. See sample code in `src/test.ipynb` 
2. **src/utils.py**: Contains helper functions for downloading images from the image_link.
3. **src/constants.py:** Contains the allowed units for each entity type.
4. **sample_code.py:** We also provided a sample dummy code that can generate an output file in the given format. Usage of this file is optional. 

*Dataset files*

1. **dataset/train.csv**: Training file with labels (`entity_value`).
2. **dataset/test.csv**: Test file without output labels (`entity_value`). Generate predictions using your model/solution on this file's data and format the output file to match sample_test_out.csv (Refer the above section "Output Format")
3. **dataset/sample_test.csv**: Sample test input file.
4. **dataset/sample_test_out.csv**: Sample outputs for sample_test.csv. The output for test.csv must be formatted in the exact same way. Note: The predictions in the file might not be correct

### Constraints

1. You will be provided with a sample output file and a sanity checker file. Format your output to match the sample output file exactly and pass it through the sanity checker to ensure its validity. Note: If the file does not pass through the sanity checker, it will not be evaluated. You should recieve a message like `Parsing successfull for file: ...csv` if the output file is correctly formatted.

2. You are given the list of allowed units in constants.py and also in Appendix. Your outputs must be in these units. Predictions using any other units will be considered invalid during validation.

### Evaluation Criteria

Submissions will be evaluated based on F1 score, which are standard measures of prediction accuracy for classification and extraction problems.

Let GT = Ground truth value for a sample and OUT be output prediction from the model for a sample. Then we classify the predictions into one of the 4 classes with the following logic: 

1. *True Positives* - If OUT != `""` and GT != `""` and OUT == GT
2. *False Positives* - If OUT != `""` and GT != `""` and OUT != GT
3. *False Positives* - If OUT != `""` and GT == `""`
4. *False Negatives* - If OUT == `""` and GT != `""`
5. *True Negatives* - If OUT == `""` and GT == `""` 

Then, F1 score = 2*Precision*Recall/(Precision + Recall) where:

1. Precision = True Positives/(True Positives + False Positives)
2. Recall = True Positives/(True Positives + False Negatives)

### Submission File

Upload a test_out.csv file in the Portal with the exact same formatting as sample_test_out.csv

### Appendix

```
entity_unit_map = {
  "width": {
    "centimetre",
    "foot",
    "millimetre",
    "metre",
    "inch",
    "yard"
  },
  "depth": {
    "centimetre",
    "foot",
    "millimetre",
    "metre",
    "inch",
    "yard"
  },
  "height": {
    "centimetre",
    "foot",
    "millimetre",
    "metre",
    "inch",
    "yard"
  },
  "item_weight": {
    "milligram",
    "kilogram",
    "microgram",
    "gram",
    "ounce",
    "ton",
    "pound"
  },
  "maximum_weight_recommendation": {
    "milligram",
    "kilogram",
    "microgram",
    "gram",
    "ounce",
    "ton",
    "pound"
  },
  "voltage": {
    "millivolt",
    "kilovolt",
    "volt"
  },
  "wattage": {
    "kilowatt",
    "watt"
  },
  "item_volume": {
    "cubic foot",
    "microlitre",
    "cup",
    "fluid ounce",
    "centilitre",
    "imperial gallon",
    "pint",
    "decilitre",
    "litre",
    "millilitre",
    "quart",
    "cubic inch",
    "gallon"
  }
}
```

""


this is my utils.py
"
import re
from src import constants # Correct import statement
import os
import requests
import pandas as pd
import multiprocessing
import time
from time import time as timer
from tqdm import tqdm
import numpy as np
from pathlib import Path
from functools import partial
import requests
import urllib
from PIL import Image

def common_mistake(unit):
if unit in constants.allowed_units:
return unit
if unit.replace('ter', 'tre') in constants.allowed_units:
return unit.replace('ter', 'tre')
if unit.replace('feet', 'foot') in constants.allowed_units:
return unit.replace('feet', 'foot')
return unit

def parse_string(s):
s_stripped = "" if s==None or str(s)=='nan' else s.strip()
if s_stripped == "":
return None, None
pattern = re.compile(r'^-?\d+(.\d+)?\s+[a-zA-Z\s]+$')
if not pattern.match(s_stripped):
raise ValueError("Invalid format in {}".format(s))
parts = s_stripped.split(maxsplit=1)
number = float(parts[0])
unit = common_mistake(parts[1])
if unit not in constants.allowed_units:
raise ValueError("Invalid unit [{}] found in {}. Allowed units: {}".format(
unit, s, constants.allowed_units))
return number, unit


def create_placeholder_image(image_save_path):
try:
placeholder_image = Image.new('RGB', (100, 100), color='black')
placeholder_image.save(image_save_path)
except Exception as e:
return

def download_image(image_link, save_folder, retries=3, delay=3):
if not isinstance(image_link, str):
return

filename = Path(image_link).name
image_save_path = os.path.join(save_folder, filename)

if os.path.exists(image_save_path):
return

for _ in range(retries):
try:
urllib.request.urlretrieve(image_link, image_save_path)
return
except:
time.sleep(delay)

create_placeholder_image(image_save_path) #Create a black placeholder image for invalid links/images

def download_images(image_links, download_folder, allow_multiprocessing=True):
if not os.path.exists(download_folder):
os.makedirs(download_folder)

if allow_multiprocessing:
download_image_partial = partial(
download_image, save_folder=download_folder, retries=3, delay=3)

with multiprocessing.Pool(64) as pool:
list(tqdm(pool.imap(download_image_partial, image_links), total=len(image_links)))
pool.close()
pool.join()
else:
for image_link in tqdm(image_links, total=len(image_links)):
download_image(image_link, save_folder=download_folder, retries=3, delay=3)

"
and this is my sanity.py
"
import pandas as pd
import argparse
import re
import os
from src import constants
from utils import parse_string

def check_file(filename):
if not filename.lower().endswith('.csv'):
raise ValueError("Only CSV files are allowed.")
if not os.path.exists(filename):
raise FileNotFoundError("Filepath: {} invalid or not found.".format(filename))

def sanity_check(test_filename, output_filename):
check_file(test_filename)
check_file(output_filename)

try:
test_df = pd.read_csv(test_filename)
output_df = pd.read_csv(output_filename)
except Exception as e:
raise ValueError(f"Error reading the CSV files: {e}")

if 'index' not in test_df.columns:
raise ValueError("Test CSV file must contain the 'index' column.")

if 'index' not in output_df.columns or 'prediction' not in output_df.columns:
raise ValueError("Output CSV file must contain 'index' and 'prediction' columns.")

missing_index = set(test_df['index']).difference(set(output_df['index']))
if len(missing_index) != 0:
print("Missing index in test file: {}".format(missing_index))

extra_index = set(output_df['index']).difference(set(test_df['index']))
if len(extra_index) != 0:
print("Extra index in test file: {}".format(extra_index))

output_df.apply(lambda x: parse_string(x['prediction']), axis=1)
print("Parsing successfull for file: {}".format(output_filename))

if name == "main":
#Usage example: python sanity.py --test_filename sample_test.csv --output_filename sample_test_out.csv

parser = argparse.ArgumentParser(description="Run sanity check on a CSV file.")
parser.add_argument("--test_filename", type=str, required=True, help="The test CSV file name.")
parser.add_argument("--output_filename", type=str, required=True, help="The output CSV file name to check.")
args = parser.parse_args()
try:
sanity_check(args.test_filename, args.output_filename)
except Exception as e:
print('Error:', e)
"
and this is my constants.py
"
entity_unit_map = {
'width': {'centimetre', 'foot', 'inch', 'metre', 'millimetre', 'yard'},
'depth': {'centimetre', 'foot', 'inch', 'metre', 'millimetre', 'yard'},
'height': {'centimetre', 'foot', 'inch', 'metre', 'millimetre', 'yard'},
'item_weight': {'gram',
'kilogram',
'microgram',
'milligram',
'ounce',
'pound',
'ton'},
'maximum_weight_recommendation': {'gram',
'kilogram',
'microgram',
'milligram',
'ounce',
'pound',
'ton'},
'voltage': {'kilovolt', 'millivolt', 'volt'},
'wattage': {'kilowatt', 'watt'},
'item_volume': {'centilitre',
'cubic foot',
'cubic inch',
'cup',
'decilitre',
'fluid ounce',
'gallon',
'imperial gallon',
'litre',
'microlitre',
'millilitre',
'pint',
'quart'}
}

allowed_units = {unit for entity in entity_unit_map for unit in entity_unit_map[entity]}
"


based on the problem statement please give me the solution code . Give me the complete code and make sure that it should work perfectly , accurately and should  not have any errors. You are allowed to use any pretrained models and algorithms ...or something better than that . But i need the best working and suitable solution for it and has to work will a very less error and has to have better accuracy etc...make it work .