File size: 7,402 Bytes
7a920b1
5515ef5
7a920b1
 
b32f568
7a920b1
 
b32f568
 
7a920b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b32f568
7a920b1
 
 
 
 
 
 
 
 
b32f568
7a920b1
 
 
 
5515ef5
7a920b1
 
 
 
5515ef5
7a920b1
 
 
 
 
 
b32f568
 
 
 
 
 
 
 
7a920b1
 
 
 
b32f568
7a920b1
 
 
 
b32f568
7a920b1
 
 
 
 
 
 
 
 
 
5515ef5
 
 
 
 
b32f568
5515ef5
 
 
 
 
 
 
 
 
b32f568
5515ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b32f568
 
 
 
 
 
 
 
5515ef5
 
 
 
b32f568
5515ef5
 
 
 
b32f568
5515ef5
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import os
from openai import OpenAI, AsyncOpenAI
from pydantic import BaseModel, Field
from typing import Optional, Literal
from src.modules.constants import PROMPT_LIBRARY

SYSTEM_PROMPT = """
    You are an e-commerce fashion catalog assistant.
    Classify products and generate detailed descriptions based on images.
    """
USER_PROMPT = """
    Analyze this fashion product image and provide:
    1) Master category, 2) Gender, 3) Sub-category, and 4) A detailed description.
    """


class ProductClassification(BaseModel):
    """Structured output model for fashion product classification and description"""

    master_category: Literal["Footwear", "Accessories", "Apparel", "Personal Care"] = (
        Field(description="The master category of the product")
    )
    gender: Literal["Men", "Women", "Unisex", "Boys", "Girls"] = Field(
        description="The target gender for the product"
    )
    sub_category: Literal[
        "Sandal",
        "Scarves",
        "Shoes",
        "Watches",
        "Innerwear",
        "Topwear",
        "Belts",
        "Bags",
        "Flip Flops",
        "Nails",
        "Bottomwear",
        "Fragrance",
        "Wallets",
        "Jewellery",
        "Loungewear and Nightwear",
        "Socks",
        "Headwear",
        "Lips",
        "Saree",
        "Ties",
        "Accessories",
        "Eyewear",
        "Dress",
        "Skin Care",
        "Stoles",
        "Makeup",
        "Cufflinks",
        "Skin",
        "Hair",
        "Apparel Set",
        "Water Bottle",
        "Eyes",
        "Shoe Accessories",
        "Umbrellas",
        "Mufflers",
        "Beauty Accessories",
        "Gloves",
        "Sports Accessories",
        "Perfumes",
        "Bath and Body",
    ] = Field(description="The specific sub-category of the product")
    description: str = Field(
        description="A detailed description of the product based on the image"
    )


def analyze_product_image(
    image_url: str,
    model: str = "accounts/fireworks/models/qwen2p5-vl-72b-instruct",
    api_key: Optional[str] = None,
    provider: str = "Fireworks",
    prompt_style: Optional[str] = None,
) -> ProductClassification:
    """
    Analyze a fashion product image using VLM with structured output

    Args:
        image_url: URL or base64-encoded image string (with data:image prefix)
        model: Model to use for inference (default: Qwen2.5 VL 72B)
        api_key: Fireworks API key (defaults to FIREWORKS_API_KEY env variable)
        provider: Provider to use for inference (default: Fireworks)
        prompt_style: Prompt style from library (concise, descriptive, explanatory). Defaults to fallback prompts.

    Returns:
        ProductClassification: Structured classification and description
    """
    if provider.lower() in ["fireworks", "fireworksai"]:
        client = OpenAI(
            api_key=api_key or os.getenv("FIREWORKS_API_KEY"),
            base_url="https://api.fireworks.ai/inference/v1",
        )
    elif provider.lower() == "openai":
        client = OpenAI(
            api_key=api_key or os.getenv("OPENAI_API_KEY"),
        )
    else:
        raise ValueError(f"Unknown provider: {provider}")

    # Get prompts from library or use defaults
    if prompt_style and prompt_style in PROMPT_LIBRARY:
        system_prompt = PROMPT_LIBRARY[prompt_style]["system"]
        user_prompt = PROMPT_LIBRARY[prompt_style]["user"]
    else:
        system_prompt = SYSTEM_PROMPT
        user_prompt = USER_PROMPT

    # Call the API with structured output
    completion = client.beta.chat.completions.parse(
        model=model,
        messages=[
            {"role": "system", "content": system_prompt},
            {
                "role": "user",
                "content": [
                    {"type": "image_url", "image_url": {"url": image_url}},
                    {"type": "text", "text": user_prompt},
                ],
            },
        ],
        response_format=ProductClassification,
    )

    # Extract and return the structured output
    return completion.choices[0].message.parsed


async def analyze_product_image_async(
    image_url: str,
    model: str = "accounts/fireworks/models/qwen2p5-vl-72b-instruct",
    api_key: Optional[str] = None,
    provider: str = "Fireworks",
    prompt_style: Optional[str] = None,
) -> ProductClassification:
    """
    Async version of analyze_product_image for concurrent processing

    Args:
        image_url: URL or base64-encoded image string (with data:image prefix)
        model: Model to use for inference (default: Qwen2.5 VL 72B)
        api_key: API key (defaults to provider-specific env variable)
        provider: Provider to use for inference (default: Fireworks)
        prompt_style: Prompt style from library (concise, descriptive, explanatory). Defaults to fallback prompts.

    Returns:
        ProductClassification: Structured classification and description
    """
    if provider.lower() in ["fireworks", "fireworksai"]:
        client = AsyncOpenAI(
            api_key=api_key or os.getenv("FIREWORKS_API_KEY"),
            base_url="https://api.fireworks.ai/inference/v1",
        )
    elif provider.lower() == "openai":
        client = AsyncOpenAI(
            api_key=api_key or os.getenv("OPENAI_API_KEY"),
        )
    else:
        raise ValueError(f"Unknown provider: {provider}")

    # Get prompts from library or use defaults
    if prompt_style and prompt_style in PROMPT_LIBRARY:
        system_prompt = PROMPT_LIBRARY[prompt_style]["system"]
        user_prompt = PROMPT_LIBRARY[prompt_style]["user"]
    else:
        system_prompt = SYSTEM_PROMPT
        user_prompt = USER_PROMPT

    # Call the API with structured output
    completion = await client.beta.chat.completions.parse(
        model=model,
        messages=[
            {"role": "system", "content": system_prompt},
            {
                "role": "user",
                "content": [
                    {"type": "image_url", "image_url": {"url": image_url}},
                    {"type": "text", "text": user_prompt},
                ],
            },
        ],
        response_format=ProductClassification,
    )

    # Extract and return the structured output
    return completion.choices[0].message.parsed


def batch_analyze_products(
    image_urls: list[str],
    model: str = "accounts/fireworks/models/qwen2p5-vl-72b-instruct",
    api_key: Optional[str] = None,
    base_url: str = "https://api.fireworks.ai/inference/v1",
) -> list[Optional[ProductClassification]]:
    """
    Analyze multiple fashion product images

    Args:
        image_urls: List of image URLs or base64-encoded strings
        model: Model to use for inference
        api_key: Fireworks API key
        base_url: API base URL

    Returns:
        list[Optional[ProductClassification]]: List of structured classifications (None for failed analyses)
    """
    results = []
    for idx, image_url in enumerate(image_urls):
        try:
            result = analyze_product_image(
                image_url=image_url, model=model, api_key=api_key, base_url=base_url
            )
            results.append(result)
            print(f"Processed image {idx + 1}/{len(image_urls)}")
        except Exception as e:
            print(f"Error processing image {idx + 1}: {e}")
            results.append(None)
    return results