Skip to content

Commit

Permalink
Add tests and extra dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheepsta300 committed Oct 17, 2024
1 parent da9a567 commit dedbfac
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions libs/community/extended_testing_deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ atlassian-python-api>=3.36.0,<4
azure-ai-documentintelligence>=1.0.0b1,<2
azure-identity>=1.15.0,<2
azure-search-documents==11.4.0
azure.ai.vision.imageanalysis>=1.0.0b3
beautifulsoup4>=4,<5
bibtexparser>=1.4.0,<2
cassio>=0.1.6,<0.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AzureAiServicesImageAnalysisTool(BaseTool):
azure_ai_services_key (Optional[str]): The API key for Azure AI Services.
azure_ai_services_endpoint (Optional[str]): The endpoint URL for Azure AI Services.
visual_features Any: The visual features to analyze in the image, can be set as
either strings or VisualFeatures.
either strings or azure.ai.vision.imageanalysis.models.VisualFeatures.
(e.g. 'TAGS', VisualFeatures.CAPTION).
image_analysis_client (Any): The client for interacting
with Azure AI Services Image Analysis.
Expand Down
Binary file added libs/community/tests/examples/building.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""Tests for the Azure AI Services Image Analysis Tool."""

from pathlib import Path
from typing import Any

import pytest

from langchain_community.tools.azure_ai_services.image_analysis import (
AzureAiServicesImageAnalysisTool,
)

this_dir = Path(__file__).parents[3]

examples_dir = this_dir / "examples"
building_path = examples_dir / "building.jpg"


@pytest.mark.requires("azure.ai.vision.imageanalysis")
def test_content_safety(mocker: Any) -> None:
mocker.patch("azure.ai.vision.imageanalysis.ImageAnalysisClient", autospec=True)
mocker.patch("azure.core.credentials.AzureKeyCredential", autospec=True)

key = "key"
endpoint = "endpoint"

tool = AzureAiServicesImageAnalysisTool(
azure_ai_services_key=key, azure_ai_services_endpoint=endpoint
)
assert tool.azure_ai_services_key == key
assert tool.azure_ai_services_endpoint == endpoint


@pytest.mark.requires("azure.ai.vision.imageanalysis")
def test_local_image_analysis(mocker: Any) -> None:
key = "key"
endpoint = "endpoint"

mocker.patch("azure.ai.vision.imageanalysis.ImageAnalysisClient", autospec=True)
mocker.patch("azure.core.credentials.AzureKeyCredential", autospec=True)
mocker.patch(
"langchain_community.tools.azure_ai_services.utils.detect_file_src_type",
return_value="local",
)

tool = AzureAiServicesImageAnalysisTool(
azure_ai_services_key=key,
azure_ai_services_endpoint=endpoint,
visual_features=["CAPTION"],
)

mock_content_client = mocker.Mock()
mock_content_client.analyze.return_value = mocker.Mock()
mock_content_client.analyze.return_value.caption.text = "A building corner."

mock_content_client.analyze.return_value.objects = None
mock_content_client.analyze.return_value.tags = None
mock_content_client.analyze.return_value.read = None
mock_content_client.analyze.return_value.dense_captions = None
mock_content_client.analyze.return_value.smart_crops = None
mock_content_client.analyze.return_value.people = None

tool.image_analysis_client = mock_content_client

input = building_path
output = "Caption: A building corner."

result = tool._run(input)
assert result == output


@pytest.mark.requires("azure.ai.vision.imageanalysis")
def test_local_image_different_features(mocker: Any) -> None:
key = "key"
endpoint = "endpoint"

mocker.patch("azure.ai.vision.imageanalysis.ImageAnalysisClient", autospec=True)
mocker.patch("azure.core.credentials.AzureKeyCredential", autospec=True)
mocker.patch(
"langchain_community.tools.azure_ai_services.utils.detect_file_src_type",
return_value="local",
)

tool = AzureAiServicesImageAnalysisTool(
azure_ai_services_key=key,
azure_ai_services_endpoint=endpoint,
visual_features=["PEOPLE", "CAPTION", "SMARTCROPS"],
)

mock_content_client = mocker.Mock()
mock_content_client.analyze.return_value = mocker.Mock()
mock_content_client.analyze.return_value.caption.text = "A building corner."

mock_content_client.analyze.return_value.objects = None
mock_content_client.analyze.return_value.tags = None
mock_content_client.analyze.return_value.read = None
mock_content_client.analyze.return_value.dense_captions = None

mock_smart_crops = mocker.MagicMock()
mock_smart_crops.list = [
{"aspectRatio": 1.97, "boundingBox": {"x": 43, "y": 24, "w": 853, "h": 432}}
]
mock_smart_crops.__len__.return_value = 1
mock_content_client.analyze.return_value.smart_crops = mock_smart_crops

mock_people = mocker.MagicMock()
mock_people.list = [
{
"boundingBox": {"x": 454, "y": 44, "w": 408, "h": 531},
"confidence": 0.9601945281028748,
},
]
mock_people.__len__.return_value = 1
mock_content_client.analyze.return_value.people = mock_people

tool.image_analysis_client = mock_content_client

input = building_path
output = (
"Caption: A building corner.\n"
"Smart Crops: {'aspectRatio': 1.97,"
" 'boundingBox': {'x': 43, 'y': 24, 'w': 853, 'h': 432}}\n"
"People: {'boundingBox': {'x': 454, 'y': 44, 'w': 408, 'h': 531},"
" 'confidence': 0.9601945281028748}"
)

result = tool._run(input)
assert result == output

0 comments on commit dedbfac

Please sign in to comment.