-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
248 lines (190 loc) · 6.37 KB
/
__init__.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""Audio Retrieval plugin.
| Copyright 2017-2023, Voxel51, Inc.
| `voxel51.com <https://voxel51.com/>`_
|
"""
import base64
from bson import json_util
import json
import fiftyone.operators as foo
import fiftyone.operators.types as types
import fiftyone.core.utils as fou
import qdrant_client as qc
import qdrant_client.http.models as qmodels
import replicate
def _to_qdrant_id(_id):
return _id + "00000000"
def _to_qdrant_ids(ids):
return [_to_qdrant_id(_id) for _id in ids]
def _to_fiftyone_id(qid):
return qid.replace("-", "")[:-8]
model_uri = "daanelson/imagebind:0383f62e173dc821ec52663ed22a076d9c970549c209666ac3db181618b7a304"
def _imagebind_embed_image(fp):
return replicate.run(
model_uri,
input={"input": open(fp, "rb"), "modality": "vision"},
)
def _execution_mode(ctx, inputs):
delegate = ctx.params.get("delegate", False)
if delegate:
description = "Uncheck this box to execute the operation immediately"
else:
description = "Check this box to delegate execution of this task"
inputs.bool(
"delegate",
default=False,
required=True,
label="Delegate execution?",
description=description,
view=types.CheckboxView(),
)
if delegate:
inputs.view(
"notice",
types.Notice(
label=(
"You've chosen delegated execution. Note that you must "
"have a delegated operation service running in order for "
"this task to be processed. See "
"https://docs.voxel51.com/plugins/index.html#operators "
"for more information"
)
),
)
def _get_collection_name(dataset):
return f"imagebind_{dataset.name}"
def _create_index(dataset):
embeddings, sample_ids = [], []
for sample in dataset.iter_samples(progress=True):
if "imagebind_embedding" in sample:
ie = sample["imagebind_embedding"]
else:
ie = _imagebind_embed_image(sample.filepath)
embeddings.append(ie)
sample_ids.append(sample.id)
batch_size = 100
client = qc.QdrantClient()
vectors_config = qmodels.VectorParams(
size=1024,
distance=qmodels.Distance.COSINE,
)
client.recreate_collection(
collection_name=_get_collection_name(dataset),
vectors_config=vectors_config,
)
for _embeddings, _ids in zip(
fou.iter_batches(embeddings, batch_size),
fou.iter_batches(sample_ids, batch_size),
):
client.upsert(
collection_name=_get_collection_name(dataset),
points=qmodels.Batch(
ids=_to_qdrant_ids(_ids),
payloads=[{"sample_id": _sid} for _sid in _ids],
vectors=_embeddings,
),
)
class CreateImageBindIndex(foo.Operator):
@property
def config(self):
_config = foo.OperatorConfig(
name="create_imagebind_index",
label="Audio Retrieval: Create vector index for images <> audio",
dynamic=True,
)
_config.icon = "/assets/icon.svg"
return _config
def resolve_input(self, ctx):
inputs = types.Object()
inputs.message(
"create_image_audio_index",
label="Audio Retrieval: Create vector index for images <> audio",
)
_execution_mode(ctx, inputs)
return types.Property(inputs)
def resolve_delegation(self, ctx):
return ctx.params.get("delegate", False)
def execute(self, ctx):
_create_index(ctx.dataset)
ctx.trigger("reload_dataset")
def _imagebind_embed_audio(audio_data):
return replicate.run(
model_uri,
input={"input": audio_data, "modality": "audio"},
)
def handle_payload(payload):
base64_string = payload.get("file_data", "")
audio_data = base64.b64decode(base64_string)
return audio_data
def run_audio_image_search(ctx):
current_ids = ctx.view.values("id")
_filter = qmodels.Filter(
must=[qmodels.HasIdCondition(has_id=_to_qdrant_ids(current_ids))]
)
dataset = ctx.dataset
k = int(ctx.params.get("num_results"))
audio_data = handle_payload(ctx.params)
TMP_FILE = "tmp.wav"
ofile = open(TMP_FILE, "wb")
ofile.write(audio_data)
query_embedding = _imagebind_embed_audio(open(TMP_FILE, "rb"))
client = qc.QdrantClient()
results = client.search(
collection_name=_get_collection_name(dataset),
query_vector=query_embedding,
with_payload=False,
limit=k,
query_filter=_filter,
)
sample_ids = [_to_fiftyone_id(sc.id) for sc in results]
view = ctx.dataset.select(sample_ids, ordered=True)
return view
def serialize_view(view):
return json.loads(json_util.dumps(view._serialize()))
class OpenAudioRetrievalPanel(foo.Operator):
@property
def config(self):
return foo.OperatorConfig(
name="open_audio_retrieval_panel",
label="Audio Retrieval: open audio retrieval panel",
icon="/assets/icon.svg",
)
def resolve_placement(self, ctx):
return types.Placement(
types.Places.SAMPLES_GRID_SECONDARY_ACTIONS,
types.Button(
label="Audio to Image Search",
icon="/assets/icon.svg",
prompt=False,
),
)
def execute(self, ctx):
ctx.trigger(
"open_panel",
params=dict(
name="AudioRetrievalPanel", isActive=True, layout="horizontal"
),
)
class RunAudioToImageSearch(foo.Operator):
@property
def config(self):
return foo.OperatorConfig(
name="search_images_from_audio",
label="Search Images from Audio",
unlisted=True,
)
def resolve_input(self, ctx):
inputs = types.Object()
inputs.int("num_results", label="Number of Results", required=True)
inputs.str("file_data", label="Image File", required=False)
return types.Property(inputs)
def execute(self, ctx):
view = run_audio_image_search(ctx)
ctx.trigger(
"set_view",
params=dict(view=serialize_view(view)),
)
def register(p):
p.register(RunAudioToImageSearch)
p.register(OpenAudioRetrievalPanel)
p.register(CreateImageBindIndex)