Spaces:
Running
on
Zero
Running
on
Zero
AdrienB134
commited on
Commit
•
a95df8d
1
Parent(s):
9f28ec7
vdefrv
Browse files
app.py
CHANGED
@@ -133,6 +133,8 @@ def search(query: str, ds, images, k):
|
|
133 |
results = []
|
134 |
for idx in top_k_indices:
|
135 |
results.append((images[idx])) #, f"Page {idx}"
|
|
|
|
|
136 |
print("done")
|
137 |
return results
|
138 |
|
@@ -158,7 +160,17 @@ def convert_files(files):
|
|
158 |
@spaces.GPU
|
159 |
def index_gpu(images, ds):
|
160 |
"""Example script to run inference with ColPali"""
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
# run inference - docs
|
163 |
dataloader = DataLoader(
|
164 |
images,
|
@@ -178,6 +190,9 @@ def index_gpu(images, ds):
|
|
178 |
batch_doc = {k: v.to(device) for k, v in batch_doc.items()}
|
179 |
embeddings_doc = model(**batch_doc)
|
180 |
ds.extend(list(torch.unbind(embeddings_doc.to("cpu"))))
|
|
|
|
|
|
|
181 |
return f"Uploaded and converted {len(images)} pages", ds, images
|
182 |
|
183 |
@spaces.GPU
|
|
|
133 |
results = []
|
134 |
for idx in top_k_indices:
|
135 |
results.append((images[idx])) #, f"Page {idx}"
|
136 |
+
del model
|
137 |
+
del processor
|
138 |
print("done")
|
139 |
return results
|
140 |
|
|
|
160 |
@spaces.GPU
|
161 |
def index_gpu(images, ds):
|
162 |
"""Example script to run inference with ColPali"""
|
163 |
+
# Load colpali model
|
164 |
+
model_name = "vidore/colpali-v1.2"
|
165 |
+
token = os.environ.get("HF_TOKEN")
|
166 |
+
model = ColPali.from_pretrained(
|
167 |
+
"vidore/colpaligemma-3b-pt-448-base", torch_dtype=torch.bfloat16, device_map="cuda", token = token).eval()
|
168 |
+
|
169 |
+
model.load_adapter(model_name)
|
170 |
+
model = model.eval()
|
171 |
+
processor = AutoProcessor.from_pretrained(model_name, token = token)
|
172 |
+
|
173 |
+
mock_image = Image.new("RGB", (448, 448), (255, 255, 255))
|
174 |
# run inference - docs
|
175 |
dataloader = DataLoader(
|
176 |
images,
|
|
|
190 |
batch_doc = {k: v.to(device) for k, v in batch_doc.items()}
|
191 |
embeddings_doc = model(**batch_doc)
|
192 |
ds.extend(list(torch.unbind(embeddings_doc.to("cpu"))))
|
193 |
+
del model
|
194 |
+
del processor
|
195 |
+
print("done")
|
196 |
return f"Uploaded and converted {len(images)} pages", ds, images
|
197 |
|
198 |
@spaces.GPU
|