-
-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import instructor | ||
from openai import OpenAI | ||
from typing import Iterable | ||
from pydantic import BaseModel | ||
import base64 | ||
|
||
client = instructor.patch(OpenAI(), mode=instructor.function_calls.Mode.MD_JSON) | ||
|
||
class Circle(BaseModel): | ||
x: int | ||
y: int | ||
color: str | ||
|
||
def encode_image(image_path): | ||
with open(image_path, "rb") as image_file: | ||
return base64.b64encode(image_file.read()).decode('utf-8') | ||
|
||
def draw_circle(image_size, num_circles, path): | ||
|
||
from PIL import Image, ImageDraw | ||
import random | ||
|
||
image = Image.new("RGB", image_size, "white") | ||
|
||
draw = ImageDraw.Draw(image) | ||
for _ in range(num_circles): | ||
# Randomize the circle properties | ||
radius = 100#random.randint(10, min(image_size)//5) # Radius between 10 and 1/5th of the smallest dimension | ||
x = random.randint(radius, image_size[0] - radius) | ||
y = random.randint(radius, image_size[1] - radius) | ||
color = ['red', 'black', 'blue', 'green'][random.randint(0, 3)] | ||
|
||
circle_position = (x - radius, y - radius, x + radius, y + radius) | ||
print(f"Generating circle at {x, y} with color {color}") | ||
draw.ellipse(circle_position, fill=color, outline="black") | ||
|
||
image.save(path) | ||
|
||
img_path = 'circle.jpg' | ||
draw_circle((1024,1024), 1, img_path) | ||
base64_image = encode_image(img_path) | ||
|
||
response = client.chat.completions.create( | ||
model="gpt-4-vision-preview", | ||
max_tokens=1800, | ||
response_model=Circle, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "text", "text": 'find the circle'}, | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": f"data:image/jpeg;base64,{base64_image}" | ||
}, | ||
}, | ||
], | ||
} | ||
], | ||
) | ||
|
||
print(f"Found circle with center at x: {response.x}, y: {response.y} and color: {response.color}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters