Skip to content

Commit

Permalink
Merge pull request #162 from tattle-made/hotfix
Browse files Browse the repository at this point in the history
Hotfix
  • Loading branch information
duggalsu authored Mar 12, 2024
2 parents 76d900e + 805e426 commit 9c7d06b
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 119 deletions.
25 changes: 16 additions & 9 deletions src/core/models/media_factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from requests.exceptions import ConnectTimeout
import PIL
from io import BytesIO
import numpy as np
Expand All @@ -13,12 +14,15 @@
class ImageFactory:
@staticmethod
def make_from_url(image_url):
print("1", image_url)
resp = requests.get(image_url)
image_bytes = resp.content
image = PIL.Image.open(BytesIO(image_bytes))
image_array = np.array(image)
return {"image": image, "image_array": image_array, "image_bytes": image_bytes}
try:
print("1", image_url)
resp = requests.get(image_url, timeout=(3.05, 5))
image_bytes = resp.content
image = PIL.Image.open(BytesIO(image_bytes))
image_array = np.array(image)
return {"image": image, "image_array": image_array, "image_bytes": image_bytes}
except ConnectTimeout:
print('Request has timed out')

@staticmethod
def make_from_file_on_disk(image_path):
Expand Down Expand Up @@ -47,9 +51,12 @@ def make_from_file_in_memory(image_data: FileStorage):
class TextFactory:
@staticmethod
def make_from_url(text_url):
response = requests.get(text_url)
text = response.text
return {"text": text}
try:
response = requests.get(text_url, timeout=(3.05, 5))
text = response.text
return {"text": text}
except ConnectTimeout:
print('Request has timed out')

@staticmethod
def make_from_file_on_disk(image_path):
Expand Down
22 changes: 14 additions & 8 deletions src/core/operators/test_detect_text_in_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ def test_sample_image_from_memory(self):
def test_sample_image_from_url(self):
# todo : put URL of s3 endpoint in an environment variable to avoid s3 abuse
import requests
from requests.exceptions import ConnectTimeout

# https://tattle-media.s3.amazonaws.com/test-data/tattle-search/image_with_text_handwritten_hindi.jpeg
resp = requests.get(
"https://tattle-media.s3.amazonaws.com/test-data/tattle-search/text-in-image-test-hindi.png"
)
try:
# https://tattle-media.s3.amazonaws.com/test-data/tattle-search/image_with_text_handwritten_hindi.jpeg
resp = requests.get(
"https://tattle-media.s3.amazonaws.com/test-data/tattle-search/text-in-image-test-hindi.png",
timeout=(3.05, 5)
)

image = {"image_bytes": resp.content}
detected_text = detect_text_in_image.run(image)
print("----> 1", detected_text["text"])
self.assertEqual(detected_text["text"], "ठंड बहुत हैं:\nअपना ख्याल रखना\nठंडी- ठंडी\n")
except ConnectTimeout:
print('Request has timed out')

image = {"image_bytes": resp.content}
detected_text = detect_text_in_image.run(image)
print("----> 1", detected_text["text"])
self.assertEqual(detected_text["text"], "ठंड बहुत हैं:\nअपना ख्याल रखना\nठंडी- ठंडी\n")
63 changes: 41 additions & 22 deletions src/endpoint/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# from unittest.case import skip
import requests
from requests.exceptions import ConnectTimeout
import json

API_URL = "http://localhost:7000"
Expand All @@ -23,9 +24,12 @@ def testIndexText(self):
"media": open("sample_data/simple-text.txt", "rb"),
"data": json.dumps(data),
}
response = requests.post(url, json=data, files=files)
print(response.text)
self.assertEqual(response.status_code, 200)
try:
response = requests.post(url, json=data, files=files, timeout=(3.05, 5))
print(response.text)
self.assertEqual(response.status_code, 200)
except ConnectTimeout:
print('Request has timed out')

def testIndexImage(self):
url = "http://localhost:5000/index/image"
Expand All @@ -41,9 +45,12 @@ def testIndexImage(self):
"media": open("sample_data/image-with-text.jpg", "rb"),
"data": json.dumps(data),
}
response = requests.post(url, json=data, files=files)
print(response.text)
self.assertEqual(response.status_code, 200)
try:
response = requests.post(url, json=data, files=files, timeout=(3.05, 5))
print(response.text)
self.assertEqual(response.status_code, 200)
except ConnectTimeout:
print('Request has timed out')

def testIndexVideo(self):
url = API_URL + "/index/video"
Expand All @@ -55,9 +62,12 @@ def testIndexVideo(self):
"media": open("sample_data/image-with-text.jpg", "rb"),
"data": json.dumps(data),
}
response = requests.post(url, json=data, files=files)
print(response.text)
self.assertEqual(response.status_code, 200)
try:
response = requests.post(url, json=data, files=files, timeout=(3.05, 5))
print(response.text)
self.assertEqual(response.status_code, 200)
except ConnectTimeout:
print('Request has timed out')

def testRepresentText(self):
url = API_URL + "/represent/text"
Expand All @@ -74,10 +84,13 @@ def testRepresentText(self):
"media": open("sample_data/simple-text.txt", "rb"),
"data": json.dumps(data),
}
response = requests.post(url, json=data, files=files)
print(response.text)
self.assertEqual(response.status_code, 200)
# assert if the length of the vector is 512
try:
response = requests.post(url, json=data, files=files, timeout=(3.05, 5))
print(response.text)
self.assertEqual(response.status_code, 200)
# assert if the length of the vector is 512
except ConnectTimeout:
print('Request has timed out')

def testRepresentImage(self):
url = API_URL + "/represent/image"
Expand All @@ -93,9 +106,12 @@ def testRepresentImage(self):
"media": open("sample_data/image-with-text.jpg", "rb"),
"data": json.dumps(data),
}
response = requests.post(url, json=data, files=files)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()["representation"]), 512)
try:
response = requests.post(url, json=data, files=files, timeout=(3.05, 5))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json()["representation"]), 512)
except ConnectTimeout:
print('Request has timed out')

def testRepresentVideo(self):
url = API_URL + "/represent/image"
Expand All @@ -111,9 +127,12 @@ def testRepresentVideo(self):
"media": open("sample_data/cat_water.mp4", "rb"),
"data": json.dumps(data),
}
response = requests.post(url, json=data, files=files)
print(response.text)
self.assertEqual(response.status_code, 200)
# assert if the length of the vector is 512
# assert if the second returned parameter is a generator
# assert if every item in the generator is a vectr of 512 dimension
try:
response = requests.post(url, json=data, files=files, timeout=(3.05, 5))
print(response.text)
self.assertEqual(response.status_code, 200)
# assert if the length of the vector is 512
# assert if the second returned parameter is a generator
# assert if every item in the generator is a vectr of 512 dimension
except ConnectTimeout:
print('Request has timed out')
8 changes: 6 additions & 2 deletions src/endpoint/test_health.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import unittest
import requests
from requests.exceptions import ConnectTimeout


class TestHealth(unittest.TestCase):
def testHealthEndpoint(self):
url = "http://localhost:5000/health"
response = requests.get(url)
self.assertEqual(response.status_code, 200)
try:
response = requests.get(url, timeout=(3.05, 5))
self.assertEqual(response.status_code, 200)
except ConnectTimeout:
print('Request has timed out')
1 change: 1 addition & 0 deletions src/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def reporter(ch, method, properties, body):
environ.get("KOSH_API_URL") + "/index/report",
headers=headersAuth,
json=report,
timeout=(3.05, 5),
)
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception:
Expand Down
52 changes: 28 additions & 24 deletions src/tests/core/store/test_audio_es_vec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from unittest.case import skip
import requests
from requests.exceptions import ConnectTimeout
from core.store.es_vec import ES
from core.config import StoreConfig, StoreParameters
from core.models.media import MediaType
Expand All @@ -27,31 +28,34 @@
class TestAudioES(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
# ping es server to see if its working
response = requests.get("http://es:9200")
try:
# ping es server to see if its working
response = requests.get("http://es:9200", timeout=(3.05, 5))

if response.status_code == 200:
print("Elastic search server is running")
else:
print("No elasticsearch service found. Tests are bound to fail.")
param_dict = {
"host_name": "es",
"text_index_name": "test_text",
"image_index_name": "test_image",
"video_index_name": "test_video",
"audio_index_name": "test_audio",
}
cls.param = StoreConfig(
label="test",
type="es",
parameters=StoreParameters(
host_name=param_dict["host_name"],
image_index_name=param_dict["image_index_name"],
text_index_name=param_dict["text_index_name"],
video_index_name=param_dict["video_index_name"],
audio_index_name=param_dict["audio_index_name"],
),
)
if response.status_code == 200:
print("Elastic search server is running")
else:
print("No elasticsearch service found. Tests are bound to fail.")
param_dict = {
"host_name": "es",
"text_index_name": "test_text",
"image_index_name": "test_image",
"video_index_name": "test_video",
"audio_index_name": "test_audio",
}
cls.param = StoreConfig(
label="test",
type="es",
parameters=StoreParameters(
host_name=param_dict["host_name"],
image_index_name=param_dict["image_index_name"],
text_index_name=param_dict["text_index_name"],
video_index_name=param_dict["video_index_name"],
audio_index_name=param_dict["audio_index_name"],
),
)
except ConnectTimeout:
print('Request has timed out')

@classmethod
def tearDownClass(cls) -> None:
Expand Down
54 changes: 29 additions & 25 deletions src/tests/core/store/test_es_vec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from unittest.case import skip
import requests
from requests.exceptions import ConnectTimeout
from core.store.es_vec import ES
from core.config import StoreConfig, StoreParameters
from core.models.media import MediaType
Expand All @@ -15,32 +16,35 @@
class TestES(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print("-----------------ES TEST---------------------")
# ping es server to see if its working
response = requests.get("http://es:9200")
try:
print("-----------------ES TEST---------------------")
# ping es server to see if its working
response = requests.get("http://es:9200", timeout=(3.05, 5))

if response.status_code == 200:
print("Elastic search server is running")
else:
print("No elasticsearch service found. Tests are bound to fail.")
param_dict = {
"host_name": "es",
"text_index_name": "test_text",
"image_index_name": "test_image",
"video_index_name": "test_video",
"audio_index_name": "test_audio",
}
cls.param = StoreConfig(
label="test",
type="es",
parameters=StoreParameters(
host_name=param_dict["host_name"],
image_index_name=param_dict["image_index_name"],
text_index_name=param_dict["text_index_name"],
video_index_name=param_dict["video_index_name"],
audio_index_name=param_dict["audio_index_name"],
),
)
if response.status_code == 200:
print("Elastic search server is running")
else:
print("No elasticsearch service found. Tests are bound to fail.")
param_dict = {
"host_name": "es",
"text_index_name": "test_text",
"image_index_name": "test_image",
"video_index_name": "test_video",
"audio_index_name": "test_audio",
}
cls.param = StoreConfig(
label="test",
type="es",
parameters=StoreParameters(
host_name=param_dict["host_name"],
image_index_name=param_dict["image_index_name"],
text_index_name=param_dict["text_index_name"],
video_index_name=param_dict["video_index_name"],
audio_index_name=param_dict["audio_index_name"],
),
)
except ConnectTimeout:
print('Request has timed out')

@classmethod
def tearDownClass(cls) -> None:
Expand Down
Loading

0 comments on commit 9c7d06b

Please sign in to comment.