|
1 | 1 | import base64 |
2 | 2 | import logging |
3 | 3 | import os |
| 4 | +import re |
4 | 5 | import time |
5 | 6 |
|
6 | 7 | import anthropic |
@@ -148,7 +149,9 @@ def _download_images(urls: list[str]) -> list[tuple[str, bytes, str]]: |
148 | 149 | "- Have a clean, white, or neutral background\n" |
149 | 150 | "- Show the complete product, not a close-up of a detail\n" |
150 | 151 | "- Match the specific product described (correct brand, model, color if known)\n\n" |
151 | | - "If NONE of the images are a good match for the product, respond with 0." |
| 152 | + "IMPORTANT: Your entire response must be a single integer and nothing else. " |
| 153 | + "No explanation, no reasoning, no text. Just the number.\n" |
| 154 | + "If NONE of the images are a good match, respond with: 0" |
152 | 155 | ) |
153 | 156 |
|
154 | 157 |
|
@@ -222,8 +225,13 @@ def _select_best_image( |
222 | 225 | try: |
223 | 226 | choice = int(reply) |
224 | 227 | except ValueError: |
225 | | - logger.warning("Vision model returned non-numeric response: %s", reply) |
226 | | - return None |
| 228 | + match = re.search(r'\b([0-5])\b', reply) |
| 229 | + if match: |
| 230 | + choice = int(match.group(1)) |
| 231 | + logger.info("Extracted choice %d from verbose response", choice) |
| 232 | + else: |
| 233 | + logger.warning("Vision model returned non-numeric response: %s", reply) |
| 234 | + return None |
227 | 235 |
|
228 | 236 | if choice == 0: |
229 | 237 | return None |
@@ -291,7 +299,7 @@ def find_product_image(self, catalog_product_id: int): |
291 | 299 |
|
292 | 300 | if image_url: |
293 | 301 | entry.image_url = image_url |
294 | | - session.flush() |
| 302 | + session.commit() |
295 | 303 | logger.info("Set image_url for %s: %s", label, image_url) |
296 | 304 | else: |
297 | 305 | logger.info("No suitable image found for %s", label) |
0 commit comments