Added Set Name
Browse filesAdded a short descriptive name for the newly generated set of MTG cards.
- mtg_schemas.py +82 -81
mtg_schemas.py
CHANGED
|
@@ -1,82 +1,83 @@
|
|
| 1 |
-
from typing import List, Literal, Annotated, Optional
|
| 2 |
-
from pydantic import BaseModel, Field, ValidationError, field_validator, conint, StringConstraints
|
| 3 |
-
|
| 4 |
-
# StrEnum does not come with Python 3.10, and we need to use Python 3.10 because that's what Hugging Face Spaces uses.
|
| 5 |
-
# Create a compatible fallback
|
| 6 |
-
#from enum import c
|
| 7 |
-
|
| 8 |
-
import sys
|
| 9 |
-
|
| 10 |
-
if sys.version_info >= (3, 11):
|
| 11 |
-
# Python 3.11+ has StrEnum built-in
|
| 12 |
-
from enum import StrEnum
|
| 13 |
-
else:
|
| 14 |
-
# For Python 3.10 and below, create a compatible fallback
|
| 15 |
-
from enum import Enum
|
| 16 |
-
|
| 17 |
-
class StrEnum(str, Enum):
|
| 18 |
-
"""Compatibility fallback for Python < 3.11."""
|
| 19 |
-
pass
|
| 20 |
-
|
| 21 |
-
import base64
|
| 22 |
-
from io import BytesIO
|
| 23 |
-
from PIL import Image
|
| 24 |
-
|
| 25 |
-
class YesNoAnswer(StrEnum):
|
| 26 |
-
Yes = "Yes"
|
| 27 |
-
No = "No"
|
| 28 |
-
|
| 29 |
-
class YesNoName(BaseModel):
|
| 30 |
-
YesNo: YesNoAnswer = Field(description="A Yes or No Answer.")
|
| 31 |
-
Name: str = Field(default="", description="The name (may be empty).")
|
| 32 |
-
|
| 33 |
-
# Define a structured output for a list of YesNoName objects.
|
| 34 |
-
class YesNoNameList(BaseModel):
|
| 35 |
-
items: List[YesNoName]
|
| 36 |
-
|
| 37 |
-
# Used to enforce uniqueness of new card name.
|
| 38 |
-
# We don't want to re-use a card name that has already been used by a pre-existing card.
|
| 39 |
-
class MTGNameOnly(BaseModel):
|
| 40 |
-
Name: str
|
| 41 |
-
|
| 42 |
-
# Regex: one or more tokens; each token is { <digits> | X | R | U | W | G | B }
|
| 43 |
-
# ManaCost definition (from earlier)
|
| 44 |
-
ManaCost = Optional[
|
| 45 |
-
Annotated[str, StringConstraints(pattern=r'^(?:\{(?:[0-9]+|[XRUWGB])\})+$')]
|
| 46 |
-
]
|
| 47 |
-
|
| 48 |
-
# These are the Subtypes that I found MTG card information downloaded from MTGJSON (https://mtgjson.com/)
|
| 49 |
-
# Obviously, some of them are rather niche.
|
| 50 |
-
class SubtypeEnum(StrEnum):
|
| 51 |
-
Legendary = "Legendary"
|
| 52 |
-
Basic = "Basic"
|
| 53 |
-
Snow = "Snow"
|
| 54 |
-
BasicSnow = "Basic, Snow"
|
| 55 |
-
World = "World"
|
| 56 |
-
LegendarySnow = "Legendary, Snow"
|
| 57 |
-
Host = "Host"
|
| 58 |
-
Ongoing = "Ongoing"
|
| 59 |
-
NoneType = "None" # sentinel if no subtype
|
| 60 |
-
|
| 61 |
-
class MTGCard(BaseModel):
|
| 62 |
-
# simple strings
|
| 63 |
-
Name: str
|
| 64 |
-
Supertype: Optional[SubtypeEnum] = None
|
| 65 |
-
Type: str
|
| 66 |
-
Subtype: str
|
| 67 |
-
Keywords: str
|
| 68 |
-
Text: str
|
| 69 |
-
FlavorText: Optional[str] = ""
|
| 70 |
-
|
| 71 |
-
# constrained fields
|
| 72 |
-
Colors: Optional[List[Literal['R', 'U', 'W', 'G', 'B']]] = None
|
| 73 |
-
ManaCost: ManaCost
|
| 74 |
-
|
| 75 |
-
# Power/toughness may be absent for non-creatures
|
| 76 |
-
Power: Optional[conint(gt=0)] = None
|
| 77 |
-
Toughness: Optional[conint(gt=0)] = None
|
| 78 |
-
|
| 79 |
-
# Define a structured output for a list of MTG cards.
|
| 80 |
-
class MTGCardList(BaseModel):
|
| 81 |
-
|
|
|
|
| 82 |
explanation: str
|
|
|
|
| 1 |
+
from typing import List, Literal, Annotated, Optional
|
| 2 |
+
from pydantic import BaseModel, Field, ValidationError, field_validator, conint, StringConstraints
|
| 3 |
+
|
| 4 |
+
# StrEnum does not come with Python 3.10, and we need to use Python 3.10 because that's what Hugging Face Spaces uses.
|
| 5 |
+
# Create a compatible fallback
|
| 6 |
+
#from enum import c
|
| 7 |
+
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
if sys.version_info >= (3, 11):
|
| 11 |
+
# Python 3.11+ has StrEnum built-in
|
| 12 |
+
from enum import StrEnum
|
| 13 |
+
else:
|
| 14 |
+
# For Python 3.10 and below, create a compatible fallback
|
| 15 |
+
from enum import Enum
|
| 16 |
+
|
| 17 |
+
class StrEnum(str, Enum):
|
| 18 |
+
"""Compatibility fallback for Python < 3.11."""
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
import base64
|
| 22 |
+
from io import BytesIO
|
| 23 |
+
from PIL import Image
|
| 24 |
+
|
| 25 |
+
class YesNoAnswer(StrEnum):
|
| 26 |
+
Yes = "Yes"
|
| 27 |
+
No = "No"
|
| 28 |
+
|
| 29 |
+
class YesNoName(BaseModel):
|
| 30 |
+
YesNo: YesNoAnswer = Field(description="A Yes or No Answer.")
|
| 31 |
+
Name: str = Field(default="", description="The name (may be empty).")
|
| 32 |
+
|
| 33 |
+
# Define a structured output for a list of YesNoName objects.
|
| 34 |
+
class YesNoNameList(BaseModel):
|
| 35 |
+
items: List[YesNoName]
|
| 36 |
+
|
| 37 |
+
# Used to enforce uniqueness of new card name.
|
| 38 |
+
# We don't want to re-use a card name that has already been used by a pre-existing card.
|
| 39 |
+
class MTGNameOnly(BaseModel):
|
| 40 |
+
Name: str
|
| 41 |
+
|
| 42 |
+
# Regex: one or more tokens; each token is { <digits> | X | R | U | W | G | B }
|
| 43 |
+
# ManaCost definition (from earlier)
|
| 44 |
+
ManaCost = Optional[
|
| 45 |
+
Annotated[str, StringConstraints(pattern=r'^(?:\{(?:[0-9]+|[XRUWGB])\})+$')]
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# These are the Subtypes that I found MTG card information downloaded from MTGJSON (https://mtgjson.com/)
|
| 49 |
+
# Obviously, some of them are rather niche.
|
| 50 |
+
class SubtypeEnum(StrEnum):
|
| 51 |
+
Legendary = "Legendary"
|
| 52 |
+
Basic = "Basic"
|
| 53 |
+
Snow = "Snow"
|
| 54 |
+
BasicSnow = "Basic, Snow"
|
| 55 |
+
World = "World"
|
| 56 |
+
LegendarySnow = "Legendary, Snow"
|
| 57 |
+
Host = "Host"
|
| 58 |
+
Ongoing = "Ongoing"
|
| 59 |
+
NoneType = "None" # sentinel if no subtype
|
| 60 |
+
|
| 61 |
+
class MTGCard(BaseModel):
|
| 62 |
+
# simple strings
|
| 63 |
+
Name: str
|
| 64 |
+
Supertype: Optional[SubtypeEnum] = None
|
| 65 |
+
Type: str
|
| 66 |
+
Subtype: str
|
| 67 |
+
Keywords: str
|
| 68 |
+
Text: str
|
| 69 |
+
FlavorText: Optional[str] = ""
|
| 70 |
+
|
| 71 |
+
# constrained fields
|
| 72 |
+
Colors: Optional[List[Literal['R', 'U', 'W', 'G', 'B']]] = None
|
| 73 |
+
ManaCost: ManaCost
|
| 74 |
+
|
| 75 |
+
# Power/toughness may be absent for non-creatures
|
| 76 |
+
Power: Optional[conint(gt=0)] = None
|
| 77 |
+
Toughness: Optional[conint(gt=0)] = None
|
| 78 |
+
|
| 79 |
+
# Define a structured output for a list of MTG cards.
|
| 80 |
+
class MTGCardList(BaseModel):
|
| 81 |
+
Name: str = Field(description="A short descriptive name for the newly generated set of MTG cards.")
|
| 82 |
+
cards: List[MTGCard]
|
| 83 |
explanation: str
|