Skip to content
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

Closed
haesleinhuepf opened this issue Oct 27, 2024 · 6 comments
Closed

[Test]: Re-use code #70

haesleinhuepf opened this issue Oct 27, 2024 · 6 comments
Assignees

Comments

@haesleinhuepf
Copy link
Owner

Copy over the create function from here: https://github.com/haesleinhuepf/darth-d/blob/main/darth_d/_create.py

@haesleinhuepf haesleinhuepf self-assigned this Oct 27, 2024
@haesleinhuepf
Copy link
Owner Author

git-bob comment

Repository owner deleted a comment from github-actions bot Oct 27, 2024
Repository owner deleted a comment from github-actions bot Oct 27, 2024
Copy link
Contributor

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 create function. I'll provide a Jupyter notebook with the function copied over:

%%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 images_from_url_responses function or import it from the appropriate location to make this work.

@haesleinhuepf
Copy link
Owner Author

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

Copy link
Contributor

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.

@haesleinhuepf
Copy link
Owner Author

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.

Copy link
Contributor

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 images_from_url_responses where we now use PIL's Image directly instead of converting between numpy arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant