Created
November 7, 2023 23:44
-
-
Save gourneau/31c8d82b33ca6ee4648f991fddc49081 to your computer and use it in GitHub Desktop.
dream.py
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
import base64 | |
import json | |
import sys | |
import uuid | |
from openai import OpenAI | |
import requests | |
from environs import Env | |
# Init environment | |
env = Env() | |
env.read_env() | |
api_key = env("OPENAI_API_KEY") | |
# Function to encode the image | |
def encode_image(image_path): | |
with open(image_path, "rb") as image_file: | |
return base64.b64encode(image_file.read()).decode('utf-8') | |
# Path to your image | |
image_path = "clouds.jpg" | |
# Getting the base64 string | |
base64_image = encode_image(image_path) | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {api_key}" | |
} | |
prompt = "Here is a photo of a cloud. Think about what also it might look like you can think abstractly and freely. Give me a description of what it also might look like. Use your imagination like a child would, and describe what you imagine. Describe it in a way that could be used to make an illustration for a children's book. It needs to be concrete a cotton and similar generic responses not a good responses. Use child like wonder. The color is too important because it will always be white like clouds, instead focus more on the shapes. The description should not really talk about cloud, because we want to find the next closest things to clouds they look like. Try to make a descripton that matches says where the objects should be placed in the image. For example, the description should say something like the object should be placed in the top left corner of the image. The description should be a complete sentence. The description should be a complete sentence. Try to avoid clichés like cotton candy and sheep. Make the description as unique as possible, and make it beatiful and colorful and fun" | |
payload = { | |
"model": "gpt-4-vision-preview", | |
"messages": [ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": prompt | |
}, | |
{ | |
"type": "image_url", | |
"image_url": { | |
"url": f"data:image/jpeg;base64,{base64_image}" | |
} | |
} | |
] | |
} | |
], | |
"max_tokens": 300 | |
} | |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) | |
pretty_json = json.dumps(response.json(), indent=4, sort_keys=True) | |
#print(pretty_json) | |
content = response.json()["choices"][0]["message"]["content"] | |
print(content) | |
print("Generating image...") | |
client = OpenAI() | |
response = client.images.generate( | |
model="dall-e-3", | |
prompt= content, | |
size="1024x1024", | |
quality="standard", | |
n=1, | |
) | |
image_url = response.data[0].url | |
def download_file(url, filename): | |
# Sends a GET request to the specified URL | |
response = requests.get(url, stream=True) | |
# Check if the request was successful | |
if response.status_code == 200: | |
# Open the file in binary write mode | |
with open(filename, 'wb') as file: | |
for chunk in response.iter_content(chunk_size=128): | |
file.write(chunk) | |
print(f"Downloaded '{filename}'.") | |
else: | |
print(f"Failed to download from '{url}'. Status code: {response.status_code}") | |
# Generate a random UUID | |
file_uuid = uuid.uuid4() | |
# Convert UUID to a string that can be used as a filename | |
filename = str(file_uuid) + '.png' # Replace '.ext' with the desired file extension | |
download_file(image_url, filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment