Views
No views yet
>= 5.6.0) with the image extra, plus peft for the LoRA adapter (requires transformers v5):pip install "sentence-transformers[image]" peftP("True") relevance probabilities:1from sentence_transformers import CrossEncoder
2
3model = CrossEncoder("lightonai/MonoQwen2-VL-v0.1")
4
5# Pass document page images as PIL.Image, a local file path, or an image URL
6query = "What is the variable represented on the y-axis of the graph?"
7pages = [
8 "https://huggingface.co/lightonai/MonoQwen2-VL-v0.1/resolve/main/assets/doc1.jpg",
9 "https://huggingface.co/lightonai/MonoQwen2-VL-v0.1/resolve/main/assets/doc2.jpg",
10]
11
12scores = model.predict([(query, page) for page in pages])
13print(scores)
14# [0.61328125 0.29492188]
15
16rankings = model.rank(query, pages)
17print(rankings)
18# [{'corpus_id': 0, 'score': 0.61328125}, {'corpus_id': 1, 'score': 0.29492188}]Qwen/Qwen2-VL-2B-Instruct base model automatically. The model loads in bfloat16 by default, but you can pass model_kwargs={"dtype": torch.float32} to CrossEncoder(...) for full fp32 precision. Text passages can also be passed as documents in place of images.1import requests
2import torch
3from PIL import Image
4from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
5
6# Load processor and model
7processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
8model = Qwen2VLForConditionalGeneration.from_pretrained(
9 "lightonai/MonoQwen2-VL-v0.1",
10 device_map="auto",
11 # attn_implementation="flash_attention_2",
12 # torch_dtype=torch.bfloat16,
13)
14
15# Define query and load image
16query = "What is the variable represented on the y-axis of the graph?"
17image_url = "https://huggingface.co/lightonai/MonoQwen2-VL-v0.1/resolve/main/assets/doc1.jpg"
18image = Image.open(requests.get(image_url, stream=True).raw)
19
20# Construct the prompt and prepare input
21prompt = (
22 "Assert the relevance of the previous image document to the following query, "
23 "answer True or False. The query is: {query}"
24).format(query=query)
25
26messages = [
27 {
28 "role": "user",
29 "content": [
30 {"type": "image", "image": image},
31 {"type": "text", "text": prompt},
32 ],
33 }
34]
35
36# Apply chat template and tokenize
37text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
38inputs = processor(text=text, images=image, return_tensors="pt").to("cuda")
39
40# Run inference to obtain logits
41with torch.no_grad():
42 outputs = model(**inputs)
43 logits_for_last_token = outputs.logits[:, -1, :]
44
45# Convert tokens and calculate relevance score
46true_token_id = processor.tokenizer.convert_tokens_to_ids("True")
47false_token_id = processor.tokenizer.convert_tokens_to_ids("False")
48relevance_score = torch.softmax(logits_for_last_token[:, [true_token_id, false_token_id]], dim=-1)
49
50# Extract and display probabilities
51true_prob = relevance_score[0, 0].item()
52false_prob = relevance_score[0, 1].item()
53
54print(f"True probability: {true_prob:.4f}, False probability: {false_prob:.4f}")
55# True probability: 0.6133, False probability: 0.3848peft to be installed in your environment (pip install peft). If you don't want to use peft, you can use model.load_adapter on the original Qwen2-VL-2B model.ndcg@5 scores:| Dataset | MrLight_dse-qwen2-2b-mrl-v1 | MonoQwen2-VL-v0.1 reranking |
|---|---|---|
| vidore/arxivqa_test_subsampled | 85.6 | 89.0 |
| vidore/docvqa_test_subsampled | 57.1 | 59.7 |
| vidore/infovqa_test_subsampled | 88.1 | 93.2 |
| vidore/tabfquad_test_subsampled | 93.1 | 96.0 |
| vidore/shiftproject_test | 82.0 | 93.0 |
| vidore/syntheticDocQA_artificial_intelligence_test | 97.5 | 100.0 |
| vidore/syntheticDocQA_energy_test | 92.9 | 97.7 |
| vidore/syntheticDocQA_government_reports_test | 96.0 | 98.0 |
| vidore/syntheticDocQA_healthcare_industry_test | 96.4 | 99.3 |
| vidore/tatdqa_test | 69.4 | 79.0 |
| Mean | 85.8 | 90.5 |
1@misc{MonoQwen,
2 title={MonoQwen: Visual Document Reranking},
3 author={Chaffin, Antoine and Lac, Aurélien},
4 url={https://huggingface.co/lightonai/MonoQwen2-VL-v0.1},
5 year={2024}
6}