Add image embedding task and test case

This commit is contained in:
liyaxing
2023-09-27 17:24:38 +08:00
parent e13e31aae9
commit 87f82ec9e2
4 changed files with 32 additions and 4 deletions
+14 -1
View File
@@ -2,6 +2,8 @@
from enum import Enum from enum import Enum
import uuid import uuid
import time import time
from typing import Union
from knowledge import ObjectID
class ComputeTaskResultCode(Enum): class ComputeTaskResultCode(Enum):
OK = 0 OK = 0
@@ -61,7 +63,7 @@ class ComputeTask:
if inner_functions is not None: if inner_functions is not None:
self.params["inner_functions"] = inner_functions self.params["inner_functions"] = inner_functions
def set_text_embedding_params(self, input, model_name=None, callchain_id = None): def set_text_embedding_params(self, input: str, model_name=None, callchain_id = None):
self.task_type = ComputeTaskType.TEXT_EMBEDDING self.task_type = ComputeTaskType.TEXT_EMBEDDING
self.create_time = time.time() self.create_time = time.time()
self.task_id = uuid.uuid4().hex self.task_id = uuid.uuid4().hex
@@ -71,6 +73,17 @@ class ComputeTask:
else: else:
self.params["model_name"] = "text-embedding-ada-002" self.params["model_name"] = "text-embedding-ada-002"
self.params["input"] = input self.params["input"] = input
def set_image_embedding_params(self, input = Union[ObjectID, bytes], model_name=None, callchain_id = None):
self.task_type = ComputeTaskType.IMAGE_EMBEDDING
self.create_time = time.time()
self.task_id = uuid.uuid4().hex
self.callchain_id = callchain_id
if model_name is not None:
self.params["model_name"] = model_name
else:
self.params["model_name"] = None
self.params["input"] = input
def set_text_2_image_params(self, prompt: str, model_name, callchain_id=None): def set_text_2_image_params(self, prompt: str, model_name, callchain_id=None):
self.task_type = ComputeTaskType.TEXT_2_IMAGE self.task_type = ComputeTaskType.TEXT_2_IMAGE
+1
View File
@@ -137,3 +137,4 @@ python-telegram-bot
pydub pydub
stability_sdk stability_sdk
sentence-transformers==2.2.2 sentence-transformers==2.2.2
tiktoken
+4 -1
View File
@@ -67,7 +67,10 @@ def test_st():
] ]
# Compute embeddings # Compute embeddings
embeddings = model.encode(sentences, convert_to_tensor=True) #embeddings = model.encode(sentences, convert_to_tensor=True)
embeddings = model.encode(sentences)
print("embeddings as follows: ")
print(embeddings)
# Compute cosine-similarities for each sentence with each other sentence # Compute cosine-similarities for each sentence with each other sentence
cosine_scores = util.cos_sim(embeddings, embeddings) cosine_scores = util.cos_sim(embeddings, embeddings)
+13 -2
View File
@@ -26,12 +26,13 @@ from knowledge import (
EmailObject, EmailObject,
ImageObject, ImageObject,
) )
from aios_kernel import LocalSentenceTransformer_Image_ComputeNode, ComputeTask
import asyncio import asyncio
import unittest import unittest
class TestVectorSTorage(unittest.TestCase): class TestVectorSTorage(unittest.IsolatedAsyncioTestCase):
def test_object(self): async def test_object(self):
data = HashValue.hash_data("1233".encode("utf-8")) data = HashValue.hash_data("1233".encode("utf-8"))
print(data.to_base58()) print(data.to_base58())
print(data.to_base36()) print(data.to_base36())
@@ -65,6 +66,15 @@ class TestVectorSTorage(unittest.TestCase):
image_id = images[image_keys[1]] image_id = images[image_keys[1]]
print(f"got image object: {image_keys[1]} {image_id.to_base58()}") print(f"got image object: {image_keys[1]} {image_id.to_base58()}")
node = LocalSentenceTransformer_Image_ComputeNode();
ret = node.initial()
self.assertEqual(ret, True)
task = ComputeTask()
task.set_image_embedding_params(image_id)
ret = await node.execute_task(task)
print(ret)
'''
buf = KnowledgeStore().get_object_store().get_object(image_id) buf = KnowledgeStore().get_object_store().get_object(image_id)
image_obj= ImageObject.decode(buf) image_obj= ImageObject.decode(buf)
file_size = image_obj.get_file_size() file_size = image_obj.get_file_size()
@@ -83,6 +93,7 @@ class TestVectorSTorage(unittest.TestCase):
#model = SentenceTransformer('clip-ViT-B-32-multilingual-v1') #model = SentenceTransformer('clip-ViT-B-32-multilingual-v1')
model = SentenceTransformer('clip-ViT-B-32') model = SentenceTransformer('clip-ViT-B-32')
model.encode(image, convert_to_tensor=True) model.encode(image, convert_to_tensor=True)
'''
def test_relation(self): def test_relation(self):
obj1 = ObjectID.hash_data("12345".encode("utf-8")) obj1 = ObjectID.hash_data("12345".encode("utf-8"))