Commit
·
bf490e1
1
Parent(s):
f95c642
Only display those images, which were actually sent to the VLMs
Browse files
app.py
CHANGED
@@ -126,9 +126,10 @@ def try_scaled_image_url(client: httpx.Client, url: str, marketplace: str, max_r
|
|
126 |
|
127 |
return url
|
128 |
|
129 |
-
def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[Dict]:
|
130 |
"""Download images and convert them to base64 format for Gemini."""
|
131 |
encoded_images = []
|
|
|
132 |
timeout = httpx.Timeout(10.0, connect=5.0)
|
133 |
with httpx.Client(timeout=timeout) as client:
|
134 |
for url in image_urls:
|
@@ -144,6 +145,7 @@ def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[
|
|
144 |
'mime_type': 'image/jpeg', # Assuming JPEG format
|
145 |
'data': encoded_image
|
146 |
})
|
|
|
147 |
break # Success, exit retry loop
|
148 |
except httpx.TimeoutException:
|
149 |
print(f"Timeout downloading image (attempt {attempt + 1}/{max_retries}): {url}")
|
@@ -153,7 +155,7 @@ def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[
|
|
153 |
print(f"Error downloading image: {type(e).__name__}: {str(e)}")
|
154 |
if attempt == max_retries - 1:
|
155 |
print(f"Max retries reached, skipping image: {url}")
|
156 |
-
return encoded_images
|
157 |
|
158 |
def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str) -> str:
|
159 |
"""Get response from a Gemini model."""
|
@@ -197,12 +199,15 @@ def process_input(id_product_money: str, prompt: str, progress=gr.Progress()) ->
|
|
197 |
|
198 |
status_msg = "Downloading and processing images..."
|
199 |
progress(0.4, desc=status_msg)
|
200 |
-
encoded_images = download_and_encode_images(all_image_urls, marketplace)
|
201 |
print(f"Number of encoded images: {len(encoded_images)}")
|
202 |
|
203 |
if not encoded_images:
|
204 |
raise ValueError("No images could be downloaded")
|
205 |
|
|
|
|
|
|
|
206 |
status_msg = "Getting response from Gemini 1.5 Flash..."
|
207 |
progress(0.6, desc=status_msg)
|
208 |
gemini_1_5_response = get_gemini_response("gemini-1.5-flash", encoded_images, prompt)
|
|
|
126 |
|
127 |
return url
|
128 |
|
129 |
+
def download_and_encode_images(image_urls: List[str], marketplace: str) -> Tuple[List[Dict], List[str]]:
|
130 |
"""Download images and convert them to base64 format for Gemini."""
|
131 |
encoded_images = []
|
132 |
+
successful_urls = [] # Track URLs that were successfully downloaded
|
133 |
timeout = httpx.Timeout(10.0, connect=5.0)
|
134 |
with httpx.Client(timeout=timeout) as client:
|
135 |
for url in image_urls:
|
|
|
145 |
'mime_type': 'image/jpeg', # Assuming JPEG format
|
146 |
'data': encoded_image
|
147 |
})
|
148 |
+
successful_urls.append(final_url) # Store the URL that worked (original or scaled)
|
149 |
break # Success, exit retry loop
|
150 |
except httpx.TimeoutException:
|
151 |
print(f"Timeout downloading image (attempt {attempt + 1}/{max_retries}): {url}")
|
|
|
155 |
print(f"Error downloading image: {type(e).__name__}: {str(e)}")
|
156 |
if attempt == max_retries - 1:
|
157 |
print(f"Max retries reached, skipping image: {url}")
|
158 |
+
return encoded_images, successful_urls
|
159 |
|
160 |
def get_gemini_response(model_name: str, encoded_images: List[Dict], prompt: str) -> str:
|
161 |
"""Get response from a Gemini model."""
|
|
|
199 |
|
200 |
status_msg = "Downloading and processing images..."
|
201 |
progress(0.4, desc=status_msg)
|
202 |
+
encoded_images, successful_urls = download_and_encode_images(all_image_urls, marketplace)
|
203 |
print(f"Number of encoded images: {len(encoded_images)}")
|
204 |
|
205 |
if not encoded_images:
|
206 |
raise ValueError("No images could be downloaded")
|
207 |
|
208 |
+
# Update all_image_urls to only include successfully downloaded URLs
|
209 |
+
all_image_urls = successful_urls
|
210 |
+
|
211 |
status_msg = "Getting response from Gemini 1.5 Flash..."
|
212 |
progress(0.6, desc=status_msg)
|
213 |
gemini_1_5_response = get_gemini_response("gemini-1.5-flash", encoded_images, prompt)
|