feat: rest samples- visual search- search using URL

This commit is contained in:
v-ghajam 2024-07-02 14:48:10 +03:00 коммит произвёл Gheiath
Родитель 928ffa1a6c
Коммит 9caf16c5c9
2 изменённых файлов: 43 добавлений и 22 удалений

Просмотреть файл

@ -11,8 +11,9 @@ from requests import HTTPError
def visual_search_basic(
image_path,
subscription_key,
image_url=None,
image_path=None,
auth_header_name="Ocp-Apim-Subscription-Key",
mkt="en-us",
):
@ -28,14 +29,18 @@ def visual_search_basic(
Args:
subscription_key (str): Azure subscription key of Bing Visual Search service
auth_header_name (str): Name of the authorization header
image_url (str): Url of the image used for search
image_path (str): Path of the image used for search
mkt (str): Market to search in
"""
# Default value for demo purposes
image_url = (
image_url
or "https://upload.wikimedia.org/wikipedia/commons/4/40/Bing_Cherries_(USDA_ARS).jpg"
)
# Construct a request
endpoint = "https://api.bing.microsoft.com/v7.0/images/visualsearch"
# Image_name is the name of the image
with open(image_path, "rb") as img:
file = {"image": ("Image_name", img.read())}
params = {"mkt": mkt}
headers = {
auth_header_name: subscription_key,
@ -44,15 +49,36 @@ def visual_search_basic(
# Call the API
try:
response = requests.post(
endpoint, headers=headers, params=params, files=file, timeout=10
)
if image_path:
# Image_name is the name of the image
with open(image_path, "rb") as img:
file = {"image": ("Image_name", img.read())}
response = requests.post(
endpoint, headers=headers, params=params, files=file, timeout=10
)
else:
headers["Content-Type"] += ";boundary=--example-boundary-1234"
body = (
"--example-boundary-1234\n"
'Content-Disposition: form-data; name="knowledgeRequest"\n\n'
'{"imageInfo": {"url": "' + image_url + '"}\n}\n'
"--example-boundary-1234--"
)
response = requests.post(
endpoint, headers=headers, params=params, data=body, timeout=10
)
response.raise_for_status()
return response
except HTTPError as ex:
print(ex)
print("++The above exception was thrown and handled succesfully++")
return response
except FileNotFoundError as ex:
print(ex)
print("++The above exception was thrown and handled succesfully++")
return
def main() -> None:
@ -72,8 +98,11 @@ def main() -> None:
)
)
response = visual_search_basic("./my_image.jpg", subscription_key)
print("\nResponse Headers:\n")
response = visual_search_basic(
subscription_key,
# image_path="./my_image.jpg",
# image_url="https://upload.wikimedia.org/wikipedia/commons/4/40/Bing_Cherries_(USDA_ARS).jpg",
)
pprint(dict(response.headers))
print("\nJSON Response:\n")

Просмотреть файл

@ -27,16 +27,12 @@ class VisualSearchRESTSamplesTest(unittest.TestCase):
def test_visual_search_basic(self):
"""Test the basic REST call to Visual Search API"""
response = visual_search_basic(
"./my_image.jpg", subscription_key=self.subscription_key
)
response = visual_search_basic(subscription_key=self.subscription_key)
self.assertEqual(response.status_code, 200)
def test_visual_search_response_is_json(self):
"""Test that Visual Search API returns responses in JSON format"""
response = visual_search_basic(
"./my_image.jpg", subscription_key=self.subscription_key
)
response = visual_search_basic(subscription_key=self.subscription_key)
try:
response.json()
except JSONDecodeError:
@ -46,14 +42,12 @@ class VisualSearchRESTSamplesTest(unittest.TestCase):
def test_visual_search_no_auth(self):
"""Test that Visual Search API returns 401 if authorization fails"""
response = visual_search_basic("./my_image.jpg", subscription_key="")
response = visual_search_basic(subscription_key="")
self.assertEqual(response.status_code, 401)
def test_visual_search_response_object_type(self):
"""Test that Visual Search API returns the correct type hint"""
response = visual_search_basic(
"./my_image.jpg", subscription_key=self.subscription_key
)
response = visual_search_basic(subscription_key=self.subscription_key)
try:
self.assertEqual(response.json()["_type"], "ImageKnowledge")
except KeyError:
@ -61,9 +55,7 @@ class VisualSearchRESTSamplesTest(unittest.TestCase):
def test_visual_search_response_object_structure(self):
"""Test that Visual Search API responses follow the correct structure"""
response = visual_search_basic(
"./my_image.jpg", subscription_key=self.subscription_key
)
response = visual_search_basic(subscription_key=self.subscription_key)
response_json = response.json()
self.assertIn("image", response_json)
self.assertIn("imageInsightsToken", response_json["image"])