Spaces:
Configuration error
Configuration error
oremaz commited on
Commit ·
1a3b775
1
Parent(s): 6aad47c
Update agent.py
Browse files199 unmodified lines2002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622634 unmodified lines268269270271272273274199 unmodified linesfrom typing import Any, List, Optionalfrom llama_index.core.embeddings import BaseEmbeddingimport torchfrom FlagEmbedding.visual.modeling import Visualized_BGEclass BAAIVisualizedAdvanced(BaseEmbedding):"""Advanced implementation using FlagEmbedding's Visualized_BGE."""def __init__(self,model_name_bge: str = "BAAI/bge-base-en-v1.5",model_weight_path: str = "path/to/Visualized_base_en_v1.5.pth",**kwargs: Any) -> None:super().__init__(**kwargs)# Initialize the Visualized BGE modelself._model = Visualized_BGE(model_name_bge=model_name_bge,model_weight=model_weight_path)self._model.eval()@classmethoddef class_name(cls) -> str:return "baai_visualized_advanced"def _get_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]:"""Generate embedding for query with optional image."""with torch.no_grad():if image_path:# Encode both text and imageembedding = self._model.encode(image=image_path, text=query)else:# Text-only encodingembedding = self._model.encode(text=query)return embedding.cpu().numpy().tolist()def _get_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]:"""Generate embedding for text with optional image."""with torch.no_grad():if image_path:# Image-only encodingembedding = self._model.encode(image=image_path)else:# Text-only encodingembedding = self._model.encode(text=text)return embedding.cpu().numpy().tolist()def _get_text_embeddings(self, texts: List[str], image_paths: Optional[List[str]] = None) -> List[List[float]]:"""Batch embedding generation."""embeddings = []image_paths = image_paths or [None] * len(texts)with torch.no_grad():for text, img_path in zip(texts, image_paths):if img_path:emb = self._model.encode(image=img_path, text=text)else:emb = self._model.encode(text=text)embeddings.append(emb.cpu().numpy().tolist())return embeddings4 unmodified linesreturn self._get_text_embedding(text, image_path)embed_model = BAAIVisualizedEmbedding()# Code LLMcode_llm = HuggingFaceLLM(model_name="Qwen/Qwen2.5-Coder-3B-Instruct",199 unmodified lines2002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492504 unmodified lines255256257258259260261199 unmodified linesfrom typing import Any, List, Optionalfrom llama_index.core.embeddings import BaseEmbeddingfrom sentence_transformers import SentenceTransformerfrom PIL import Imageclass MultimodalCLIPEmbedding(BaseEmbedding):"""Custom embedding class using CLIP for multimodal capabilities."""def __init__(self, model_name: str = "clip-ViT-B-32", **kwargs: Any) -> None:super().__init__(**kwargs)self._model = SentenceTransformer(model_name)@classmethoddef class_name(cls) -> str:return "multimodal_clip"def _get_query_embedding(self, query: str, image_path: Optional[str] = None) -> List[float]:if image_path:image = Image.open(image_path)embedding = self._model.encode(image)return embedding.tolist()else:embedding = self._model.encode(query)return embedding.tolist()def _get_text_embedding(self, text: str, image_path: Optional[str] = None) -> List[float]:if image_path:image = Image.open(image_path)embedding = self._model.encode(image)return embedding.tolist()else:embedding = self._model.encode(text)return embedding.tolist()def _get_text_embeddings(self, texts: List[str], image_paths: Optional[List[str]] = None) -> List[List[float]]:embeddings = []image_paths = image_paths or [None] * len(texts)for text, img_path in zip(texts, image_paths):if img_path:image = Image.open(img_path)emb = self._model.encode(image)else:emb = self._model.encode(text)embeddings.append(emb.tolist())return embeddings4 unmodified linesreturn self._get_text_embedding(text, image_path)embed_model = MultimodalCLIPEmbedding()# Code LLMcode_llm = HuggingFaceLLM(model_name="Qwen/Qwen2.5-Coder-3B-Instruct",
agent.py+29-42