-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from theroyallab/vision
Vision
- Loading branch information
Showing
13 changed files
with
321 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Vision utilities for ExLlamaV2.""" | ||
|
||
import io | ||
import base64 | ||
import re | ||
from PIL import Image | ||
from common import model | ||
import aiohttp | ||
from common.networking import ( | ||
handle_request_error, | ||
) | ||
from common.tabby_config import config | ||
from fastapi import HTTPException | ||
from exllamav2.generator import ExLlamaV2MMEmbedding | ||
from async_lru import alru_cache | ||
|
||
|
||
async def get_image(url: str) -> Image: | ||
if url.startswith("data:image"): | ||
# Handle base64 image | ||
match = re.match(r"^data:image\/[a-zA-Z0-9]+;base64,(.*)$", url) | ||
if match: | ||
base64_image = match.group(1) | ||
bytes_image = base64.b64decode(base64_image) | ||
else: | ||
error_message = handle_request_error( | ||
"Failed to read base64 image input.", | ||
exc_info=False, | ||
).error.message | ||
|
||
raise HTTPException(400, error_message) | ||
|
||
else: | ||
# Handle image URL | ||
if config.network.disable_fetch_requests: | ||
error_message = handle_request_error( | ||
f"Failed to fetch image from {url} as fetch requests are disabled.", | ||
exc_info=False, | ||
).error.message | ||
|
||
raise HTTPException(400, error_message) | ||
|
||
async with aiohttp.ClientSession() as session: | ||
async with session.get(url) as response: | ||
if response.status == 200: | ||
bytes_image = await response.read() | ||
else: | ||
error_message = handle_request_error( | ||
f"Failed to fetch image from {url}.", | ||
exc_info=False, | ||
).error.message | ||
|
||
raise HTTPException(400, error_message) | ||
|
||
return Image.open(io.BytesIO(bytes_image)) | ||
|
||
|
||
@alru_cache(20) | ||
async def get_image_embedding(url: str) -> ExLlamaV2MMEmbedding: | ||
image = await get_image(url) | ||
return model.container.vision_model.get_image_embeddings( | ||
model=model.container.model, | ||
tokenizer=model.container.tokenizer, | ||
image=image, | ||
text_alias=None, | ||
) | ||
|
||
|
||
def clear_image_embedding_cache(): | ||
get_image_embedding.cache_clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import List | ||
from backends.exllamav2.vision import get_image_embedding | ||
from common import model | ||
from loguru import logger | ||
|
||
from common.optional_dependencies import dependencies | ||
|
||
if dependencies.exllamav2: | ||
from exllamav2 import ExLlamaV2VisionTower | ||
|
||
|
||
class MultimodalEmbeddingWrapper: | ||
"""Common multimodal embedding wrapper""" | ||
|
||
type: str = None | ||
content: List = [] | ||
text_alias: List[str] = [] | ||
|
||
async def add(self, url: str): | ||
# Determine the type of vision embedding to use | ||
if not self.type: | ||
if isinstance(model.container.vision_model, ExLlamaV2VisionTower): | ||
self.type = "ExLlamaV2MMEmbedding" | ||
|
||
if self.type == "ExLlamaV2MMEmbedding": | ||
embedding = await get_image_embedding(url) | ||
self.content.append(embedding) | ||
self.text_alias.append(embedding.text_alias) | ||
else: | ||
logger.error("No valid vision model to create embedding") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.