Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -160,15 +160,25 @@ class ExtendedQuantumDocumentManager(QuantumDocumentManager):
|
|
160 |
"""Extended with multi-modal document handling."""
|
161 |
def create_image_collection(self, image_paths: List[str]):
|
162 |
embeddings = []
|
|
|
163 |
for img_path in image_paths:
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
return Chroma.from_embeddings(
|
170 |
embeddings=embeddings,
|
171 |
-
documents=
|
172 |
collection_name="neuro_images"
|
173 |
)
|
174 |
|
@@ -793,32 +803,6 @@ clip_model = CLIPModel.from_pretrained(ResearchConfig.CLIP_SETTINGS["model"])
|
|
793 |
clip_processor = CLIPProcessor.from_pretrained(ResearchConfig.CLIP_SETTINGS["model"])
|
794 |
multi_retriever = MultiModalRetriever(retriever.research_retriever, clip_model, clip_processor)
|
795 |
|
796 |
-
# ------------------------------
|
797 |
-
# Updated Document Processing for Multi-Modal Documents
|
798 |
-
# ------------------------------
|
799 |
-
class ExtendedQuantumDocumentManager(ExtendedQuantumDocumentManager):
|
800 |
-
"""Extended with multi-modal document handling."""
|
801 |
-
def create_image_collection(self, image_paths: List[str]):
|
802 |
-
embeddings = []
|
803 |
-
for img_path in image_paths:
|
804 |
-
image = Image.open(img_path)
|
805 |
-
inputs = clip_processor(images=image, return_tensors="pt")
|
806 |
-
with torch.no_grad():
|
807 |
-
emb = clip_model.get_image_features(**inputs)
|
808 |
-
embeddings.append(emb.numpy())
|
809 |
-
return Chroma.from_embeddings(
|
810 |
-
embeddings=embeddings,
|
811 |
-
documents=image_paths,
|
812 |
-
collection_name="neuro_images"
|
813 |
-
)
|
814 |
-
|
815 |
-
# Initialize image collection using the extended manager
|
816 |
-
qdm = ExtendedQuantumDocumentManager()
|
817 |
-
qdm.create_image_collection([
|
818 |
-
"data/images/quantum_computing.png",
|
819 |
-
"data/images/neural_arch.png"
|
820 |
-
])
|
821 |
-
|
822 |
# ------------------------------
|
823 |
# Execute the Application
|
824 |
# ------------------------------
|
|
|
160 |
"""Extended with multi-modal document handling."""
|
161 |
def create_image_collection(self, image_paths: List[str]):
|
162 |
embeddings = []
|
163 |
+
valid_images = []
|
164 |
for img_path in image_paths:
|
165 |
+
try:
|
166 |
+
image = Image.open(img_path)
|
167 |
+
inputs = clip_processor(images=image, return_tensors="pt")
|
168 |
+
with torch.no_grad():
|
169 |
+
emb = clip_model.get_image_features(**inputs)
|
170 |
+
embeddings.append(emb.numpy())
|
171 |
+
valid_images.append(img_path)
|
172 |
+
except FileNotFoundError as fe:
|
173 |
+
logger.warning(f"Image file not found: {img_path}. Skipping this file.")
|
174 |
+
except Exception as e:
|
175 |
+
logger.exception(f"Error processing image {img_path}: {str(e)}")
|
176 |
+
if not embeddings:
|
177 |
+
logger.error("No valid images found for image collection.")
|
178 |
+
return None
|
179 |
return Chroma.from_embeddings(
|
180 |
embeddings=embeddings,
|
181 |
+
documents=valid_images,
|
182 |
collection_name="neuro_images"
|
183 |
)
|
184 |
|
|
|
803 |
clip_processor = CLIPProcessor.from_pretrained(ResearchConfig.CLIP_SETTINGS["model"])
|
804 |
multi_retriever = MultiModalRetriever(retriever.research_retriever, clip_model, clip_processor)
|
805 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
806 |
# ------------------------------
|
807 |
# Execute the Application
|
808 |
# ------------------------------
|