Views
No views yet


1import torch
2from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
3from qwen_vl_utils import process_vision_info
4
5
6## Initialize Model and Processor
7model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
8 "Bia/CORAL", torch_dtype="auto", device_map="auto"
9)
10
11processor = AutoProcessor.from_pretrained("Bia/CORAL")
12
13## Prepare Inputs
14query = [
15 {
16 "role": "user",
17 "content": [
18 {"type": "text", "text": "Find a product of backpack that have the same brand with <Product 1> \n "},
19 {
20 "type": "image",
21 "image": "CORAL/images/product_1.png",
22 },
23 {"type": "text", "text": "\n Ransel MOSSDOOM Polyester dengan Ruang Komputer dan Penyimpanan Besar, Ukuran $30 \times 12 \times 38$ cm , Berat 0.32 kg. </Product 1> and the same fashion style with <Product 2> "},
24 {
25 "type": "image",
26 "image": "CORAL/images/product_2.png",
27 },
28 {"type": "text", "text": "\n Elegant Pink Flats with Low Heel and Buckle Closure for Stylish Party Wear </Product 2> with a quilted texture and a chain strap."}
29 ],
30 }
31]
32
33candidate = [
34 {
35 "role": "user",
36 "content": [
37 {"type": "text", "text": "Represent the given product: "},
38 {
39 "type": "image",
40 "image": "CORAL/images/product_3.png",
41 },
42 {"type": "text", "text": "\n MOSSDOOM Elegant Pink PU Leather Handbag with Chain Strap and Large Capacity, Compact Size $18 \times 9.5 \times 15 \mathrm{~cm}$."},
43 ],
44 }
45]
46
47query_text = processor.apply_chat_template(
48 query, tokenize=False, add_generation_prompt=True
49)
50
51candidate_text = processor.apply_chat_template(
52 candidate, tokenize=False, add_generation_prompt=True
53)
54
55query_image_inputs, query_video_inputs = process_vision_info(query)
56
57candidate_image_inputs, candidate_video_inputs = process_vision_info(candidate)
58
59query_inputs = processor(
60 text=[query_text],
61 images=query_image_inputs,
62 videos=query_video_inputs,
63 padding=True,
64 return_tensors="pt",
65).to("cuda")
66
67candidate_inputs = processor(
68 text=[candidate_text],
69 images=candidate_image_inputs,
70 videos=candidate_video_inputs,
71 padding=True,
72 return_tensors="pt",
73).to("cuda")
74
75
76# Encode Embeddings
77with torch.inference_mode():
78 query_outputs = model(**query_inputs, return_dict=True, output_hidden_states=True)
79 query_embedding = query_outputs.hidden_states[-1][:,-1,:]
80 query_embedding = torch.nn.functional.normalize(query_embedding, dim=-1)
81 print(query_embedding.shape) # torch.Size([1, 2048])
82
83 candidate_outputs = model(**candidate_inputs, return_dict=True, output_hidden_states=True)
84 candidate_embedding = candidate_outputs.hidden_states[-1][:,-1,:]
85 candidate_embedding = torch.nn.functional.normalize(candidate_embedding, dim=-1)
86 print(candidate_embedding.shape) # torch.Size([1, 2048])
87
88# Compute Similarity
89similarity = torch.matmul(query_embedding, candidate_embedding.T)
90print(similarity) # tensor([[0.6992]], device='cuda:0', dtype=torch.bfloat16)
1@article{chow2025merit,
2 title={MERIT: Multilingual Semantic Retrieval with Interleaved Multi-Condition Query},
3 author={Chow, Wei and Gao, Yuan and Li, Linfeng and Wang, Xian and Xu, Qi and Song, Hang and Kong, Lingdong and Zhou, Ran and Zeng, Yi and Cai, Yidong and others},
4 journal={arXiv preprint arXiv:2506.03144},
5 year={2025}
6}