Commit
·
c65ba24
1
Parent(s):
fefecb6
Use scaled-down Lamoda images
Browse files
app.py
CHANGED
@@ -95,21 +95,56 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
|
|
95 |
|
96 |
return []
|
97 |
|
98 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
"""Download images and convert them to base64 format for Gemini."""
|
100 |
encoded_images = []
|
101 |
-
|
|
|
102 |
for url in image_urls:
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
return encoded_images
|
114 |
|
115 |
def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str) -> str:
|
@@ -151,7 +186,7 @@ def process_input(id_product_money: str, prompt: str) -> Tuple[List[str], str, s
|
|
151 |
print(f"\nAll image URLs: {all_image_urls}")
|
152 |
|
153 |
print("\nDownloading and encoding images...")
|
154 |
-
encoded_images = download_and_encode_images(all_image_urls)
|
155 |
print(f"Number of encoded images: {len(encoded_images)}")
|
156 |
|
157 |
if not encoded_images:
|
|
|
95 |
|
96 |
return []
|
97 |
|
98 |
+
def try_scaled_image_url(client: httpx.Client, url: str, marketplace: str, max_retries: int = 3) -> str:
|
99 |
+
"""Try to get a scaled version of the image URL, fall back to original if not available."""
|
100 |
+
if marketplace == 'lamoda':
|
101 |
+
scaled_url = url.replace('product', 'img600x866')
|
102 |
+
for attempt in range(max_retries):
|
103 |
+
try:
|
104 |
+
response = client.get(scaled_url, timeout=5.0)
|
105 |
+
if response.status_code == 200:
|
106 |
+
print(f"Using scaled image: {scaled_url}")
|
107 |
+
return scaled_url
|
108 |
+
else:
|
109 |
+
print(f"Scaled image not available (status {response.status_code}), using original: {url}")
|
110 |
+
return url
|
111 |
+
except httpx.TimeoutException:
|
112 |
+
print(f"Timeout checking scaled image (attempt {attempt + 1}/{max_retries})")
|
113 |
+
if attempt == max_retries - 1:
|
114 |
+
print(f"Max retries reached, using original: {url}")
|
115 |
+
return url
|
116 |
+
except Exception as e:
|
117 |
+
print(f"Error checking scaled image: {type(e).__name__}: {str(e)}")
|
118 |
+
return url
|
119 |
+
return url
|
120 |
+
|
121 |
+
def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[Dict]:
|
122 |
"""Download images and convert them to base64 format for Gemini."""
|
123 |
encoded_images = []
|
124 |
+
timeout = httpx.Timeout(10.0, connect=5.0)
|
125 |
+
with httpx.Client(timeout=timeout) as client:
|
126 |
for url in image_urls:
|
127 |
+
max_retries = 3
|
128 |
+
for attempt in range(max_retries):
|
129 |
+
try:
|
130 |
+
# Try to get scaled version if available
|
131 |
+
final_url = try_scaled_image_url(client, url, marketplace)
|
132 |
+
response = client.get(final_url)
|
133 |
+
response.raise_for_status()
|
134 |
+
encoded_image = base64.b64encode(response.content).decode('utf-8')
|
135 |
+
encoded_images.append({
|
136 |
+
'mime_type': 'image/jpeg', # Assuming JPEG format
|
137 |
+
'data': encoded_image
|
138 |
+
})
|
139 |
+
break # Success, exit retry loop
|
140 |
+
except httpx.TimeoutException:
|
141 |
+
print(f"Timeout downloading image (attempt {attempt + 1}/{max_retries}): {url}")
|
142 |
+
if attempt == max_retries - 1:
|
143 |
+
print(f"Max retries reached, skipping image: {url}")
|
144 |
+
except Exception as e:
|
145 |
+
print(f"Error downloading image: {type(e).__name__}: {str(e)}")
|
146 |
+
if attempt == max_retries - 1:
|
147 |
+
print(f"Max retries reached, skipping image: {url}")
|
148 |
return encoded_images
|
149 |
|
150 |
def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str) -> str:
|
|
|
186 |
print(f"\nAll image URLs: {all_image_urls}")
|
187 |
|
188 |
print("\nDownloading and encoding images...")
|
189 |
+
encoded_images = download_and_encode_images(all_image_urls, marketplace)
|
190 |
print(f"Number of encoded images: {len(encoded_images)}")
|
191 |
|
192 |
if not encoded_images:
|
test.py
CHANGED
@@ -94,21 +94,56 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
|
|
94 |
|
95 |
return []
|
96 |
|
97 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
"""Download images and convert them to base64 format for Gemini."""
|
99 |
encoded_images = []
|
100 |
-
|
|
|
101 |
for url in image_urls:
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
return encoded_images
|
113 |
|
114 |
def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str) -> str:
|
@@ -150,7 +185,7 @@ def process_input(id_product_money: str, prompt: str) -> Tuple[List[str], str, s
|
|
150 |
print(f"\nAll image URLs: {all_image_urls}")
|
151 |
|
152 |
print("\nDownloading and encoding images...")
|
153 |
-
encoded_images = download_and_encode_images(all_image_urls)
|
154 |
print(f"Number of encoded images: {len(encoded_images)}")
|
155 |
|
156 |
if not encoded_images:
|
|
|
94 |
|
95 |
return []
|
96 |
|
97 |
+
def try_scaled_image_url(client: httpx.Client, url: str, marketplace: str, max_retries: int = 3) -> str:
|
98 |
+
"""Try to get a scaled version of the image URL, fall back to original if not available."""
|
99 |
+
if marketplace == 'lamoda':
|
100 |
+
scaled_url = url.replace('product', 'img600x866')
|
101 |
+
for attempt in range(max_retries):
|
102 |
+
try:
|
103 |
+
response = client.get(scaled_url, timeout=5.0)
|
104 |
+
if response.status_code == 200:
|
105 |
+
print(f"Using scaled image: {scaled_url}")
|
106 |
+
return scaled_url
|
107 |
+
else:
|
108 |
+
print(f"Scaled image not available (status {response.status_code}), using original: {url}")
|
109 |
+
return url
|
110 |
+
except httpx.TimeoutException:
|
111 |
+
print(f"Timeout checking scaled image (attempt {attempt + 1}/{max_retries})")
|
112 |
+
if attempt == max_retries - 1:
|
113 |
+
print(f"Max retries reached, using original: {url}")
|
114 |
+
return url
|
115 |
+
except Exception as e:
|
116 |
+
print(f"Error checking scaled image: {type(e).__name__}: {str(e)}")
|
117 |
+
return url
|
118 |
+
return url
|
119 |
+
|
120 |
+
def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[Dict]:
|
121 |
"""Download images and convert them to base64 format for Gemini."""
|
122 |
encoded_images = []
|
123 |
+
timeout = httpx.Timeout(10.0, connect=5.0)
|
124 |
+
with httpx.Client(timeout=timeout) as client:
|
125 |
for url in image_urls:
|
126 |
+
max_retries = 3
|
127 |
+
for attempt in range(max_retries):
|
128 |
+
try:
|
129 |
+
# Try to get scaled version if available
|
130 |
+
final_url = try_scaled_image_url(client, url, marketplace)
|
131 |
+
response = client.get(final_url)
|
132 |
+
response.raise_for_status()
|
133 |
+
encoded_image = base64.b64encode(response.content).decode('utf-8')
|
134 |
+
encoded_images.append({
|
135 |
+
'mime_type': 'image/jpeg', # Assuming JPEG format
|
136 |
+
'data': encoded_image
|
137 |
+
})
|
138 |
+
break # Success, exit retry loop
|
139 |
+
except httpx.TimeoutException:
|
140 |
+
print(f"Timeout downloading image (attempt {attempt + 1}/{max_retries}): {url}")
|
141 |
+
if attempt == max_retries - 1:
|
142 |
+
print(f"Max retries reached, skipping image: {url}")
|
143 |
+
except Exception as e:
|
144 |
+
print(f"Error downloading image: {type(e).__name__}: {str(e)}")
|
145 |
+
if attempt == max_retries - 1:
|
146 |
+
print(f"Max retries reached, skipping image: {url}")
|
147 |
return encoded_images
|
148 |
|
149 |
def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str) -> str:
|
|
|
185 |
print(f"\nAll image URLs: {all_image_urls}")
|
186 |
|
187 |
print("\nDownloading and encoding images...")
|
188 |
+
encoded_images = download_and_encode_images(all_image_urls, marketplace)
|
189 |
print(f"Number of encoded images: {len(encoded_images)}")
|
190 |
|
191 |
if not encoded_images:
|