Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,321 Bytes
8001e7f e3f404d 8001e7f e3f404d 8001e7f 79fd59c 404e8d7 79fd59c b7dad17 79fd59c caf5c98 79fd59c 9f28ec7 09ba206 9f28ec7 4c3be8d 79fd59c 207c30a 869830f 79fd59c 207c30a 79fd59c 207c30a 79fd59c 8001e7f 9f28ec7 8001e7f 404e8d7 8001e7f 2e15984 8001e7f 2e15984 a791fbb 404e8d7 a95df8d 28ef079 8001e7f 515c759 8001e7f a95df8d 8001e7f a95df8d 8001e7f 79fd59c 8001e7f affda77 e8e541b affda77 8001e7f 8fb0879 fc00b82 8001e7f caf5c98 8001e7f caf5c98 8001e7f 79fd59c 8001e7f ded88a4 8001e7f caf5c98 fc00b82 8001e7f caf5c98 8001e7f e3f404d 8001e7f dc5eecc e3f404d ded88a4 cef7a44 affda77 dc5eecc cef7a44 e3f404d 8001e7f |
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 249 250 |
import os
import spaces
import gradio as gr
import torch
from colpali_engine.models.paligemma_colbert_architecture import ColPali
from colpali_engine.trainer.retrieval_evaluator import CustomEvaluator
from colpali_engine.utils.colpali_processing_utils import (
process_images,
process_queries,
)
from pdf2image import convert_from_path
from PIL import Image
from torch.utils.data import DataLoader
from tqdm import tqdm
from transformers import AutoProcessor, Idefics3ForConditionalGeneration
import re
import time
from PIL import Image
import torch
import subprocess
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
import time
@spaces.GPU
def model_inference(
images, text, assistant_prefix= "Réfléchis step by step. Répond uniquement avec les informations du document fourni.", decoding_strategy = "Greedy", temperature= 0.4, max_new_tokens=512,
repetition_penalty=1.2, top_p=0.8
):
## Load idefics
id_processor = AutoProcessor.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3")
id_model = Idefics3ForConditionalGeneration.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3",
torch_dtype=torch.bfloat16,
attn_implementation="flash_attention_2"
).to("cuda")
BAD_WORDS_IDS = id_processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
EOS_WORDS_IDS = [id_processor.tokenizer.eos_token_id]
images = [Image.open(image[0]) for image in images]
if text == "" and not images:
gr.Error("Please input a query and optionally image(s).")
if text == "" and images:
gr.Error("Please input a text query along the image(s).")
if isinstance(images, Image.Image):
images = [images]
resulting_messages = [
{
"role": "user",
"content": [{"type": "image"}] + [
{"type": "text", "text": text}
]
}
]
if assistant_prefix:
text = f"{assistant_prefix} {text}"
prompt = id_processor.apply_chat_template(resulting_messages, add_generation_prompt=True)
inputs = id_processor(text=prompt, images=[images], return_tensors="pt")
inputs = {k: v.to("cuda") for k, v in inputs.items()}
generation_args = {
"max_new_tokens": max_new_tokens,
"repetition_penalty": repetition_penalty,
}
assert decoding_strategy in [
"Greedy",
"Top P Sampling",
]
if decoding_strategy == "Greedy":
generation_args["do_sample"] = False
elif decoding_strategy == "Top P Sampling":
generation_args["temperature"] = temperature
generation_args["do_sample"] = True
generation_args["top_p"] = top_p
generation_args.update(inputs)
# Generate
generated_ids = id_model.generate(**generation_args)
generated_texts = id_processor.batch_decode(generated_ids[:, generation_args["input_ids"].size(1):], skip_special_tokens=True)
return generated_texts[0]
@spaces.GPU
def search(query: str, ds, images, k):
# Load colpali model
model_name = "vidore/colpali-v1.2"
token = os.environ.get("HF_TOKEN")
model = ColPali.from_pretrained(
"vidore/colpaligemma-3b-pt-448-base", torch_dtype=torch.bfloat16, device_map="cuda", token = token).eval()
model.load_adapter(model_name)
model = model.eval()
processor = AutoProcessor.from_pretrained(model_name, token = token)
mock_image = Image.new("RGB", (448, 448), (255, 255, 255))
device = "cuda:0" if torch.cuda.is_available() else "cpu"
if device != model.device:
model.to(device)
start = time.time()
qs = []
with torch.no_grad():
batch_query = process_queries(processor, [query], mock_image)
batch_query = {k: v.to(device) for k, v in batch_query.items()}
embeddings_query = model(**batch_query)
qs.extend(list(torch.unbind(embeddings_query.to("cpu"))))
retriever_evaluator = CustomEvaluator(is_multi_vector=True)
scores = retriever_evaluator.evaluate(qs, ds)
top_k_indices = scores.argsort(axis=1)[0][-k:][::-1]
results = []
print(images)
print(top_k_indices)
for idx in top_k_indices:
print(idx)
results.append((images[idx])) #, f"Page {idx}"
print(f"Temps: {time.time()- start} s")
del model
del processor
print("done")
return results
def index(files, ds):
print("Converting files")
images = convert_files(files)
print(f"Files converted with {len(images)} images.")
return index_gpu(images, ds)
def convert_files(files):
images = []
for f in files:
images.extend(convert_from_path(f, thread_count=4))
if len(images) >= 250:
raise gr.Error("The number of images in the dataset should be less than 150.")
return images
@spaces.GPU
def index_gpu(images, ds):
"""Example script to run inference with ColPali"""
# Load colpali model
model_name = "vidore/colpali-v1.2"
token = os.environ.get("HF_TOKEN")
model = ColPali.from_pretrained(
"vidore/colpaligemma-3b-pt-448-base", torch_dtype=torch.bfloat16, device_map="cuda", token = token).eval()
model.load_adapter(model_name)
model = model.eval()
processor = AutoProcessor.from_pretrained(model_name, token = token)
mock_image = Image.new("RGB", (448, 448), (255, 255, 255))
# run inference - docs
dataloader = DataLoader(
images,
batch_size=4,
shuffle=False,
collate_fn=lambda x: process_images(processor, x),
)
device = "cuda:0" if torch.cuda.is_available() else "cpu"
if device != model.device:
model.to(device)
for batch_doc in tqdm(dataloader):
with torch.no_grad():
batch_doc = {k: v.to(device) for k, v in batch_doc.items()}
embeddings_doc = model(**batch_doc)
ds.extend(list(torch.unbind(embeddings_doc.to("cpu"))))
del model
del processor
print("done")
return f"Uploaded and converted {len(images)} pages", ds, images
def get_example():
return [
[["RAPPORT_DEVELOPPEMENT_DURABLE_2019.pdf"], "Quels sont les 4 axes majeurs des achats?"],
[["RAPPORT_DEVELOPPEMENT_DURABLE_2019.pdf"], "Quelles sont les actions entreprise en Afrique du Sud?"],
[["RAPPORT_DEVELOPPEMENT_DURABLE_2019.pdf"], "fais moi un tableau de la répartition homme femme"],
]
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# ColPali + Idefics3: Efficient Document Retrieval with Vision Language Models 📚")
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("## 1️⃣ Upload PDFs")
file = gr.File(file_types=["pdf"], file_count="multiple", label="Upload PDFs")
message = gr.Textbox("Files not yet uploaded", label="Status")
convert_button = gr.Button("🔄 Index documents")
embeds = gr.State(value=[])
imgs = gr.State(value=[])
img_chunk = gr.State(value=[])
with gr.Column(scale=3):
gr.Markdown("## 2️⃣ Search with ColPali")
query = gr.Textbox(placeholder="Enter your query here", label="Query")
k = gr.Slider(minimum=1, maximum=10, step=1, label="Number of results", value=5)
search_button = gr.Button("🔍 Search", variant="primary")
with gr.Row():
gr.Examples(
examples=get_example(),
inputs=[file, query],
)
# Define the actions
output_gallery = gr.Gallery(label="Retrieved Documents", height=600, show_label=True)
convert_button.click(index, inputs=[file, embeds], outputs=[message, embeds, imgs])
search_button.click(search, inputs=[query, embeds, imgs, k], outputs=[output_gallery])
gr.Markdown("## 3️⃣ Get your answer with Idefics")
answer_button = gr.Button("Answer", variant="primary")
output = gr.Markdown(label="Output")
answer_button.click(model_inference, inputs=[output_gallery, query], outputs=output)
if __name__ == "__main__":
demo.queue(max_size=10).launch(debug=True) |