Repository navigation
Expand file tree
/
Copy pathbase.py
More file actions
163 lines (129 loc) · 3.58 KB
/
Copy pathbase.py
File metadata and controls
163 lines (129 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
from typing import Union, Dict, List, Optional, Any
from enum import IntEnum
import numpy as np
from modelcache.utils import import_sql_client
from modelcache.utils.error import NotFoundError
class DataType(IntEnum):
STR = 0
IMAGE_BASE64 = 1
IMAGE_URL = 2
@dataclass
class QuestionDep:
"""
QuestionDep
"""
name: str
data: str
dep_type: int = DataType.STR
@classmethod
def from_dict(cls, d: Dict):
return cls(
name=d["name"],
data=d["data"],
dep_type=d["dep_type"]
)
@dataclass
class Question:
"""
Question
"""
content: str
deps: Optional[List[QuestionDep]] = None
@classmethod
def from_dict(cls, d: Dict):
deps = []
for dep in d["deps"]:
deps.append(QuestionDep.from_dict(dep))
return cls(d["content"], deps)
@dataclass
class Answer:
"""
data_type:
0: str
1: base64 image
"""
answer: Any
answer_type: int = DataType.STR
@dataclass
class CacheData:
"""
CacheData
"""
question: Union[str, Question]
answers: List[Answer]
embedding_data: Optional[np.ndarray] = None
def __init__(self, question, answers, embedding_data=None):
self.question = question
self.answers = []
if isinstance(answers, (str, Answer)):
answers = [answers]
for data in answers:
if isinstance(data, (list, tuple)):
self.answers.append(Answer(*data))
elif isinstance(data, Answer):
self.answers.append(data)
else:
self.answers.append(Answer(answer=data))
self.embedding_data = embedding_data
class CacheStorage(metaclass=ABCMeta):
"""
BaseStorage for scalar data.
"""
@abstractmethod
def create(self):
pass
@abstractmethod
def insert_query_resp(self, query_resp, **kwargs):
pass
@abstractmethod
def get_data_by_id(self, key):
pass
@abstractmethod
def mark_deleted(self, keys):
pass
@abstractmethod
def model_deleted(self, model):
pass
@abstractmethod
def clear_deleted_data(self):
pass
@abstractmethod
def get_ids(self, deleted=True):
pass
@abstractmethod
def count(self):
pass
@abstractmethod
def flush(self):
pass
@abstractmethod
def close(self):
pass
@abstractmethod
def batch_insert(self, all_data: List[CacheData]):
pass
@abstractmethod
def update_hit_count_by_id(self, primary_id):
pass
@staticmethod
def get(name, **kwargs):
if name in ["mysql", "oceanbase"]:
from modelcache.manager.scalar_data.sql_storage import SQLStorage
config = kwargs.get("config")
import_sql_client(name)
cache_base = SQLStorage(db_type=name, config=config)
elif name == 'sqlite':
SQL_URL = {"sqlite": "./sqlite.db"}
from modelcache.manager.scalar_data.sql_storage_sqlite import SQLStorage
sql_url = kwargs.get("sql_url", SQL_URL[name])
cache_base = SQLStorage(db_type=name, url=sql_url)
elif name == 'elasticsearch':
from modelcache.manager.scalar_data.sql_storage_es import SQLStorage
config = kwargs.get("config")
cache_base = SQLStorage(db_type=name, config=config)
else:
raise NotFoundError("cache store", name)
return cache_base