-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclip_serve_models.py
165 lines (145 loc) · 4.01 KB
/
clip_serve_models.py
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
163
164
165
from pydantic import BaseModel, Field, ConfigDict
from typing import List, Union
"""
Request models
"""
class TextRequest(BaseModel):
"""
TextRequest model, used to send text to the API
and get the embeddings.
Example:
```json
{
"text": "a photo of a cat"
}
```
"""
text: Union[str, List[str]] = Field(default_factory=list)
def to_json(self):
return self.model_dump_json()
class ImageRequest(BaseModel):
"""
ImageRequest model, used to send image to the API
and get the embeddings.
Example:
```json
{
"image_b64": "base64 encoded image"
}
```
"""
image_b64: Union[str, List[str]] = Field(default_factory=list)
def to_json(self):
return self.model_dump_json()
class ZeroShotClassificationRequest(BaseModel):
"""
ZeroShotClassificationRequest model, used to send
labels and images to the API and get the classification
results.
Example:
```json
{
"labels": ["dog", "cat"],
"images_b64": ["base64 encoded image"]
}
```
"""
labels: List[str] = Field(default_factory=list)
images_b64: List[str] = Field(default_factory=list)
def to_json(self):
return self.model_dump_json()
################################################################################
"""
Response models
"""
class ImageEmbedding(BaseModel):
image_id: str
embedding: List[float]
class TextEmbedding(BaseModel):
text: str
embedding: List[float]
class SoftmaxOutput(BaseModel):
image_id: str
softmax_scores: List[float]
class ClassificationResult(BaseModel):
labels: List[str] = Field(default_factory=list)
softmax_outputs: List[SoftmaxOutput] = Field(default_factory=list)
class TextEmbeddingResponse(BaseModel):
"""
TextEmbeddingResponse model, response format for text embeddings.
Example:
```json
{
"model_name": "clip",
"text_embeddings": [
{
"text": "a photo of a cat",
"embedding": [0.12, 0.21, 0.38, ..., 0.512]
},
...
]
}
```
"""
model_config = ConfigDict(protected_namespaces=())
model_name: str
text_embeddings: List[TextEmbedding] = Field(default_factory=list)
class ImageEmbeddingResponse(BaseModel):
"""
ImageEmbeddingResponse model, response format for image embeddings.
Example:
```json
{
"model_name": "clip",
"image_embeddings": [
{
"image_id": "36d2e446-e5ad-423e-a847-b7da1a2b4d70",
"embedding": [0.44, 0.55, 0.61, ..., 0.512]
},
...
]
}
```
"""
model_config = ConfigDict(protected_namespaces=())
model_name: str
image_embeddings: List[ImageEmbedding] = Field(default_factory=list)
class ClassificationResponse(BaseModel):
"""
ClassificationResponse model, response format for classification results.
Example:
```json
{
"model_name": "clip",
"text_embeddings": [
{
"text": "cat",
"embedding": [0.12, 0.21, 0.38, ..., 0.512]
},
...
],
"image_embeddings": [
{
"image_id": "36d2e446-e5ad-423e-a847-b7da1a2b4d70",
"embedding": [0.44, 0.55, 0.61, ..., 0.512]
},
...
],
"classification_result": {
"labels": ["cat", "dog", "bird"],
"softmax_outputs": [
{
"image_id": "36d2e446-e5ad-423e-a847-b7da1a2b4d70",
"softmax_scores": [0.7, 0.2, 0.1]
},
...
]
}
}
```
"""
model_config = ConfigDict(protected_namespaces=())
model_name: str
text_embeddings: List[TextEmbedding] = Field(default_factory=list)
image_embeddings: List[ImageEmbedding] = Field(default_factory=list)
classification_result: ClassificationResult = Field(default_factory=list)