Giter Club home page Giter Club logo

ai-generated-magic-cards's Introduction

ai-generated-magic-cards

Tools for encoding Magic: The Gathering cards into a form suitable for AI text generation with tools such as aitextgen.

In contrast to mtgencode, which encodes Magic cards intended for a character-based RNN training method, this form of encoding is much lighter since byte-pair encoding and Transformers can learn a model so well such that mtgencode is overkill. Additionally, this level of encoding can extend to future Magic sets, while many new mechanics introduced since mtgencode stopped development in 2017 make it dated.

This was used to create the dataset to train the GPT-2 model for Create Your Own AI-Generated Magic: The Gathering Cards.

Preparation

These scripts use the AllPrintingsCSVFiles download from MTGJSON. Download and unzip, then point the INPUT_FILE in the scripts to the contained cards.csv.

Additionally, you'll need to install jinja2 to run the scripts:

pip3 install jinja2

mtgjson_parser.py

mtgjson_encoder.py is a lighter-templating that renders the cards in a style similar to that of Scryfall. Although you can train AI on it and the output will be more interpretable without decoding, it's less flexible.

Examples of cards with this encoding:

Ballista Squad {3}{W}
Creature — Human Rebel
{X}{W}, {T}: Ballista Squad deals X damage to target attacking or blocking creature.
2/2
---
The perfect antidote for a tightly packed formation.
Agonizing Memories {2}{B}{B}
Sorcery
Look at target player's hand and choose two cards from it. Put them on top of that player's library in any order.
---
In the aftermath of war, when the slaying is long done, the greatest miseries come home to roost.
Karn Liberated {7}
Legendary Planeswalker — Karn
+4: Target player exiles a card from their hand.
−3: Exile target permanent.
−14: Restart the game, leaving in exile all non-Aura permanent cards exiled with Karn Liberated. Then put those cards onto the battlefield under your control.
Loyalty: 6

mtgencoder.py

mtgjson_encoder.py encodes Magic cards into a similar format as mtgencode, although only in two major ways:

  1. Cards are encoded with the following sections/keys: <|name|>, <|manaCost|>, <|type|>, <|text|>, <|power|>, <|toughness|>, <|loyalty|>. These keys are randomized, allowing the cards to be generated in any order. All the encoded cards are repeated in a different order for REPETITION times (10 in the script); all repetitions are shuffled again to mitigate data leakage.
  2. If the card name is present in the text; it is replaced with a ~; this is mostly to prevent data leakage where the <|text|> appears before the <|name|>

Examples of encoded cards:

<|toughness|><|text|>Counter target spell unless its controller pays {X}.<|power|><|type|>Instant<|loyalty|><|manaCost|>{X}{U}<|name|>Clash of Wills
<|loyalty|><|text|>~ enters the battlefield tapped.
{T}: Add {C}.
{T}: Add {U} or {R}. ~ deals 1 damage to you.<|toughness|><|name|>Caldera Lake<|power|><|manaCost|><|type|>Land
<|loyalty|>5<|text|>+1: Scry 1, then draw a card.
−2: Return target creature to its owner's hand.
−8: You get an emblem with "Whenever an opponent casts their first spell each turn, counter that spell."<|name|>Jace, Unraveler of Secrets<|toughness|><|type|>Legendary Planeswalker — Jace<|manaCost|>{3}{U}{U}<|power|>

The AI Model

The model itself takes advantage of efficient tokenization and aitextgen's schema capabilities. The keys such as <|name|> are explicitly added tokens to the tokeniziation; therefore they get their own token_id and are faster to explicitly detect/generate without confounding. These added tokens are also schema_tokens, which when used with generate(schema=True), returns a dictionary with each field as its own key, allowing it to be passed to a jinja2 template for decoding without additional decoding.

Maintainer/Creator

Max Woolf (@minimaxir)

Max's open-source projects are supported by his Patreon and GitHub Sponsors. If you found this project helpful, any monetary contributions to the Patreon are appreciated and will be put to good creative use.

License

MIT

ai-generated-magic-cards's People

Contributors

minimaxir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

phongtnit

ai-generated-magic-cards's Issues

Failure to install

When downloading and setting up in the cell on the notebook (in a new copy) I`m getting the following error

`---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
in
5 import sys
6 from jinja2 import Template
----> 7 from aitextgen import aitextgen
8
9 try:

2 frames
/usr/local/lib/python3.9/dist-packages/aitextgen/train.py in
12 import pytorch_lightning as pl
13 from pytorch_lightning.callbacks.progress import ProgressBarBase
---> 14 from pytorch_lightning.utilities import _TPU_AVAILABLE
15
16

ImportError: cannot import name '_TPU_AVAILABLE' from 'pytorch_lightning.utilities' (/usr/local/lib/python3.9/dist-packages/pytorch_lightning/utilities/init.py)


NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.