File size: 13,611 Bytes
d166583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f199c3e
d166583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72d6ba3
d166583
 
72d6ba3
d166583
 
 
 
 
 
 
 
 
 
 
72d6ba3
 
 
d166583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f199c3e
14987d0
f199c3e
d166583
 
 
 
 
 
 
 
 
14987d0
72d6ba3
 
 
d166583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
inference steps - to be run whenever a new image is uploaded
input: image and textual prompt
steps: 
1. generate an image (or more than one) with stable diffusion
2. GPT-4o - detect main pieces of furniture
3. perform object detection on the image looking for the main pieces of furniture
4. generate embeddings for the image and the subimages
5. perform a similarity search on the index of ikea products
6. return the results: generated image, main pieces of furniture, similar ikea products
"""

import logging
from datetime import datetime

# Set up logging to both file and console
log_filename = f"inference_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(log_filename),
        logging.StreamHandler()
    ]
)

import replicate
from pydantic import BaseModel, Field
from openai import OpenAI
import base64
import numpy as np
import os
import requests
from PIL import Image
from io import BytesIO
import sys
import dotenv
import pandas as pd
import faiss

logging.info("Loading environment variables...")
dotenv.load_dotenv()   
client = OpenAI()
# step 1

class Prompt(BaseModel):
    prompt: str = Field(description="A detailed prompt for a diffusion model")

def generate_prompt_for_flux(user_prompt):
    completion = client.beta.chat.completions.parse(
        model="gpt-4o-mini",
        messages=[
            {
            "role": "user",
            "content": [
                {"type": "text", "text": f"Generate a prompt for a diffusion model that is a more detailed version of the following prompt: {user_prompt}. Keep it succinct but more descriptive than the original. Just return a few words, listing possible relevant furniture elements and objects that will be present in the image. "},
            ],
        }
    ],
         response_format=Prompt,
    )

    analysis = completion.choices[0].message.parsed
    return analysis.prompt

def search_similar_products(image_path, index, metadata_df, top_k=5):
    """
    Search for similar products given a local image
    
    Args:
        image_path (str): Path to the query image
        index: FAISS index
        metadata_df: DataFrame containing product metadata
        top_k (int): Number of similar items to return
    
    Returns:
        list: List of dictionaries containing similar product information
    """
    logging.info(f"\nGenerating embedding for image: {image_path}")
    # Generate embedding for the query image
    output = replicate.run(
        "krthr/clip-embeddings:1c0371070cb827ec3c7f2f28adcdde54b50dcd239aa6faea0bc98b174ef03fb4",
        input={"image": image_path}
    )
    if 'embedding' in output:
        query_embedding = np.array(output['embedding'])
    else:
        query_embedding = np.array(output).astype('float32').reshape(1, -1)
    
    logging.info("Searching FAISS index...")
    # Search the index
    distances, indices = index.search(np.array([query_embedding]), top_k)
    
    logging.info("Processing search results...")
    # Get the metadata for the similar products
    results = []
    for idx, distance in zip(indices[0], distances[0]):
        result = {
            'product_url': metadata_df.iloc[idx]['product_url'],
            'image_url': metadata_df.iloc[idx]['image_url'],
            'distance': float(distance)
        }
        results.append(result)
    
    return results


def find_similar_ikea_products(image_input, index, metadata_df, top_k=5, process_detections_list=None):
    """
    Convenience function to find similar IKEA products
    
    Args:
        image_input (str): Path to local image file or URL of image
        top_k (int): Number of similar items to return
        process_detections_list (list, optional): List of detection dictionaries containing bbox and label
    """
    logging.info("\nProcessing full image...")
    # First process the full image
    logging.info("\nProcessing full image:")
    # Handle both local files and URLs
    if image_input.startswith(('http://', 'https://')):
        logging.info(f"Processing URL image: {image_input}")
        image_path = image_input
    else:
        logging.info(f"Processing local image: {image_input}")
        if not os.path.exists(image_input):
            raise FileNotFoundError(f"Local image file not found: {image_input}")
        image_path = open(image_input, "rb")
        
    results = search_similar_products(image_path, index, metadata_df, top_k)
    
    logging.info(f"\nTop {top_k} similar products (overall image):")
    for i, result in enumerate(results, 1):
        logging.info(f"\n{i}. Similarity score: {1 / (1 + result['distance']):.3f}")
        logging.info(f"Product URL: {result['product_url']}")
        logging.info(f"Image URL: {result['image_url']}")
    
    # If detections are provided, process sub-images
    if process_detections_list:
        logging.info("\nProcessing object detections...")
        # Load the image
        if isinstance(image_input, str) and image_input.startswith(('http://', 'https://')):
            image_path = image_input
        else:
            # local image processing - 
            image = Image.open(image_input)
        
        # Process each detection
        detections = process_detections_list['detections']
        for i, detection in enumerate(detections):
            logging.info(f"\nProcessing detection {i+1}: {detection['label']}")
            logging.info(f"Confidence: {detection['confidence']:.3f}")
            
            # Extract bounding box coordinates
            x1, y1, x2, y2 = detection['bbox']
            
            logging.info(f"Cropping image to bbox: ({x1}, {y1}, {x2}, {y2})")
            # Crop the image to the bounding box
            cropped = image.crop((x1, y1, x2, y2))
            
            # Save the cropped image temporarily
            temp_path = f"temp_crop_{i}.jpg"
            cropped.save(temp_path)
            logging.info(f"Saved temporary crop to: {temp_path}")
            
            try:
                # Find similar products for this crop
                logging.info(f"\nFinding similar products for {detection['label']}:")
                sub_results = search_similar_products(temp_path, index, metadata_df, top_k)
                
                logging.info(f"\nTop {top_k} similar products for {detection['label']}:")
                for j, result in enumerate(sub_results, 1):
                    logging.info(f"\n{j}. Similarity score: {1 / (1 + result['distance']):.3f}")
                    logging.info(f"Product URL: {result['product_url']}")
                    logging.info(f"Image URL: {result['image_url']}")
                
            except Exception as e:
                logging.error(f"Error processing detection {i+1}: {e}")
            
            finally:
                # Clean up temporary file
                if os.path.exists(temp_path):
                    logging.info(f"Cleaning up temporary file: {temp_path}")
                    os.remove(temp_path)
    
    return results


def generate_image(prompt, control_image, guidance_scale, output_quality, negative_prompt, control_strength, image_to_image_strength, control_type):
    logging.info("\nGenerating image with Stable Diffusion...")
    logging.info(f"Prompt: {prompt}")

    if isinstance(control_image, str):
        image = open(control_image, "rb")
    else:
        image = control_image
    
    input = {
        "prompt": prompt,
        "control_image": image,
        "guidance_scale": guidance_scale,
        "output_quality": output_quality,
        "negative_prompt": negative_prompt,
        "control_strength": control_strength,
        "image_to_image_strength": image_to_image_strength,
        "control_type": control_type
    }

    logging.info("Running image generation model...")
    output = replicate.run(
        "xlabs-ai/flux-dev-controlnet:9a8db105db745f8b11ad3afe5c8bd892428b2a43ade0b67edc4e0ccd52ff2fda",
        input=input
)
    logging.info("Saving generated images...")
    for index, item in enumerate(output):
        with open(f"output_{index}.jpg", "wb") as file:
            file.write(item.read())
        logging.info(f"Saved output_{index}.jpg")
    return output

# step 2

def analyze_image(image_path):  
    logging.info(f"\nAnalyzing image with GPT-4V: {image_path}")

    class ImageAnalysis(BaseModel):
        objects: list[str] = Field(description="A list of objects in the image")

    # Function to encode the image
    def encode_image(image_path):
        logging.info("Encoding image to base64...")
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')

    # Encode the image
    encoded_image = encode_image(image_path)

    logging.info("Sending request to GPT-4o-mini vision...")
    completion = client.beta.chat.completions.parse(
        model="gpt-4o-mini",
        messages=[
            {
            "role": "user",
            "content": [
                {"type": "text", "text": "Analyze this image and list the main objects of furniture in the image."},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{encoded_image}"
                    },
                },
            ],
        }
    ],
         response_format=ImageAnalysis,
    )

    analysis = completion.choices[0].message.parsed
    main_objects = ', '.join(analysis.objects)
    logging.info(f"""
    Objects: {', '.join(analysis.objects)}
    """)
    return main_objects

# step 3

def detect_objects(image_path, main_objects):
    logging.info(f"\nDetecting objects in image: {image_path}")
    image = open(image_path, "rb")
    input = {
        "image": image,
        "query": main_objects,
        "box_threshold": 0.2,
        "text_threshold": 0.2
    }

    logging.info("Running object detection model...")
    output = replicate.run(
        "adirik/grounding-dino:efd10a8ddc57ea28773327e881ce95e20cc1d734c589f7dd01d2036921ed78aa",
        input=input
    )
    logging.info("Detection results:")
    logging.info(output)
    return output

# step 4, 5
def search_index(image_path, index, metadata_df, main_objects = None, top_k=5):
    logging.info(f"\nSearching index for similar products to: {image_path}")
    #process_detections_list = detect_objects(image_path, main_objects)
    results = find_similar_ikea_products(image_path, index, metadata_df, top_k=5)
    return results




def main(prompt, control_image, index, metadata_df):
    """
    Main function to orchestrate the entire inference pipeline
    
    Args:
        prompt (str): Text prompt for image generation
        control_image: Input image for controlled generation
        index: FAISS index for similarity search
        metadata_df: DataFrame containing product metadata
    
    Returns:
        dict: Results containing generated images, detected objects, and similar products
    """


    logging.info("\nStarting inference pipeline...")
    results = {}

    logging.info("\nStep 0: Generating a detailed prompt for the diffusion model...")
    # Step 0: Generate a detailed prompt for the diffusion model
    prompt = generate_prompt_for_flux(prompt)

    prompt += ", realistic, high quality, 8K, photorealistic, high detail, sharp focus"

    logging.info(f"\nGenerated prompt: {prompt}")
    
    logging.info("\nStep 1: Generating image...")
    # Step 1: Generate image
    generated_images = generate_image(
        prompt=prompt,
        control_image=control_image,
        guidance_scale=2.5,
        output_quality=100,
        negative_prompt="low quality, ugly, distorted, artefacts, low detail, low quality, low resolution, low definition, imaginary, unrealistic, fictional",
        control_strength=0.5,
        image_to_image_strength=0.1,
        control_type="canny"
    )
    results['generated_images'] = generated_images
    results['generated_image_path'] = "output_0.jpg"
    
    # logging.info("\nStep 2: Analyzing generated image...")
    # # Step 2: Analyze generated image with GPT-4V
    obj_detection = False
    if obj_detection:
        main_objects = analyze_image("output_0.jpg")  # Using the first generated image
        results['detected_furniture'] = main_objects
    
    logging.info("\nSteps 3-5: Detecting objects and searching for similar products...")
    # Step 3 & 4 & 5: Detect objects and search for similar products
    similar_products = search_index(
        image_path="output_0.jpg",
        index=index,
        metadata_df=metadata_df,
        top_k=5
    )
    results['similar_products'] = similar_products
    
    return results

def load_index():
    logging.info("\nLoading FAISS index...")
    return faiss.read_index("data/ikea_faiss.index")

def load_metadata():
    logging.info("Loading metadata...")
    return pd.read_csv("data/filtered_metadata.csv")

if __name__ == "__main__":
    # Example usage
    logging.info("\nStarting program...")
    
    if len(sys.argv) != 3:
        logging.error("Usage: python inference.py <prompt> <link to control image>")
        sys.exit(1)
        
    prompt = sys.argv[1]
    control_image = sys.argv[2]
    
    logging.info("\nLoading required data...")
    # Load your FAISS index and metadata_df here
    index = load_index()
    metadata_df = load_metadata()
    
    results = main(prompt, control_image, index, metadata_df)
    logging.info("\nPipeline completed successfully!")