GPT-2 text generation avatar

GPT-2 text generation

Try for free

No credit card required

View all Actors
GPT-2 text generation

GPT-2 text generation

jirimoravcik/gpt2-text-generation
Try for free

No credit card required

This actor uses the GPT-2 language model to generate text.

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 os
2from transformers import pipeline
3from apify import Actor
4
5async def main():
6    async with Actor:
7        actor_input = await Actor.get_input() or {}
8
9        generator = pipeline('text-generation', model='gpt2')
10        output = generator(actor_input["prompt"], max_length=actor_input["max_length"], num_return_sequences=1)
11
12        await Actor.push_data(output)

.dockerignore

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

Dockerfile

1FROM huggingface/transformers-pytorch-gpu
2
3COPY requirements.txt ./
4
5RUN echo "Python version:" \
6 && python3 --version \
7 && echo "Pip version:" \
8 && pip --version \
9 && echo "Installing dependencies from requirements.txt:" \
10 && pip install -r requirements.txt \
11 && echo "All installed Python packages:" \
12 && pip freeze
13
14COPY . ./
15
16RUN python3 -c 'from transformers import pipeline; generator = pipeline("text-generation", model="gpt2")'
17
18CMD ["python3", "-m", "src"]

INPUT_SCHEMA.json

1{
2  "title": "Generate text",
3  "description": "This is actor input schema",
4  "type": "object",
5  "schemaVersion": 1,
6  "properties": {
7    "prompt": {
8      "title": "Initial prompt",
9      "type": "string",
10      "description": "The initial prompt used as basis for text generation",
11      "default": "My name is Apify and I like to",
12      "editor": "textarea"
13    },
14    "max_length": {
15      "title": "Maximum sequence length",
16      "type": "integer",
17      "description": "The maximum length of the generated sequence",
18      "default": 30,
19      "editor": "number"
20    }
21  },
22  "required": [
23    "prompt",
24    "max_length"
25  ]
26}

requirements.txt

1# Add your dependencies here.
2# See https://pip.pypa.io/en/latest/cli/pip_install/#requirements-file-format
3# for how to format them
4
5apify ~= 1.0.0
Developer
Maintained by Community
Actor metrics
  • 2 monthly users
  • 3 stars
  • Created in Nov 2022
  • Modified over 1 year ago
Categories