instructor

Форк
0
65 строк · 1.8 Кб
1
import instructor
2
from openai import OpenAI
3
from pydantic import BaseModel
4
import base64
5

6
client = instructor.from_openai(OpenAI(), mode=instructor.Mode.MD_JSON)
7

8

9
class Circle(BaseModel):
10
    x: int
11
    y: int
12
    color: str
13

14

15
def encode_image(image_path):
16
    with open(image_path, "rb") as image_file:
17
        return base64.b64encode(image_file.read()).decode("utf-8")
18

19

20
def draw_circle(image_size, num_circles, path):
21
    from PIL import Image, ImageDraw
22
    import random
23

24
    image = Image.new("RGB", image_size, "white")
25

26
    draw = ImageDraw.Draw(image)
27
    for _ in range(num_circles):
28
        # Randomize the circle properties
29
        radius = 100  # random.randint(10, min(image_size)//5)  # Radius between 10 and 1/5th of the smallest dimension
30
        x = random.randint(radius, image_size[0] - radius)
31
        y = random.randint(radius, image_size[1] - radius)
32
        color = ["red", "black", "blue", "green"][random.randint(0, 3)]
33

34
        circle_position = (x - radius, y - radius, x + radius, y + radius)
35
        print(f"Generating circle at {x, y} with color {color}")
36
        draw.ellipse(circle_position, fill=color, outline="black")
37

38
    image.save(path)
39

40

41
img_path = "circle.jpg"
42
draw_circle((1024, 1024), 1, img_path)
43
base64_image = encode_image(img_path)
44

45
response = client.chat.completions.create(
46
    model="gpt-4-vision-preview",
47
    max_tokens=1800,
48
    response_model=Circle,
49
    messages=[
50
        {
51
            "role": "user",
52
            "content": [
53
                {"type": "text", "text": "find the circle"},
54
                {
55
                    "type": "image_url",
56
                    "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
57
                },
58
            ],
59
        }
60
    ],
61
)
62

63
print(
64
    f"Found circle with center at x: {response.x}, y: {response.y} and color: {response.color}"
65
)
66

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.