Update README.md
Browse files
README.md
CHANGED
@@ -16,30 +16,50 @@ from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
|
|
16 |
|
17 |
# Load processor and model
|
18 |
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
19 |
-
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
with torch.no_grad():
|
31 |
outputs = model(**inputs)
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
-
#
|
39 |
-
true_prob = relevance_score[
|
40 |
-
false_prob = relevance_score[
|
41 |
|
42 |
-
print(f"True probability: {true_prob}, False probability: {false_prob}")
|
43 |
```
|
44 |
|
45 |
This example demonstrates how to use the model to assess the relevance of an image with respect to a query. It outputs the probability that the image is relevant ("True") or not relevant ("False").
|
|
|
16 |
|
17 |
# Load processor and model
|
18 |
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
19 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
20 |
+
"lightonai/MonoQwen2-VL-2B-LoRA-Reranker",
|
21 |
+
)
|
22 |
+
|
23 |
+
# Define query and load image
|
24 |
+
query = "Is this your query about a document ?"
|
25 |
+
image_path = "your/path/to/image.png"
|
26 |
+
image = Image.open(image_path)
|
27 |
+
|
28 |
+
# Construct the prompt and prepare input
|
29 |
+
prompt = (
|
30 |
+
"Assert the relevance of the previous image document to the following query, "
|
31 |
+
"answer True or False. The query is: {query}"
|
32 |
+
).format(query=query)
|
33 |
+
|
34 |
+
messages = [
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": [
|
38 |
+
{"type": "image", "image": image},
|
39 |
+
{"type": "text", "text": prompt},
|
40 |
+
],
|
41 |
+
}
|
42 |
+
]
|
43 |
+
|
44 |
+
# Apply chat template and tokenize
|
45 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
46 |
+
inputs = processor(text=text, images=image, return_tensors="pt").to("cuda:1")
|
47 |
+
|
48 |
+
# Run inference to obtain logits
|
49 |
with torch.no_grad():
|
50 |
outputs = model(**inputs)
|
51 |
+
logits_for_last_token = outputs.logits[:, -1, :]
|
52 |
+
|
53 |
+
# Convert tokens and calculate relevance score
|
54 |
+
true_token_id = processor.tokenizer.convert_tokens_to_ids("True")
|
55 |
+
false_token_id = processor.tokenizer.convert_tokens_to_ids("False")
|
56 |
+
relevance_score = torch.softmax(logits_for_last_token[:, [true_token_id, false_token_id]], dim=-1)
|
57 |
|
58 |
+
# Extract and display probabilities
|
59 |
+
true_prob = relevance_score[0, 0].item()
|
60 |
+
false_prob = relevance_score[0, 1].item()
|
61 |
|
62 |
+
print(f"True probability: {true_prob:.4f}, False probability: {false_prob:.4f}")
|
63 |
```
|
64 |
|
65 |
This example demonstrates how to use the model to assess the relevance of an image with respect to a query. It outputs the probability that the image is relevant ("True") or not relevant ("False").
|