Views
No views yet
transformers==4.45.2.1from transformers import MllamaForConditionalGeneration, AutoProcessor
2import torch
3from PIL import Image
4
5CORRECTION_TEMPLATE = """Below is a QUESTION from a user and an EXAMPLE RESPONSE.
6Please provide a more helpful RESPONSE, improving the EXAMPLE RESPONSE by making the content even clearer, more accurate, and with a reasonable logic.
7Focus on addressing the human's QUESTION step by step based on the image without including irrelevant content.
8
9QUESTION:
10{Question}
11
12EXAMPLE RESPONSE:
13{Example_Response}
14
15Now, refine and improve the RESPONSE further. You can consider two approaches:
161. REFINEMENT: If the SUMMARY section in the response is closely related to the question, the CAPTION section accurately describes the image, the REASONING section is logically clear and correct without any contradictions, and the CONCLUSION provides an accurate answer based on the previous steps, enhance clarity, accuracy, or reasoning logic as needed.
172. NEW RESPONSE: If the SUMMARY section incorrectly summarizes the intent of the issue, the CAPTION contains content unrelated to or incorrect about the image, there are logical errors or contradictions in the REASONING, or the CONCLUSION incorrectly states the findings, please enhance the accuracy and quality of each step, and craft a more effective RESPONSE that thoroughly resolves the QUESTION.
18
19RESPONSE:
20"""
21
22question = """Hint: Please answer the question requiring an integer answer and provide the final value, e.g., 1, 2, 3, at the end.
23Question: What is the age gap between these two people in image? (Unit: years)"""
24
25image_path = "./case.jpg"
26
27model_path = "Tuwhy/Llama-3.2V-11B-Sherlock-iter2"
28
29model = MllamaForConditionalGeneration.from_pretrained(
30 model_path,
31 torch_dtype=torch.bfloat16,
32 device_map='cpu',
33 ).cuda().eval()
34
35device = 'cuda'
36processor = AutoProcessor.from_pretrained(model_path)
37
38# kwargs_default = dict(do_sample=False, max_new_tokens=2048, temperature=0.0, top_p=None, num_beams=1)
39kwargs_default = dict(do_sample=True, max_new_tokens=2048, temperature=0.6, top_p=0.7, num_beams=1)
40
41image = Image.open(image_path)
42messages = [
43 {'role': 'user', 'content': [
44 {'type': 'image'},
45 {'type': 'text', 'text': question}
46 ]}
47]
48
49input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
50inputs = processor(image, input_text, return_tensors='pt').to(device)
51output = model.generate(**inputs, **kwargs_default)
52response = processor.decode(output[0][inputs['input_ids'].shape[1]:]).replace('<|eot_id|>', '')
53
54print(f"INTIAL RESPONSE: {response}")
55
56for i in range(3):
57 prompt = CORRECTION_TEMPLATE.format(
58 Question=question,
59 Example_Response=response
60 )
61 messages = [
62 {'role': 'user', 'content': [
63 {'type': 'image'},
64 {'type': 'text', 'text': prompt}
65 ]}
66 ]
67 input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
68 inputs = processor(image, input_text, return_tensors='pt').to(device)
69 output = model.generate(**inputs, **kwargs_default)
70 response = processor.decode(output[0][inputs['input_ids'].shape[1]:]).replace('<|eot_id|>', '')
71
72 print(f"REFINED RESPONSE {i+1}: {response}")