Safetensors
qwen2_vl
vidore
reranker
uminaty commited on
Commit
d902199
1 Parent(s): 874608a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +40 -20
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("lightonai/MonoQwen2-VL-2B-LoRA-Reranker")
20
-
21
- # Define the query and the image
22
- query = "What is the value of the thing in the document"
23
- image = Image.open("path_to_image.jpg")
24
-
25
- # Prepare the inputs
26
- prompt = f"Assert the relevance of the previous image document to the following query, answer True or False. The query is: {query}"
27
- inputs = processor(text=prompt, images=image, return_tensors="pt")
28
-
29
- # Run the model and obtain results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  with torch.no_grad():
31
  outputs = model(**inputs)
32
- logits = outputs.logits
33
- logits_for_last_token = logits[:, -1, :]
34
- true_token_id = processor.tokenizer.convert_tokens_to_ids("True")
35
- false_token_id = processor.tokenizer.convert_tokens_to_ids("False")
36
- relevance_score = torch.softmax(logits_for_last_token[:, [true_token_id, false_token_id]], dim=-1)
 
37
 
38
- # Print the True/False probabilities
39
- true_prob = relevance_score[:, 0].item()
40
- false_prob = relevance_score[:, 1].item()
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").