MCPcopy Create free account
hub / github.com/Yuan-Li-FNLP/R3-RAG / encode_text

Method encode_text

tool/FlashRAG/flashrag/retriever/encoder.py:224–245  ·  view source on GitHub ↗
(self, text_list: List[str])

Source from the content-addressed store, hash-verified

222
223 @torch.inference_mode()
224 def encode_text(self, text_list: List[str]) -> np.ndarray:
225 # Each item in image_list: PIL Image, local path, or URL
226 if self.model_type == "CLIPModel" or self.model_type == 'ChineseCLIPModel':
227 inputs = self.processor(
228 text=text_list,
229 padding=True,
230 truncation=True,
231 return_tensors="pt",
232 )
233 inputs = {k: v.cuda() for k, v in inputs.items()}
234 text_emb = self.model.get_text_features(**inputs)
235 text_emb = text_emb / text_emb.norm(p=2, dim=-1, keepdim=True)
236 text_emb = text_emb.detach().cpu().numpy().astype(np.float32)
237 elif self.model_type.endswith("CLIPModel"):
238 text_emb = self.model.encode_text(
239 text_list,
240 padding=True,
241 truncation=True,
242 )
243 else:
244 raise NotImplementedError(f"Unsupported model type: {self.model_type}")
245 return text_emb

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected