Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -241,17 +241,21 @@ def search(search_query):
|
|
| 241 |
|
| 242 |
# Iterate over the top 5 results
|
| 243 |
for id in best_photo_ids:
|
| 244 |
-
print(id)
|
| 245 |
id, _ = id.split('.')
|
| 246 |
url = id2url.get(id, "")
|
| 247 |
-
print(url)
|
| 248 |
if url == "": continue
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
#credits = f'Photo by <a href="https://unsplash.com/@{photo["photographer_username"]}?utm_source=NaturalLanguageImageSearch&utm_medium=referral">{photo["photographer_first_name"]} {photo["photographer_last_name"]}</a> on <a href="https://unsplash.com/?utm_source=NaturalLanguageImageSearch&utm_medium=referral">Unsplash</a>'
|
| 254 |
-
imgs.append(img)
|
| 255 |
#display(HTML(f'Photo by <a href="https://unsplash.com/@{photo["photographer_username"]}?utm_source=NaturalLanguageImageSearch&utm_medium=referral">{photo["photographer_first_name"]} {photo["photographer_last_name"]}</a> on <a href="https://unsplash.com/?utm_source=NaturalLanguageImageSearch&utm_medium=referral">Unsplash</a>'))
|
| 256 |
|
| 257 |
if len(imgs) == 5: break
|
|
|
|
| 241 |
|
| 242 |
# Iterate over the top 5 results
|
| 243 |
for id in best_photo_ids:
|
|
|
|
| 244 |
id, _ = id.split('.')
|
| 245 |
url = id2url.get(id, "")
|
|
|
|
| 246 |
if url == "": continue
|
| 247 |
+
try:
|
| 248 |
+
r = requests.get(url + "?w=512", stream=True, timeout=10)
|
| 249 |
+
r.raise_for_status() # Raise exception for bad status codes
|
| 250 |
+
r.raw.decode_content = True # Handle compressed responses
|
| 251 |
+
img = Image.open(r.raw)
|
| 252 |
+
imgs.append(img.copy()) # Copy before closing stream
|
| 253 |
+
r.close() # Clean up connection
|
| 254 |
+
except Exception as e:
|
| 255 |
+
print(f"Failed to load {url}: {e}")
|
| 256 |
+
continue
|
| 257 |
+
|
| 258 |
#credits = f'Photo by <a href="https://unsplash.com/@{photo["photographer_username"]}?utm_source=NaturalLanguageImageSearch&utm_medium=referral">{photo["photographer_first_name"]} {photo["photographer_last_name"]}</a> on <a href="https://unsplash.com/?utm_source=NaturalLanguageImageSearch&utm_medium=referral">Unsplash</a>'
|
|
|
|
| 259 |
#display(HTML(f'Photo by <a href="https://unsplash.com/@{photo["photographer_username"]}?utm_source=NaturalLanguageImageSearch&utm_medium=referral">{photo["photographer_first_name"]} {photo["photographer_last_name"]}</a> on <a href="https://unsplash.com/?utm_source=NaturalLanguageImageSearch&utm_medium=referral">Unsplash</a>'))
|
| 260 |
|
| 261 |
if len(imgs) == 5: break
|