@@ -11,7 +11,7 @@ def format_url_list(urls):
1111
1212
1313def insert_images (
14- model_name , urls , metadatas , image_embeddings = None , replace_existing = True
14+ model_name , urls , metadatas , image_embeddings = None , replace_existing = True , fail_on_error = True
1515):
1616 existing_urls = []
1717 if not replace_existing :
@@ -24,30 +24,51 @@ def insert_images(
2424 ]
2525
2626 new_urls = [url for url in urls if url not in existing_urls ]
27- image_embeddings = (
28- [
29- embeddings [model_name ].get_image_embedding (load_image_from_url (url ))
30- for url in new_urls
31- ]
32- if image_embeddings is None
33- else [
34- embedding
35- for url , embedding in zip (urls , image_embeddings )
36- if url not in existing_urls
37- ]
38- )
39- metadatas = [
40- json .dumps (metadata )
41- for url , metadata in zip (urls , metadatas )
42- if url not in existing_urls
43- ]
44-
45- if len (new_urls ) > 0 :
46- collections [model_name ].insert ([new_urls , image_embeddings , metadatas ])
27+
28+ # Handle individual image failures
29+ successful_urls = []
30+ failed_urls = []
31+ computed_embeddings = []
32+ processed_metadatas = []
33+
34+ if image_embeddings is None :
35+ # Process each image individually
36+ for i , url in enumerate (new_urls ):
37+ try :
38+ embedding = embeddings [model_name ].get_image_embedding (load_image_from_url (url ))
39+ computed_embeddings .append (embedding )
40+ processed_metadatas .append (json .dumps (metadatas [urls .index (url )]))
41+ successful_urls .append (url )
42+ except Exception as e :
43+ if fail_on_error :
44+ # Original behavior: propagate the exception
45+ raise
46+ else :
47+ # New behavior: collect the error and continue
48+ failed_urls .append ({"url" : url , "error" : str (e )})
49+ else :
50+ # Use provided embeddings
51+ for url , embedding in zip (urls , image_embeddings ):
52+ if url not in existing_urls :
53+ try :
54+ computed_embeddings .append (embedding )
55+ processed_metadatas .append (json .dumps (metadatas [urls .index (url )]))
56+ successful_urls .append (url )
57+ except Exception as e :
58+ if fail_on_error :
59+ # Original behavior: propagate the exception
60+ raise
61+ else :
62+ # New behavior: collect the error and continue
63+ failed_urls .append ({"url" : url , "error" : str (e )})
64+
65+ if len (successful_urls ) > 0 :
66+ collections [model_name ].insert ([successful_urls , computed_embeddings , processed_metadatas ])
4767
4868 return {
49- "added" : new_urls ,
69+ "added" : successful_urls ,
5070 "found" : existing_urls ,
71+ "failed" : failed_urls ,
5172 }
5273
5374
0 commit comments