flake8 compliant
This commit is contained in:
+33
-17
@@ -25,6 +25,11 @@ from src.model_textual import (
|
||||
SalienceModelOutput
|
||||
)
|
||||
|
||||
|
||||
class NoDocumentFoundException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ModelOutputs(BaseModel):
|
||||
visual_syntax: VisualSyntaxModelOutput
|
||||
contact: ContactModelOutput
|
||||
@@ -52,7 +57,7 @@ class ModelOutputs(BaseModel):
|
||||
in cls.model_fields.items()
|
||||
}
|
||||
return cls(**kwargs)
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_annotations(
|
||||
cls,
|
||||
@@ -70,17 +75,28 @@ class ModelOutputs(BaseModel):
|
||||
) -> ModelOutputs:
|
||||
"""Instantiate from annotation."""
|
||||
kwargs = {
|
||||
"visual_syntax": VisualSyntaxModelOutput.from_choice(visual_syntax),
|
||||
"contact": ContactModelOutput.from_choice(contact),
|
||||
"angle": AngleModelOutput.from_choice(angle),
|
||||
"point_of_view": PointOfViewModelOutput.from_choice(point_of_view),
|
||||
"distance": DistanceModelOutput.from_choice(distance),
|
||||
"modality_lighting": ModalityLightingModelOutput.from_choice(modality_lighting),
|
||||
"modality_color": ModalityColorModelOutput.from_choice(modality_color),
|
||||
"modality_depth": ModalityDepthModelOutput.from_choice(modality_depth),
|
||||
"information_value": InformationValueModelOutput.from_choice(information_value),
|
||||
"framing": FramingModelOutput.from_choice(framing),
|
||||
"salience": SalienceModelOutput.from_choice(salience)
|
||||
"visual_syntax": VisualSyntaxModelOutput
|
||||
.from_choice(visual_syntax),
|
||||
"contact": ContactModelOutput
|
||||
.from_choice(contact),
|
||||
"angle": AngleModelOutput
|
||||
.from_choice(angle),
|
||||
"point_of_view": PointOfViewModelOutput
|
||||
.from_choice(point_of_view),
|
||||
"distance": DistanceModelOutput
|
||||
.from_choice(distance),
|
||||
"modality_lighting": ModalityLightingModelOutput
|
||||
.from_choice(modality_lighting),
|
||||
"modality_color": ModalityColorModelOutput
|
||||
.from_choice(modality_color),
|
||||
"modality_depth": ModalityDepthModelOutput
|
||||
.from_choice(modality_depth),
|
||||
"information_value": InformationValueModelOutput
|
||||
.from_choice(information_value),
|
||||
"framing": FramingModelOutput
|
||||
.from_choice(framing),
|
||||
"salience": SalienceModelOutput
|
||||
.from_choice(salience)
|
||||
}
|
||||
return cls(**kwargs)
|
||||
|
||||
@@ -115,7 +131,10 @@ class VisualCommunication(BaseModel):
|
||||
|
||||
@field_validator("image", mode="before")
|
||||
@classmethod
|
||||
def convert_to_image(cls, image: Image.Image | BytesIO | bytes) -> Image.Image:
|
||||
def convert_to_image(
|
||||
cls,
|
||||
image: Image.Image | BytesIO | bytes
|
||||
) -> Image.Image:
|
||||
if isinstance(image, bytes):
|
||||
image = BytesIO(image)
|
||||
if isinstance(image, BytesIO):
|
||||
@@ -124,7 +143,7 @@ class VisualCommunication(BaseModel):
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"{self.classname()}(name='{self.name}')"
|
||||
|
||||
|
||||
def webencoded_image(self) -> str:
|
||||
"""Convert image to be displayed on webpage."""
|
||||
# convert images to bytes string
|
||||
@@ -138,6 +157,3 @@ class VisualCommunication(BaseModel):
|
||||
if not force and self.prediction is not None:
|
||||
logging.warning("set force=True to overwrite existing values.")
|
||||
self.prediction = ModelOutputs.from_random()
|
||||
|
||||
class NoDocumentFoundException(Exception):
|
||||
pass
|
||||
|
||||
+20
-8
@@ -20,15 +20,17 @@ def total_annotated(
|
||||
) -> int:
|
||||
"""Get total number of annotated documents in database."""
|
||||
query = {
|
||||
"annotation": { "$ne": None }
|
||||
"annotation": {
|
||||
"$ne": None
|
||||
}
|
||||
}
|
||||
return collection.count_documents(filter=query)
|
||||
|
||||
|
||||
def get_visual_communication(
|
||||
collection: Collection,
|
||||
with_annotation: bool = False
|
||||
) -> VisualCommunication:
|
||||
collection: Collection,
|
||||
with_annotation: bool = False
|
||||
) -> VisualCommunication:
|
||||
"""Get a random visual communication from the database."""
|
||||
query = {}
|
||||
if with_annotation:
|
||||
@@ -36,8 +38,14 @@ def get_visual_communication(
|
||||
else:
|
||||
query["annotation"] = None
|
||||
data = collection.aggregate([
|
||||
{ "$match": query }, # find using filters
|
||||
{ "$sample": { "size": 1 } } # get one random
|
||||
{
|
||||
"$match": query # find using filters
|
||||
},
|
||||
{
|
||||
"$sample": {
|
||||
"size": 1 # get one random
|
||||
}
|
||||
}
|
||||
])
|
||||
data = list(data) # read data from cursor object
|
||||
if len(data) == 0:
|
||||
@@ -58,7 +66,9 @@ def upsert_predictions(
|
||||
"name": vis_com_name
|
||||
}
|
||||
update = {
|
||||
"$set": { "prediction": predictions.model_dump() }
|
||||
"$set": {
|
||||
"prediction": predictions.model_dump()
|
||||
}
|
||||
}
|
||||
res = collection.update_one(
|
||||
filter=query,
|
||||
@@ -79,7 +89,9 @@ def upsert_annotations(
|
||||
"name": vis_com_name
|
||||
}
|
||||
update = {
|
||||
"$set": { "annotation": annotations.model_dump() }
|
||||
"$set": {
|
||||
"annotation": annotations.model_dump()
|
||||
}
|
||||
}
|
||||
res = collection.update_one(
|
||||
filter=query,
|
||||
|
||||
Reference in New Issue
Block a user