Skip to content

Commit

Permalink
Replaced deprecated resources.path with resources.files
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Movchan committed Aug 13, 2024
1 parent 84d0752 commit dd1bad5
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 83 deletions.
10 changes: 4 additions & 6 deletions aana/core/chat/chat_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ def load_chat_template(chat_template_name: str) -> str:
Raises:
ValueError: If the chat template does not exist.
"""
with resources.path(
"aana.core.chat.templates", f"{chat_template_name}.jinja"
) as path:
if not path.exists():
raise ValueError(f"Chat template {chat_template_name} does not exist.") # noqa: TRY003
path = resources.files("aana.core.chat.templates") / f"{chat_template_name}.jinja"
if not path.exists():
raise ValueError(f"Chat template {chat_template_name} does not exist.") # noqa: TRY003

return path.read_text()
return path.read_text()


def apply_chat_template(
Expand Down
2 changes: 1 addition & 1 deletion aana/tests/db/datastore/test_video_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def dummy_video():
"""Creates a dummy video for testing."""
media_id = str(uuid.uuid4())
path = resources.path("aana.tests.files.videos", "squirrel.mp4")
path = resources.files("aana.tests.files.videos") / "squirrel.mp4"
video = Video(path=path, media_id=media_id)
return video

Expand Down
4 changes: 2 additions & 2 deletions aana/tests/deployments/test_hf_blip2_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ async def test_methods(self, setup_deployment, image_name):
handle = await AanaDeploymentHandle.create(handle_name)

expected_output_path = (
resources.path("aana.tests.files.expected", "")
resources.files("aana.tests.files.expected")
/ "hf_blip2"
/ f"{deployment_name}_{image_name}.json"
)

path = resources.path("aana.tests.files.images", image_name)
path = resources.files("aana.tests.files.images") / image_name
image = Image(path=path, save_on_disk=False, media_id=image_name)

output = await handle.generate(image=image)
Expand Down
4 changes: 2 additions & 2 deletions aana/tests/deployments/test_hf_pipeline_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ async def test_call(self, setup_deployment, image_name):
handle = await AanaDeploymentHandle.create(handle_name)

expected_output_path = (
resources.path("aana.tests.files.expected", "")
resources.files("aana.tests.files.expected")
/ "hf_pipeline"
/ f"{deployment_name}_{image_name}.json"
)
path = resources.path("aana.tests.files.images", image_name)
path = resources.files("aana.tests.files.images") / image_name
image = Image(path=path, save_on_disk=False, media_id=image_name)

output = await handle.call(images=image)
Expand Down
4 changes: 2 additions & 2 deletions aana/tests/deployments/test_idefics2_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ async def test_idefics2_deployment_chat(self, setup_deployment, prompt, image_na

prompt_hash = get_object_hash(prompt)
expected_output_path = (
resources.path("aana.tests.files.expected", "")
resources.files("aana.tests.files.expected")
/ "idefics"
/ f"{deployment_name}_{image_name}_{prompt_hash}.json"
)

image = Image(path=resources.path("aana.tests.files.images", image_name))
image = Image(path=resources.files("aana.tests.files.images") / image_name)
dialog = ImageChatDialog.from_prompt(prompt=prompt, images=[image])

# test chat method
Expand Down
2 changes: 1 addition & 1 deletion aana/tests/deployments/test_text_generation_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def test_text_generation_methods(

query_hash = get_object_hash(query)
expected_output_path = (
resources.path("aana.tests.files.expected", "")
resources.files("aana.tests.files.expected")
/ "text_generation"
/ f"{deployment_name}_{query_hash}.json"
)
Expand Down
4 changes: 2 additions & 2 deletions aana/tests/deployments/test_vad_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ async def test_vad(self, setup_deployment, audio_file):

audio_file_name = Path(audio_file).stem
expected_output_path = (
resources.path("aana.tests.files.expected", "")
resources.files("aana.tests.files.expected")
/ "vad"
/ f"{audio_file_name}.json"
)

path = resources.path("aana.tests.files.audios", audio_file)
path = resources.files("aana.tests.files.audios") / audio_file
assert path.exists(), f"Audio not found: {path}"

audio = Audio(path=path)
Expand Down
4 changes: 2 additions & 2 deletions aana/tests/deployments/test_whisper_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ async def test_transcribe(self, setup_deployment, audio_file):
handle = await AanaDeploymentHandle.create(handle_name)

expected_output_path = (
resources.path("aana.tests.files.expected", "")
resources.files("aana.tests.files.expected")
/ "whisper"
/ f"{deployment_name}_{audio_file}.json"
)

# Test transcribe method
path = resources.path("aana.tests.files.audios", audio_file)
path = resources.files("aana.tests.files.audios") / audio_file
assert path.exists(), f"Audio not found: {path}"
audio = Audio(path=path, media_id=audio_file)

Expand Down
20 changes: 10 additions & 10 deletions aana/tests/units/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_audio(audio_file):
"""Test that the audio can be created from path, url(pending), or content."""
# Test creation from a path
try:
path = resources.path("aana.tests.files.audios", audio_file)
path = resources.files("aana.tests.files.audios") / audio_file
audio = Audio(path=path, save_on_disk=False)
assert audio.path == path
assert audio.content is None
Expand All @@ -39,7 +39,7 @@ def test_audio(audio_file):

# Test creation from content
try:
path = resources.path("aana.tests.files.audios", audio_file)
path = resources.files("aana.tests.files.audios") / audio_file
content = path.read_bytes()
audio = Audio(content=content, save_on_disk=False)
assert audio.path is None
Expand All @@ -63,7 +63,7 @@ def test_save_audio(audio_file):
"""Test that save_on_disk works for audio."""
# Test that the audio is saved to disk when save_on_disk is True
try:
path = resources.path("aana.tests.files.audios", audio_file)
path = resources.files("aana.tests.files.audios") / audio_file
audio = Audio(path=path, save_on_disk=True)
assert audio.path == path
assert audio.content is None
Expand All @@ -88,7 +88,7 @@ def test_save_audio(audio_file):
def test_cleanup(audio_file):
"""Test that cleanup works for audios."""
try:
path = resources.path("aana.tests.files.audios", audio_file)
path = resources.files("aana.tests.files.audios") / audio_file
audio = Audio(path=path, save_on_disk=True)
assert audio.path.exists()
finally:
Expand All @@ -110,7 +110,7 @@ def test_at_least_one_input():
def test_extract_audio():
"""Test download_video and extract_audio."""
# Test VideoInput with video path (tests download_video and extract_audio): return audio bytes
path = resources.path("aana.tests.files.videos", "physicsworks.webm")
path = resources.files("aana.tests.files.videos") / "physicsworks.webm"
video_input = VideoInput(path=str(path))
video = download_video(video_input)
assert isinstance(video, Video)
Expand All @@ -125,7 +125,7 @@ def test_extract_audio():
assert audio.url is None

# Test audio from VideoInput (for audio input): return audio bytes
path = resources.path("aana.tests.files.audios", "physicsworks.wav")
path = resources.files("aana.tests.files.audios") / "physicsworks.wav"
video_input = VideoInput(path=str(path))
video = download_video(video_input)
assert isinstance(video, Video)
Expand All @@ -136,7 +136,7 @@ def test_extract_audio():
assert audio.url is None

# Test audio from VideoInput (for no audio channel): return empty bytes
path = resources.path("aana.tests.files.videos", "squirrel_no_audio.mp4")
path = resources.files("aana.tests.files.videos") / "squirrel_no_audio.mp4"
video_input = VideoInput(path=str(path))
video = download_video(video_input)
audio = extract_audio(video)
Expand All @@ -162,16 +162,16 @@ def test_extract_audio():
audio.cleanup()

# Test loading numpy from the Audio object
path = resources.path("aana.tests.files.audios", "physicsworks.wav")
path = resources.files("aana.tests.files.audios") / "physicsworks.wav"
audio = Audio(path=path, save_on_disk=True)
assert audio.get_numpy() is not None

path = resources.path("aana.tests.files.audios", "squirrel.wav")
path = resources.files("aana.tests.files.audios") / "squirrel.wav"
audio = Audio(path=path, save_on_disk=True)
assert audio.get_numpy().all() == 0

# Test content of Audio objects created in different ways from same source video is the same.
video_path = resources.path("aana.tests.files.videos", "physicsworks.webm")
video_path = resources.files("aana.tests.files.videos") / "physicsworks.webm"
video = Video(path=video_path)
extracted_audio_1 = extract_audio(video)

Expand Down
10 changes: 5 additions & 5 deletions aana/tests/units/test_frame_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_extract_frames_success(
video_name, extract_fps, fast_mode_enabled, expected_duration, expected_num_frames
):
"""Test that frames can be extracted from a video successfully."""
video_path = resources.path("aana.tests.files.videos", video_name)
video_path = resources.files("aana.tests.files.videos") / video_name
video = Video(path=video_path)
params = VideoParams(extract_fps=extract_fps, fast_mode_enabled=fast_mode_enabled)
result = extract_frames(video=video, params=params)
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_extract_frames_success(
)
def test_get_video_duration_success(video_name, expected_duration):
"""Test decord video duration."""
video_path = resources.path("aana.tests.files.videos", video_name)
video_path = resources.files("aana.tests.files.videos") / video_name
video = Video(path=video_path)
duration = get_video_duration(video=video)
assert isinstance(duration, float)
Expand All @@ -65,7 +65,7 @@ def test_get_video_duration_failure():
"""Test that duration cannot be extracted from a invalid video."""
# image file instead of video file will create Video object
# but will fail in get duration function
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
with pytest.raises(VideoReadingException):
invalid_video = Video(path=path)
get_video_duration(video=invalid_video)
Expand All @@ -88,7 +88,7 @@ def test_generate_frames_success(
generate_frames is a generator function that yields a dictionary
containing the frames, timestamps and duration of the video.
"""
video_path = resources.path("aana.tests.files.videos", video_name)
video_path = resources.files("aana.tests.files.videos") / video_name
video = Video(path=video_path)
params = VideoParams(extract_fps=extract_fps, fast_mode_enabled=fast_mode_enabled)
gen_frame = generate_frames(video=video, params=params, batch_size=1)
Expand All @@ -115,7 +115,7 @@ def test_extract_frames_failure():
"""Test that frames cannot be extracted from a non-existent video."""
# image file instead of video file will create Video object
# but will fail in extract_frames
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
with pytest.raises(VideoReadingException):
invalid_video = Video(path=path)
params = VideoParams(extract_fps=1.0, fast_mode_enabled=False)
Expand Down
12 changes: 6 additions & 6 deletions aana/tests/units/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load_numpy_from_image_bytes(content: bytes) -> np.ndarray:
def mock_download_file(mocker):
"""Mock download_file function in both media and image modules."""
# Path to the file to be used as mock return value
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
content = path.read_bytes()

# Mock for aana.core.models.media.download_file
Expand All @@ -41,7 +41,7 @@ def mock_download_file(mocker):
def test_image(mock_download_file):
"""Test that the image can be created from path, url, content, or numpy."""
try:
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
image = Image(path=path, save_on_disk=False)
assert image.path == path
assert image.content is None
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_image(mock_download_file):
image.cleanup()

try:
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
content = path.read_bytes()
image = Image(content=content, save_on_disk=False)
assert image.path is None
Expand Down Expand Up @@ -120,7 +120,7 @@ def test_image_path_not_exist():
def test_save_image(mock_download_file):
"""Test that save_on_disk works."""
try:
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
image = Image(path=path, save_on_disk=True)
assert image.path == path
assert image.content is None
Expand All @@ -143,7 +143,7 @@ def test_save_image(mock_download_file):
image.cleanup()

try:
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
content = path.read_bytes()
image = Image(content=content, save_on_disk=True)
assert image.content == content
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_at_least_one_input():
def test_get_pil_image():
"""Test that get_pil_image method."""
try:
path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
image = Image(path=path, save_on_disk=False)
pil_image = image.get_pil_image()
assert isinstance(pil_image, PIL.Image.Image)
Expand Down
50 changes: 20 additions & 30 deletions aana/tests/units/test_image_chat_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
@pytest.fixture
def input_image():
"""Gets an Image for test."""
image_path = resources.path("aana.tests.files.images", "Starry_Night.jpeg")
image_path = resources.files("aana.tests.files.images") / "Starry_Night.jpeg"
return Image(path=image_path)


@pytest.fixture
def input_prompt():
"""Gets a text for text."""
Expand Down Expand Up @@ -54,17 +55,18 @@ def test_image_content_creation(input_image):
with pytest.raises(ValueError):
content = ImageContent(image="Image")


def test_image_dialog_creation(input_image, input_prompt):
"""Test that a image chat dialog can be created."""
system_messages = ImageChatMessage(content=[
ImageContent(image=input_image),
TextContent(text=input_prompt)
], role="system")
system_messages = ImageChatMessage(
content=[ImageContent(image=input_image), TextContent(text=input_prompt)],
role="system",
)

user_messages = ImageChatMessage(content=[
ImageContent(image=input_image),
TextContent(text=input_prompt)
], role="user")
user_messages = ImageChatMessage(
content=[ImageContent(image=input_image), TextContent(text=input_prompt)],
role="user",
)

dialog = ImageChatDialog(messages=[system_messages, user_messages])

Expand All @@ -80,7 +82,7 @@ def test_image_dialog_creation(input_image, input_prompt):

assert isinstance(message.content[1], TextContent)
assert message.content[1].text == input_prompt

messages, images = dialog.to_objects()
assert len(messages) == 2
assert len(images) == 2
Expand Down Expand Up @@ -130,30 +132,18 @@ def test_image_dialog_creation_from_list(input_image, input_prompt):
messages = [
{
"content": [
{
"type": "image",
"image": input_image
},
{
"type": "text",
"text": input_prompt
}
{"type": "image", "image": input_image},
{"type": "text", "text": input_prompt},
],
"role": "system"
"role": "system",
},
{
"content": [
{
"type": "image",
"image": input_image
},
{
"type": "text",
"text": input_prompt
}
{"type": "image", "image": input_image},
{"type": "text", "text": input_prompt},
],
"role": "user"
}
"role": "user",
},
]

dialog = ImageChatDialog.from_list(messages)
Expand All @@ -170,7 +160,7 @@ def test_image_dialog_creation_from_list(input_image, input_prompt):

assert isinstance(message.content[1], TextContent)
assert message.content[1].text == input_prompt

messages, images = dialog.to_objects()
assert len(messages) == 2
assert len(images) == 2
Expand Down
Loading

0 comments on commit dd1bad5

Please sign in to comment.