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

Update pydantic to 2.7.4 #298

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions edenai_apis/features/image/explicit_content/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ACategoryType(str):
pass


class CategoryType(ACategoryType, Enum):
class CategoryType(str, Enum):
"""This enum are used to categorize the explicit content extracted from the text"""

Toxic = "Toxic"
Expand All @@ -37,7 +37,7 @@ def list_available_type(cls):
return [category for category in cls]

@classmethod
def list_choices(cls) -> Dict["ACategoryType", SubCategoryBase]:
def list_choices(cls) -> Dict[str, SubCategoryBase]:
return {
cls.Toxic: ToxicSubCategoryType,
cls.Content: ContentSubCategoryType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
SafeSubCategoryType,
OtherSubCategoryType,
)
from edenai_apis.utils.combine_enums import combine_enums

SubCategoryType = Union[

SubCategoryType = combine_enums(
"SubCategoryType",
ToxicSubCategoryType,
ContentSubCategoryType,
SexualSubCategoryType,
Expand All @@ -33,7 +36,7 @@
HateAndExtremismSubCategoryType,
SafeSubCategoryType,
OtherSubCategoryType,
]
)


class ExplicitItem(BaseModel):
Expand Down
32 changes: 25 additions & 7 deletions edenai_apis/features/text/anonymization/anonymization_dataclass.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from typing import Dict, Optional, Sequence, Union
from pydantic import BaseModel, ConfigDict, Field, FieldSerializationInfo, field_serializer, field_validator, model_validator
from pydantic import (
BaseModel,
ConfigDict,
Field,
FieldSerializationInfo,
field_serializer,
field_validator,
model_validator,
)

from edenai_apis.features.text.anonymization.category import (
CategoryType,
Expand All @@ -16,8 +24,15 @@
)
from typing import Dict, Optional, Sequence, Union

from pydantic import BaseModel, ConfigDict, Field, FieldSerializationInfo, field_serializer, field_validator, \
model_validator
from pydantic import (
BaseModel,
ConfigDict,
Field,
FieldSerializationInfo,
field_serializer,
field_validator,
model_validator,
)

from edenai_apis.features.text.anonymization.category import (
CategoryType,
Expand All @@ -32,8 +47,11 @@
LocationInformationSubCategoryType,
OtherSubCategoryType,
)
from edenai_apis.utils.combine_enums import combine_enums

SubCategoryType = Union[

SubCategoryType = combine_enums(
"SubCategoryType",
FinancialInformationSubCategoryType,
PersonalInformationSubCategoryType,
IdentificationNumbersSubCategoryType,
Expand All @@ -42,7 +60,7 @@
DateAndTimeSubCategoryType,
LocationInformationSubCategoryType,
OtherSubCategoryType,
]
)


class AnonymizationEntity(BaseModel):
Expand Down Expand Up @@ -94,9 +112,9 @@ def round_confidence_score(cls, v):
return round(v, 3)
return v

@field_serializer('subcategory', mode="plain", when_used="always")
@field_serializer("subcategory", mode="plain", when_used="always")
def serialize_subcategory(self, value: SubCategoryType, _: FieldSerializationInfo):
return getattr(value, 'value', None)
return getattr(value, "value", None)


class AnonymizationDataClass(BaseModel):
Expand Down
8 changes: 2 additions & 6 deletions edenai_apis/features/text/anonymization/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
)


class ACategoryType(str):
pass


class CategoryType(ACategoryType, Enum):
class CategoryType(str, Enum):
"""This enum are used to categorize the entities extracted from the text."""

PersonalInformation = "PersonalInformation"
Expand All @@ -35,7 +31,7 @@ def list_available_type(cls):
return [category for category in cls]

@classmethod
def list_choices(cls) -> Dict["ACategoryType", SubCategoryBase]:
def list_choices(cls) -> Dict[str, SubCategoryBase]:
return {
cls.PersonalInformation: PersonalInformationSubCategoryType,
cls.FinancialInformation: FinancialInformationSubCategoryType,
Expand Down
8 changes: 2 additions & 6 deletions edenai_apis/features/text/moderation/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
)


class ACategoryType(str):
pass


class CategoryType(ACategoryType, Enum):
class CategoryType(str, Enum):
"""This enum are used to categorize the explicit content extracted from the text"""

Toxic = "Toxic"
Expand All @@ -37,7 +33,7 @@ def list_available_type(cls):
return [category for category in cls]

@classmethod
def list_choices(cls) -> Dict["ACategoryType", SubCategoryBase]:
def list_choices(cls) -> Dict[str, SubCategoryBase]:
return {
cls.Toxic: ToxicSubCategoryType,
cls.Content: ContentSubCategoryType,
Expand Down
28 changes: 20 additions & 8 deletions edenai_apis/features/text/moderation/moderation_dataclass.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from enum import Enum
from typing import Sequence, Union
from typing import Sequence, Union, Type

from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator, FieldSerializationInfo, field_serializer
from pydantic import (
BaseModel,
ConfigDict,
Field,
StrictStr,
field_validator,
FieldSerializationInfo,
field_serializer,
)

from edenai_apis.features.text.moderation.category import (
CategoryType,
Expand All @@ -17,8 +25,10 @@
SafeSubCategoryType,
OtherSubCategoryType,
)
from edenai_apis.utils.combine_enums import combine_enums

SubCategoryType = Union[
SubCategoryType = combine_enums(
"SubCategoryType",
ToxicSubCategoryType,
ContentSubCategoryType,
SexualSubCategoryType,
Expand All @@ -28,7 +38,8 @@
HateAndExtremismSubCategoryType,
SafeSubCategoryType,
OtherSubCategoryType,
]
)

class TextModerationCategoriesMicrosoftEnum(Enum):
Category1 = "sexually explicit"
Category2 = "sexually suggestive"
Expand All @@ -43,14 +54,16 @@ class TextModerationItem(BaseModel):
likelihood_score: float

model_config = ConfigDict(use_enum_values=True)
@field_serializer('subcategory', mode="plain", when_used="always")

@field_serializer("subcategory", mode="plain", when_used="always")
def serialize_subcategory(self, value: SubCategoryType, _: FieldSerializationInfo):
return getattr(value, 'value', None)
return getattr(value, "value", None)


class ModerationDataClass(BaseModel):
nsfw_likelihood: int
items: Sequence[TextModerationItem] = Field(default_factory=list)
nsfw_likelihood_score : float
nsfw_likelihood_score: float

@field_validator("nsfw_likelihood")
@classmethod
Expand All @@ -59,7 +72,6 @@ def check_min_max(cls, value):
raise ValueError("Likelihood walue should be between 0 and 5")
return value


@staticmethod
def calculate_nsfw_likelihood(items: Sequence[TextModerationItem]):
if len(items) == 0:
Expand Down
11 changes: 10 additions & 1 deletion edenai_apis/features/text/moderation/subcategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def choose_label(cls, label: str) -> "SubCategoryBase":
f"Unknown label {label}. Only {cls.list_choices().values()} are allowed."
)


class ToxicSubCategoryType(SubCategoryBase, Enum):
Insult = "Insult"
Obscene = "Obscene"
Expand All @@ -57,6 +58,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.Toxic: SubCategoryPattern.Toxic.TOXIC,
}


class ContentSubCategoryType(SubCategoryBase, Enum):
MiddleFinger = "MiddleFinger"
PublicSafety = "PublicSafety"
Expand All @@ -80,6 +82,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.Legal: SubCategoryPattern.Content.LEGAL,
}


class SexualSubCategoryType(SubCategoryBase, Enum):
SexualActivity = "SexualActivity"
SexualSituations = "SexualSituations"
Expand All @@ -103,6 +106,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.Sexual: SubCategoryPattern.Sexual.SEXUAL,
}


class ViolenceSubCategoryType(SubCategoryBase, Enum):
GraphicViolenceOrGore = "GraphicViolenceOrGore"
PhysicalViolence = "PhysicalViolence"
Expand All @@ -118,6 +122,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.Violence: SubCategoryPattern.Violence.VIOLENCE,
}


class DrugAndAlcoholSubCategoryType(SubCategoryBase, Enum):
DrugProducts = "DrugProducts"
DrugUse = "DrugUse"
Expand All @@ -137,6 +142,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.Drinking: SubCategoryPattern.DrugAndAlcohol.DRINKING,
}


class FinanceSubCategoryType(SubCategoryBase, Enum):
Gambling = "Gambling"
Finance = "Finance"
Expand All @@ -150,6 +156,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.MoneyContent: SubCategoryPattern.Finance.MONEY_CONTENT,
}


class HateAndExtremismSubCategoryType(SubCategoryBase, Enum):
Hate = "Hate"
Harassment = "Harassment"
Expand All @@ -167,6 +174,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.Racy: SubCategoryPattern.HateAndExtremism.RACY,
}


class SafeSubCategoryType(SubCategoryBase, Enum):
Safe = "Safe"
NotSafe = "NotSafe"
Expand All @@ -178,6 +186,7 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.NotSafe: SubCategoryPattern.Safe.NOT_SAFE,
}


class OtherSubCategoryType(SubCategoryBase, Enum):
Spoof = "Spoof"
Religion = "Religion"
Expand All @@ -191,4 +200,4 @@ def list_choices(cls) -> Dict["SubCategoryBase", List[str]]:
cls.Religion: SubCategoryPattern.Other.RELIGION,
cls.Offensive: SubCategoryPattern.Other.OFFENSIVE,
cls.Other: SubCategoryPattern.Other.OTHER,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,30 @@ class TestTextModeration:
),
],
)
def test_valid_value_check_min_mac_nsfw(self, nsfw_likelihood, nsfw_likelihood_score):
def test_valid_value_check_min_mac_nsfw(
self, nsfw_likelihood, nsfw_likelihood_score
):
try:
ModerationDataClass(nsfw_likelihood=nsfw_likelihood, items=[], nsfw_likelihood_score=nsfw_likelihood_score)
ModerationDataClass(
nsfw_likelihood=nsfw_likelihood,
items=[],
nsfw_likelihood_score=nsfw_likelihood_score,
)
except ValueError:
pytest.fail(f"{nsfw_likelihood} value doesn't raises a ValueError")

def test_text_moderation_items(self):

ModerationDataClass(
nsfw_likelihood=0,
items=[
{
"label": "hi",
"category": "Toxic",
"likelihood": 1,
"subcategory": "Toxic",
"likelihood_score": 0.5,
}
],
nsfw_likelihood_score=0.0,
)
2 changes: 1 addition & 1 deletion edenai_apis/tests/test_providers_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from apis.amazon.errors import ERRORS as amazon_errors
from apis.google.errors import ERRORS as google_errors
from apis.ibm.errors import ERRORS as ibm_errors

from apis.microsoft.errors import ERRORS as microsoft_errors
from features.audio.speech_to_text_async.speech_to_text_async_args import (
data_path as audio_data_path,
Expand Down
8 changes: 8 additions & 0 deletions edenai_apis/utils/combine_enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Type
from enum import Enum

def combine_enums(name: str, *enum_classes: Type[Enum]) -> Enum:
combined = {}
for enum_class in enum_classes:
combined.update(enum_class.__members__)
return Enum(name, combined)
4 changes: 2 additions & 2 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pandas
pdf2image
pillow
pycountry
pydantic
pydantic==2.7.4
pylint
mypy
pydub
Expand Down Expand Up @@ -63,7 +63,7 @@ azure-core
aleph_alpha_client==6.0.0

# openai
openai==1.46.0
openai==1.55.3

python-dotenv

Expand Down
Loading
Loading