Skip to content

Commit

Permalink
[Hotfix] Friendly Image and Audio artifact names (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfrench authored Nov 7, 2024
1 parent 9274e34 commit f9c0918
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## \[0.34.2\] - 2024-11-07

### Fixed

- Restore human-friendly default `ImageArtifact` and `AudioArtifact` names with file type extension.

## \[0.34.1\] - 2024-11-05

### Added
Expand Down
16 changes: 16 additions & 0 deletions griptape/artifacts/audio_artifact.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

import random
import string
import time

from attrs import define, field

from griptape.artifacts import BlobArtifact
Expand All @@ -15,6 +19,12 @@ class AudioArtifact(BlobArtifact):

format: str = field(kw_only=True, metadata={"serializable": True})

def __attrs_post_init__(self) -> None:
# Generating the name string requires attributes set by child classes.
# This waits until all attributes are available before generating a name.
if self.name == self.id:
self.name = self.make_name()

@property
def mime_type(self) -> str:
return f"audio/{self.format}"
Expand All @@ -24,3 +34,9 @@ def to_bytes(self) -> bytes:

def to_text(self) -> str:
return f"Audio, format: {self.format}, size: {len(self.value)} bytes"

def make_name(self) -> str:
entropy = "".join(random.choices(string.ascii_lowercase + string.digits, k=4))
fmt_time = time.strftime("%y%m%d%H%M%S", time.localtime())

return f"audio_artifact_{fmt_time}_{entropy}.{self.format}"
16 changes: 16 additions & 0 deletions griptape/artifacts/image_artifact.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

import random
import string
import time

from attrs import define, field

from griptape.artifacts import BlobArtifact
Expand All @@ -19,6 +23,12 @@ class ImageArtifact(BlobArtifact):
width: int = field(kw_only=True, metadata={"serializable": True})
height: int = field(kw_only=True, metadata={"serializable": True})

def __attrs_post_init__(self) -> None:
# Generating the name string requires attributes set by child classes.
# This waits until all attributes are available before generating a name.
if self.name == self.id:
self.name = self.make_name()

@property
def mime_type(self) -> str:
return f"image/{self.format}"
Expand All @@ -28,3 +38,9 @@ def to_bytes(self) -> bytes:

def to_text(self) -> str:
return f"Image, format: {self.format}, size: {len(self.value)} bytes"

def make_name(self) -> str:
entropy = "".join(random.choices(string.ascii_lowercase + string.digits, k=4))
fmt_time = time.strftime("%y%m%d%H%M%S", time.localtime())

return f"image_artifact_{fmt_time}_{entropy}.{self.format}"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "griptape"
version = "0.34.1"
version = "0.34.2"
description = "Modular Python framework for LLM workflows, tools, memory, and data."
authors = ["Griptape <[email protected]>"]
license = "Apache 2.0"
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/artifacts/test_audio_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ def test_deserialization(self, audio_artifact):
assert deserialized_artifact.format == "pcm"
assert deserialized_artifact.meta["model"] == "provider/model"
assert deserialized_artifact.meta["prompt"] == "two words"

def test_name(self, audio_artifact):
assert audio_artifact.name.startswith("audio_artifact_")
assert audio_artifact.name.endswith(audio_artifact.format)
4 changes: 4 additions & 0 deletions tests/unit/artifacts/test_image_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ def test_deserialization(self, image_artifact):
assert deserialized_artifact.height == 512
assert deserialized_artifact.meta["model"] == "openai/dalle2"
assert deserialized_artifact.meta["prompt"] == "a cute cat"

def test_name(self, image_artifact):
assert image_artifact.name.startswith("image_artifact_")
assert image_artifact.name.endswith(image_artifact.format)

0 comments on commit f9c0918

Please sign in to comment.