File size: 2,163 Bytes
54ec520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
import json
import requests
import base64


def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')
    
def get_gpt_response(api_key, image_path, prompt, history=None):

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    
    if history:
        if len(history) > 4:
            history = history[-4:]
    else:
        history = []
    
    messages = history[:]
    base64_images = []
    
    if image_path:
        if isinstance(image_path, list):
            for img in image_path:
                base64_image = encode_image(img)
                base64_images.append(base64_image)
        else:
            base64_image = encode_image(image_path)
            base64_images.append(base64_image)
        
        messages.append({
            "role": "user",
            "content": [
                    {
                        "type": "text",
                        "text": prompt
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpeg;base64,{base64_images}"
                        }
                    }
                ]
        })
    else: 
        messages.append({
            "role": "user",
            "content": prompt
        })
                
    payload = {
        "model": "gpt-4o",
        "messages": messages,
        "max_tokens": 600
    }
    

    # Sending the request to the OpenAI API
    response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
    result = response.json()
    print("gpt result",result)
    try:
        content = result['choices'][0]['message']['content']
        if content.startswith("```json"):
                content = content[7:]
        if content.endswith("```"):
            content = content[:-3]
        return content
    except (KeyError, IndexError, json.JSONDecodeError) as e:
        return json.dumps({"error": "Failed to parse model output", "details": str(e)})