File size: 1,907 Bytes
1380717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import requests
from io import BytesIO
from PIL import Image

def encode_image(img):
    buffered = BytesIO()
    img.save(buffered, format="PNG")
    encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
    return encoded_string

# ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ œ๋Œ€๋กœ ์„ค์ •
img_path = "./1.png"
try:
    img = Image.open(img_path)
except FileNotFoundError:
    print(f"Error: The image file '{img_path}' was not found.")
    exit()

base64_img = encode_image(img)

api = "https://api.hyperbolic.xyz/v1/chat/completions"
api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZzMyNzAyNEBnbWFpbC5jb20ifQ._frFve-BYZdb0Qo6FIj6xcDcxpY-6QlC2O-ToQxBjkc"  # ์‹ค์ œ API ํ‚ค๋กœ ์ˆ˜์ • ํ•„์š”

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

payload = {
    "messages": [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What is this image?"},
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/png;base64,{base64_img}"},  # PNG MIME ํƒ€์ž… ์ˆ˜์ •
                },
            ],
        }
    ],
    "model": "mistralai/Pixtral-12B-2409",
    "max_tokens": 2048,
    "temperature": 0.7,
    "top_p": 0.9,
}

try:
    response = requests.post(api, headers=headers, json=payload)
    response.raise_for_status()  # HTTPError๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ
    result = response.json()  # JSON์œผ๋กœ ํŒŒ์‹ฑ
    print(result)
except requests.exceptions.HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")  # HTTP ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
except requests.exceptions.RequestException as req_err:
    print(f"Request error occurred: {req_err}")  # ์ผ๋ฐ˜ ์š”์ฒญ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
except ValueError as json_err:
    print(f"JSON decode error: {json_err}")  # JSON ํŒŒ์‹ฑ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ