DALL-E 2 Image Generation avatar
DALL-E 2 Image Generation
Try for free

No credit card required

View all Actors
DALL-E 2 Image Generation

DALL-E 2 Image Generation

jirimoravcik/dalle-2-image-generation
Try for free

No credit card required

This actor enables you to generate images using OpenAI's DALL-E 2.

.actor/Dockerfile

1# First, specify the base Docker image.
2# You can see the Docker images from Apify at https://hub.docker.com/r/apify/.
3# You can also use any other image from Docker Hub.
4FROM apify/actor-python:3.11
5
6# Second, copy just requirements.txt into the actor image,
7# since it should be the only file that affects the dependency install in the next step,
8# in order to speed up the build
9COPY requirements.txt ./
10
11# Install the packages specified in requirements.txt,
12# Print the installed Python version, pip version
13# and all installed packages with their versions for debugging
14RUN echo "Python version:" \
15 && python --version \
16 && echo "Pip version:" \
17 && pip --version \
18 && echo "Installing dependencies:" \
19 && pip install -r requirements.txt \
20 && echo "All installed Python packages:" \
21 && pip freeze
22
23# Next, copy the remaining files and directories with the source code.
24# Since we do this after installing the dependencies, quick build will be really fast
25# for most source file changes.
26COPY . ./
27
28# Specify how to launch the source code of your actor.
29# By default, the "python3 -m src" command is run
30CMD ["python3", "-m", "src"]

.actor/actor.json

1{
2	"actorSpecification": 1,
3	"name": "dalle-2-image-generation",
4	"title": "DALL-E 2 image generation",
5	"description": "Generates images using OpenAI's DALL-E 2.",
6	"version": "0.0",
7	"meta": {
8		"templateId": "python-start"
9	},
10	"input": "./input_schema.json",
11	"dockerfile": "./Dockerfile"
12}

.actor/input_schema.json

1{
2  "title": "DALL-E 2 image generation",
3  "type": "object",
4  "schemaVersion": 1,
5  "properties": {
6    "openai_api_key": {
7      "title": "OpenAI API key",
8      "type": "string",
9      "description": "An API key from https://platform.openai.com/account/api-keys",
10      "editor": "textfield",
11      "isSecret": true
12    },
13    "prompt": {
14        "title": "Prompt",
15        "type": "string",
16        "description": "The text prompt used to generate the image(s).",
17        "editor": "textfield"
18    },
19    "count": {
20        "title": "Count",
21        "type": "integer",
22        "description": "The number of images to generate.",
23        "editor": "number",
24        "default": 1
25    },
26    "width": {
27        "title": "Width",
28        "type": "integer",
29        "description": "Width of the images.",
30        "editor": "number",
31        "default": 512
32    },
33    "height": {
34        "title": "Height",
35        "type": "integer",
36        "description": "Height of the images.",
37        "editor": "number",
38        "default": 512
39    }
40  },
41  "required": ["openai_api_key", "prompt", "count", "width", "height"]
42}

src/__init__.py

src/__main__.py

1import asyncio
2import logging
3
4from apify.log import ActorLogFormatter
5
6from .main import main
7
8handler = logging.StreamHandler()
9handler.setFormatter(ActorLogFormatter())
10
11apify_client_logger = logging.getLogger('apify_client')
12apify_client_logger.setLevel(logging.INFO)
13apify_client_logger.addHandler(handler)
14
15apify_logger = logging.getLogger('apify')
16apify_logger.setLevel(logging.DEBUG)
17apify_logger.addHandler(handler)
18
19asyncio.run(main())

src/main.py

1import openai, requests
2from apify import Actor
3
4
5async def main():
6    async with Actor:
7        actor_input = await Actor.get_input() or {}
8
9        openai_api_key = actor_input.get('openai_api_key')
10        prompt = actor_input.get('prompt')
11        count = actor_input.get('count')
12        width = actor_input.get('width')
13        height = actor_input.get('height')
14
15        try:
16            response = openai.Image.create(
17                openai_api_key,
18                prompt = prompt,
19                n = count,
20                size = f"{width}x{height}",
21            )
22            image_url_data = response['data']
23            i = 0
24            for image_url_obj in image_url_data:
25                url = image_url_obj['url']
26                image = requests.get(url)
27                await Actor.set_value(f'image_{i}.png', image.content)
28                i += 1
29
30        except openai.error.OpenAIError as e:
31            print(e.http_status)
32            print(e.error)

.dockerignore

1# configurations
2.idea
3
4# crawlee and apify storage folders
5apify_storage
6crawlee_storage
7storage
8
9# installed files
10.venv
11
12# git folder
13.git

.editorconfig

1root = true
2
3[*]
4indent_style = space
5indent_size = 4
6charset = utf-8
7trim_trailing_whitespace = true
8insert_final_newline = true
9end_of_line = lf

.gitignore

1# This file tells Git which files shouldn't be added to source control
2
3.idea
4.DS_Store
5
6apify_storage
7storage
8
9.venv/
10.env/
11__pypackages__
12dist/
13build/
14*.egg-info/
15*.egg
16
17__pycache__
18
19.mypy_cache
20.dmypy.json
21dmypy.json
22.pytest_cache
23
24.scrapy
25*.log
26
27storage

requirements.txt

1# Add your dependencies here.
2# See https://pip.pypa.io/en/latest/reference/requirements-file-format/
3# for how to format them
4apify ~= 1.0.0
5openai ~= 0.27.2
Developer
Maintained by Community
Actor metrics
  • 5 monthly users
  • 100.0% runs succeeded
  • 0.0 days response time
  • Created in Mar 2023
  • Modified about 1 year ago
Categories