-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Test]: Re-use code #70
Comments
git-bob comment |
This message was generated by git-bob (version: 0.11.2dev, model: claude-3-5-sonnet-20241022, log), an experimental AI-based assistant. It can make mistakes and has limitations. Check its messages carefully. It seems there was a request to re-use the %%notebook
from stackview import jupyter_displayable_output
@jupyter_displayable_output(library_name='darth-d', help_url='https://github.com/haesleinhuepf/darth-d')
def create(prompt:str=None, image_width:int=1024, image_height:int=1024, num_images:int=1, model:str="dall-e-3", style:str='vivid', quality:str='standard'):
"""Create an image from scratch using OpenAI's DALL-E 2 or 3.
Parameters
----------
prompt: str, text explaining what the image should show
num_images: int, optional
ignored for dall-e-3
model: str, optional
"dall-e-2", "dall-e-3"
image_width: int, optional
must be 256, 512 or 1024 for dall-e-2 or 1024, 1792 for dall-e-3
image_height: int, optional
must be 256, 512 or 1024 for dall-e-2 or 1024, 1792 for dall-e-3
style: str, optional
"vivid" or "natural", ignored for dall-e-2
quality: str, optional
"standard" or "hd", ignored for dall-e-2
"""
from openai import OpenAI
from ._utilities import images_from_url_responses
client = OpenAI()
size_str = f"{image_width}x{image_height}"
kwargs = {}
if model == "dall-e-3":
kwargs['style'] = style
kwargs['quality'] = quality
response = client.images.generate(
prompt=prompt,
n=num_images,
model=model,
size=size_str,
**kwargs
)
return images_from_url_responses(response) Note: You'll need to implement the |
Ok cool. Take over the code of the `images_from_url_responses function from https://github.com/haesleinhuepf/darth-d/blob/main/darth_d/_utilities.py git-bob comment |
This message was generated by git-bob (version: 0.11.2dev, model: claude-3-5-sonnet-20241022, log), an experimental AI-based assistant. It can make mistakes and has limitations. Check its messages carefully. I'll update the notebook to include both functions: %%notebook
from stackview import jupyter_displayable_output
import numpy as np
from skimage.io import imread
from skimage import transform
from PIL import Image
import io
def numpy_to_bytestream(data):
"""Turn a NumPy array into a bytestream"""
image = Image.fromarray(data.astype(np.uint8)).convert("RGBA")
bytes_io = io.BytesIO()
image.save(bytes_io, format='PNG')
bytes_io.seek(0)
return bytes_io.read()
def images_from_url_responses(response, input_shape = None):
"""Turns a list of OpenAI's URL responses into numpy images"""
images = [imread(item.url) for item in response.data]
if input_shape is not None:
images = [transform.resize(image, input_shape, anti_aliasing=True, preserve_range=True).astype(image.dtype) for image in images]
if len(input_shape) == 2 and len(images[0].shape) == 3:
images = [image[:,:,0] for image in images]
if len(images) == 1:
return images[0]
else:
return np.asarray(images)
@jupyter_displayable_output(library_name='darth-d', help_url='https://github.com/haesleinhuepf/darth-d')
def create(prompt:str=None, image_width:int=1024, image_height:int=1024, num_images:int=1, model:str="dall-e-3", style:str='vivid', quality:str='standard'):
"""Create an image from scratch using OpenAI's DALL-E 2 or 3."""
from openai import OpenAI
client = OpenAI()
size_str = f"{image_width}x{image_height}"
kwargs = {}
if model == "dall-e-3":
kwargs['style'] = style
kwargs['quality'] = quality
response = client.images.generate(
prompt=prompt,
n=num_images,
model=model,
size=size_str,
**kwargs
)
return images_from_url_responses(response) All necessary functions are now included in one notebook. I also added the required imports at the top. |
Do not write a notebook but plain Python code instead. Furthermore, do not use numpy arrays as images, but PIL image instead. git-bob comment on how to implement this. |
This message was generated by git-bob (version: 0.11.2dev, model: claude-3-5-sonnet-20241022, log), an experimental AI-based assistant. It can make mistakes and has limitations. Check its messages carefully. Here's how we can modify the code to use PIL images instead of numpy arrays: from PIL import Image
import io
import requests
def images_from_url_responses(response, input_shape=None):
"""Turns a list of OpenAI's URL responses into PIL images"""
images = []
for item in response.data:
response = requests.get(item.url)
img = Image.open(io.BytesIO(response.content))
if input_shape:
img = img.resize(input_shape)
images.append(img)
return images[0] if len(images) == 1 else images
def create(prompt:str=None, image_width:int=1024, image_height:int=1024, num_images:int=1,
model:str="dall-e-3", style:str='vivid', quality:str='standard'):
"""Create an image using OpenAI's DALL-E"""
from openai import OpenAI
client = OpenAI()
size_str = f"{image_width}x{image_height}"
kwargs = {}
if model == "dall-e-3":
kwargs['style'] = style
kwargs['quality'] = quality
response = client.images.generate(
prompt=prompt,
n=num_images,
model=model,
size=size_str,
**kwargs
)
return images_from_url_responses(response) The main changes are in |
Copy over the
create
function from here: https://github.com/haesleinhuepf/darth-d/blob/main/darth_d/_create.pyThe text was updated successfully, but these errors were encountered: