File size: 2,631 Bytes
4818cbb
 
7a920b1
 
4818cbb
 
 
 
 
 
 
 
 
 
 
7a920b1
 
 
 
 
 
 
 
 
 
 
 
 
4818cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a920b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import base64
from io import BytesIO
import pandas as pd
import ast


def pil_to_base64(pil_image):
    """Convert PIL Image to base64 string"""
    buffered = BytesIO()
    pil_image.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode()
    return f"data:image/png;base64,{img_str}"


def image_to_base64(img_bytes):
    """Convert image bytes to base64 string with MIME type

    Handles both:
    - Dict format: {'bytes': b'...', 'path': None}
    - String format from CSV: "{'bytes': b'...', 'path': None}"
    - Raw bytes
    """
    if isinstance(img_bytes, str):
        img_dict = ast.literal_eval(img_bytes)
        if isinstance(img_dict, dict) and "bytes" in img_dict:
            img_bytes = img_dict["bytes"]

    elif isinstance(img_bytes, dict) and "bytes" in img_bytes:
        img_bytes = img_bytes["bytes"]

    # Encode to base64
    b64_string = base64.b64encode(img_bytes).decode("utf-8")
    return f"data:image/jpeg;base64,{b64_string}"


def create_training_example(row):
    """Create a training example with both classification and description tasks"""

    img_b64 = image_to_base64(row["image"])
    user_prompt = "Analyze this fashion product image and provide: 1) Master category, 2) Gender, 3) Sub-category, and 4) A detailed description."

    assistant_response = f"""
        Master Category: {row['masterCategory']}
        Gender: {row['gender']}
        Sub-category: {row['subCategory']}

        Description: This is a {row['gender'].lower()} {row['subCategory'].lower()} from the {row['masterCategory'].lower()} category."""

    return {
        "messages": [
            {
                "role": "system",
                "content": "You are a fashion product analyst. Classify products and generate detailed descriptions based on images.",
            },
            {
                "role": "user",
                "content": [
                    {"type": "image_url", "image_url": {"url": img_b64}},
                    {"type": "text", "text": user_prompt},
                ],
            },
            {"role": "assistant", "content": assistant_response},
        ]
    }


def load_test_data(csv_path="../data/test.csv"):
    """
    Load test data for inference from CSV file

    Args:
        csv_path: Path to the test CSV file (default: 'data/test.csv')

    Returns:
        pd.DataFrame: Test data with columns for inference
    """
    # Load test data from CSV
    df_test = pd.read_csv(csv_path)

    print(f"Loaded {len(df_test)} test examples from {csv_path}")
    print(f"Columns: {df_test.columns.tolist()}")

    return df_test