Spaces:
Running
Running
Update server_medical_RAM_optimize.py
Browse files
server_medical_RAM_optimize.py
CHANGED
|
@@ -46,6 +46,7 @@ class ImageSearchEngine:
|
|
| 46 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 47 |
print("Loading BiomedCLIP-PubMedBERT_256-vit_base_patch16_224...")
|
| 48 |
self.model, preprocess_train, self.preprocess = open_clip.create_model_and_transforms('hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224')
|
|
|
|
| 49 |
|
| 50 |
if not os.path.exists(h5_file_path):
|
| 51 |
# Tạo file giả nếu không có để server không crash ngay (giúp debug)
|
|
@@ -63,7 +64,7 @@ class ImageSearchEngine:
|
|
| 63 |
if os.path.exists(BIN_FILE_PATH):
|
| 64 |
print(f"⚡ Loading Index from {BIN_FILE_PATH}...")
|
| 65 |
self.index.load_index(BIN_FILE_PATH, max_elements=self.max_elements)
|
| 66 |
-
self.index.set_ef(
|
| 67 |
else:
|
| 68 |
print("⚠️ BIN file not found.")
|
| 69 |
|
|
@@ -71,19 +72,19 @@ class ImageSearchEngine:
|
|
| 71 |
if isinstance(text, str):
|
| 72 |
text = [text]
|
| 73 |
|
| 74 |
-
tokens = tokenizer(text).to(device)
|
| 75 |
|
| 76 |
with torch.no_grad():
|
| 77 |
-
text_features = model.encode_text(tokens)
|
| 78 |
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
| 79 |
|
| 80 |
return text_features.cpu().numpy()
|
| 81 |
|
| 82 |
def image_to_vector(self, image):
|
| 83 |
-
image_tensor = preprocess(image).unsqueeze(0).to(device)
|
| 84 |
|
| 85 |
with torch.no_grad():
|
| 86 |
-
image_features = model.encode_image(image_tensor)
|
| 87 |
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
| 88 |
|
| 89 |
return image_features.cpu().numpy().astype(np.float32)[0]
|
|
|
|
| 46 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 47 |
print("Loading BiomedCLIP-PubMedBERT_256-vit_base_patch16_224...")
|
| 48 |
self.model, preprocess_train, self.preprocess = open_clip.create_model_and_transforms('hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224')
|
| 49 |
+
self.tokenizer = open_clip.get_tokenizer('hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224')
|
| 50 |
|
| 51 |
if not os.path.exists(h5_file_path):
|
| 52 |
# Tạo file giả nếu không có để server không crash ngay (giúp debug)
|
|
|
|
| 64 |
if os.path.exists(BIN_FILE_PATH):
|
| 65 |
print(f"⚡ Loading Index from {BIN_FILE_PATH}...")
|
| 66 |
self.index.load_index(BIN_FILE_PATH, max_elements=self.max_elements)
|
| 67 |
+
self.index.set_ef(400)
|
| 68 |
else:
|
| 69 |
print("⚠️ BIN file not found.")
|
| 70 |
|
|
|
|
| 72 |
if isinstance(text, str):
|
| 73 |
text = [text]
|
| 74 |
|
| 75 |
+
tokens = self.tokenizer(text).to(device)
|
| 76 |
|
| 77 |
with torch.no_grad():
|
| 78 |
+
text_features = self.model.encode_text(tokens)
|
| 79 |
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
| 80 |
|
| 81 |
return text_features.cpu().numpy()
|
| 82 |
|
| 83 |
def image_to_vector(self, image):
|
| 84 |
+
image_tensor = self.preprocess(image).unsqueeze(0).to(device)
|
| 85 |
|
| 86 |
with torch.no_grad():
|
| 87 |
+
image_features = self.model.encode_image(image_tensor)
|
| 88 |
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
| 89 |
|
| 90 |
return image_features.cpu().numpy().astype(np.float32)[0]
|