File size: 1,795 Bytes
8fcd0f0
 
 
 
38a4fa7
 
8fcd0f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import requests
import os 

openai_api_key=os.environ.get("openai_api_key")

#openai_api_key = os.getenv('openai_api_key')
# Mock function to simulate image encoding and API call
def encode_image_to_base64(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')
    


# Function to get summary from OpenAI GPT-4 Vision API
def get_image_summary(image_path):
    # Encode the selected image
    base64_image = encode_image_to_base64(image_path)

    # OpenAI API URL and Key
    api_url = "https://api.openai.com/v1/chat/completions"
    #openai_api_key = "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"

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {openai_api_key}"
    }

    payload = {
        "model": "gpt-4-vision-preview",  # Update this if the model name changes
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpeg;base64,{base64_image}"
                        }
                    },
                    {
                        "type": "text",
                        "text": "You have provide an explanation for this figure or table. Consider elements like panels, axis, data and labels and etc."
                    }
                ]
            }
        ],
        "max_tokens": 1000
    }

    response = requests.post(api_url, headers=headers, json=payload)
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return "Failed to get summary. Please try again."