Datasets:

ArXiv:
File size: 2,348 Bytes
9f3bc09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import base64
import requests
import os


class GPT:
    def __init__(self):
        self.api_key = os.getenv("OPENAI_API_KEY")
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.api_key}"
        }

        self.system_prompt = "You will receive an image and a question. Please start by answering the question with a simple 'Yes', 'No', or a brief answer. Afterward, provide a detailed explanation of how you arrived at your answer, including a rationale or description of the key details in the image that led to your conclusion. Ensure the evaluation is as precise and exacting as possible, scrutinizing the image thoroughly."
        self.input_tokens_count = 0
        self.output_tokens_count = 0
        

    def encode_image(self, image_path):
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    
    
    def update_tokens_count(self, response):
        self.input_tokens_count += response.json()["usage"]["prompt_tokens"]
        self.output_tokens_count += response.json()["usage"]["completion_tokens"]

    
    def show_usage(self):
        print(f"Total vlm input tokens used: {self.input_tokens_count}\nTotal vlm output tokens used: {self.output_tokens_count}")


    def predict(self, image_path, query):
        # Getting the base64 string
        base64_image = self.encode_image(image_path)

        payload = {
        "model": "gpt-4o-2024-08-06",
        "messages": [
            {
            "role": "system",
            "content": self.system_prompt
            },
            {
            "role": "user",
            "content": [
                {
                "type": "text",
                "text": query
                },
                {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image}"
                }
                }
            ]
            }
        ],
        "max_tokens": 300
        }
        
        response = requests.post("https://api.openai.com/v1/chat/completions", headers=self.headers, json=payload)
        self.update_tokens_count(response)
        response_content = response.json()["choices"][0]["message"]["content"]

        return response_content