Skip to content

Commit

Permalink
Merge pull request #281 from edenai/fix/properly-close-files
Browse files Browse the repository at this point in the history
Fix/properly-close-files
  • Loading branch information
appelstroop authored Dec 3, 2024
2 parents 37fa0a2 + ea326bd commit 512bd39
Show file tree
Hide file tree
Showing 36 changed files with 819 additions and 800 deletions.
32 changes: 21 additions & 11 deletions edenai_apis/apis/affinda/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,29 @@ def create_document(
files: Optional[Dict[str, BufferedReader]] = None

if file.type == "url":
payload["url"] = file.file
payload["url"] = file.file
elif file.type == "file":
files = {"file": open(file.file, "rb")}

return Document(
**self.__requests(
method=HTTPMethod.POST,
url=f"{self.BASE_URL}/documents",
headers=self.headers,
files=files,
data=payload,
with open(file.file, "rb") as f:
files = {"file": f}
return Document(
**self.__requests(
method=HTTPMethod.POST,
url=f"{self.BASE_URL}/documents",
headers=self.headers,
files=files,
data=payload,
)
)
else:
return Document(
**self.__requests(
method=HTTPMethod.POST,
url=f"{self.BASE_URL}/documents",
headers=self.headers,
files=None,
data=payload,
)
)
)

def delete_document(self, identifier: str) -> None:
"""Delete the document with the given identifier.
Expand Down
29 changes: 15 additions & 14 deletions edenai_apis/apis/amazon/amazon_ocr_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,23 @@ def ocr__ocr(
def ocr__identity_parser(
self, file: str, file_url: str = ""
) -> ResponseType[IdentityParserDataClass]:
file_ = open(file, "rb")

payload = {
"DocumentPages": [
{
"Bytes": file_.read(),
"S3Object": {"Bucket": self.api_settings["bucket"], "Name": "test"},
}
]
}

original_response = handle_amazon_call(
self.clients["textract"].analyze_id, **payload
)
with open(file, "rb") as file_:
payload = {
"DocumentPages": [
{
"Bytes": file_.read(),
"S3Object": {
"Bucket": self.api_settings["bucket"],
"Name": "test",
},
}
]
}

file_.close()
original_response = handle_amazon_call(
self.clients["textract"].analyze_id, **payload
)

items: Sequence[InfosIdentityParserDataClass] = []
for document in original_response["IdentityDocuments"]:
Expand Down
102 changes: 47 additions & 55 deletions edenai_apis/apis/api4ai/api4ai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ def image__object_detection(
"""
This function is used to detect objects in an image.
"""
file_ = open(file, "rb")
files = {"image": file_}
response = requests.post(self.urls["object_detection"], files=files)
original_response = response.json()

file_.close()
with open(file, "rb") as file_:
files = {"image": file_}
response = requests.post(self.urls["object_detection"], files=files)
original_response = response.json()

if "failure" in original_response["results"][0]["status"]["code"]:
raise ProviderException(
Expand Down Expand Up @@ -135,14 +133,13 @@ def image__object_detection(
def image__face_detection(
self, file: str, file_url: str = ""
) -> ResponseType[FaceDetectionDataClass]:
file_ = open(file, "rb")
payload = {
"image": file_,
}
# Get response
response = requests.post(self.urls["face_detection"], files=payload)
original_response = response.json()
file_.close()
with open(file, "rb") as file_:
payload = {
"image": file_,
}
# Get response
response = requests.post(self.urls["face_detection"], files=payload)
original_response = response.json()

# Handle errors
if "failure" in original_response["results"][0]["status"]["code"]:
Expand Down Expand Up @@ -200,13 +197,11 @@ def image__face_detection(
def image__anonymization(
self, file: str, file_url: str = ""
) -> ResponseType[AnonymizationDataClass]:
file_ = open(file, "rb")
files = {"image": file_}
response = requests.post(self.urls["anonymization"], files=files)
with open(file, "rb") as file_:
files = {"image": file_}
response = requests.post(self.urls["anonymization"], files=files)

original_response = response.json()

file_.close()
original_response = response.json()

if "failure" in original_response["results"][0]["status"]["code"]:
raise ProviderException(
Expand Down Expand Up @@ -246,27 +241,26 @@ def image__anonymization(
def image__logo_detection(
self, file: str, file_url: str = "", model: Optional[str] = None
) -> ResponseType[LogoDetectionDataClass]:
file_ = open(file, "rb")
payload = {
"image": file_,
}
# Get response
response = requests.post(
self.urls["logo_detection"].format(model=model), files=payload
)
if response.status_code >= 400:
error_message = ""
try:
error_message = response.json().get("message", "")
except:
pass
error_message = (
error_message or "Something went wrong when calling the provider"
with open(file, "rb") as file_:
payload = {
"image": file_,
}
# Get response
response = requests.post(
self.urls["logo_detection"].format(model=model), files=payload
)
raise ProviderException(error_message, code=response.status_code)
if response.status_code >= 400:
error_message = ""
try:
error_message = response.json().get("message", "")
except:
pass
error_message = (
error_message or "Something went wrong when calling the provider"
)
raise ProviderException(error_message, code=response.status_code)

original_response = response.json()
file_.close()
original_response = response.json()
# Handle errors
if "failure" in original_response["results"][0]["status"]["code"]:
raise ProviderException(
Expand Down Expand Up @@ -309,19 +303,18 @@ def image__logo_detection(
def image__explicit_content(
self, file: str, file_url: str = ""
) -> ResponseType[ExplicitContentDataClass]:
file_ = open(file, "rb")
payload = {
"image": file_,
}
# Get response
response = requests.post(self.urls["nsfw"], files=payload)
try:
original_response = response.json()
except JSONDecodeError as exp:
raise ProviderException(
message="Internal server error", code=response.status_code
) from exp
file_.close()
with open(file, "rb") as file_:
payload = {
"image": file_,
}
# Get response
response = requests.post(self.urls["nsfw"], files=payload)
try:
original_response = response.json()
except JSONDecodeError as exp:
raise ProviderException(
message="Internal server error", code=response.status_code
) from exp

# Handle errors
if (
Expand Down Expand Up @@ -370,9 +363,8 @@ def ocr__ocr(
language: str,
file_url: str = "",
) -> ResponseType[OcrDataClass]:
file_ = open(file, "rb")
response = requests.post(self.urls["ocr"], files={"image": file_})
file_.close()
with open(file, "rb") as file_:
response = requests.post(self.urls["ocr"], files={"image": file_})

error = get_errors_from_response(response)
if error is not None:
Expand Down
74 changes: 35 additions & 39 deletions edenai_apis/apis/base64/base64_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ def _get_response(self, response: requests.Response) -> Any:
raise ProviderException(response.text, code=response.status_code)

def _send_ocr_document(self, file: str, model_type: str) -> Dict:
file_ = open(file, "rb")
image_as_base64 = (
f"data:{mimetypes.guess_type(file)[0]};base64,"
+ base64.b64encode(file_.read()).decode()
)
file_.close()
with open(file, "rb") as file_:
image_as_base64 = (
f"data:{mimetypes.guess_type(file)[0]};base64,"
+ base64.b64encode(file_.read()).decode()
)

data = {"modelTypes": [model_type], "image": image_as_base64}

Expand Down Expand Up @@ -176,20 +175,17 @@ def ocr__financial_parser(
def ocr__identity_parser(
self, file: str, file_url: str = ""
) -> ResponseType[IdentityParserDataClass]:
file_ = open(file, "rb")

image_as_base64 = (
f"data:{mimetypes.guess_type(file)[0]};base64,"
+ base64.b64encode(file_.read()).decode()
)

payload = json.dumps({"image": image_as_base64})
with open(file, "rb") as file_:
image_as_base64 = (
f"data:{mimetypes.guess_type(file)[0]};base64,"
+ base64.b64encode(file_.read()).decode()
)

headers = {"Content-Type": "application/json", "Authorization": self.api_key}
payload = json.dumps({"image": image_as_base64})

response = requests.post(url=self.url, headers=headers, data=payload)
headers = {"Content-Type": "application/json", "Authorization": self.api_key}

file_.close()
response = requests.post(url=self.url, headers=headers, data=payload)

original_response = self._get_response(response)

Expand Down Expand Up @@ -335,22 +331,22 @@ def image__face_compare(
if file1_url and file2_url:
payload = json.dumps({"url": file1_url, "queryUrl": file2_url})
else:
file_reference_ = open(file1, "rb")
file_query_ = open(file2, "rb")
image_reference_as_base64 = (
f"data:{mimetypes.guess_type(file1)[0]};base64,"
+ base64.b64encode(file_reference_.read()).decode()
)
image_query_as_base64 = (
f"data:{mimetypes.guess_type(file2)[0]};base64,"
+ base64.b64encode(file_query_.read()).decode()
)
payload = json.dumps(
{
"document": image_reference_as_base64,
"query": image_query_as_base64,
}
)
with open(file1, "rb") as file_reference_, open(file2, "rb") as file_query_:

image_reference_as_base64 = (
f"data:{mimetypes.guess_type(file1)[0]};base64,"
+ base64.b64encode(file_reference_.read()).decode()
)
image_query_as_base64 = (
f"data:{mimetypes.guess_type(file2)[0]};base64,"
+ base64.b64encode(file_query_.read()).decode()
)
payload = json.dumps(
{
"document": image_reference_as_base64,
"query": image_query_as_base64,
}
)

response = requests.request("POST", url, headers=headers, data=payload)
original_response = self._get_response(response)
Expand Down Expand Up @@ -474,12 +470,12 @@ def ocr__anonymization_async__launch_job(
self, file: str, file_url: str = ""
) -> AsyncLaunchJobResponseType:
data_job_id = {}
file_ = open(file, "rb")
image_as_base64 = (
f"data:{mimetypes.guess_type(file)[0]};base64,"
+ base64.b64encode(file_.read()).decode()
)
file_.close()
with open(file, "rb") as file_:
image_as_base64 = (
f"data:{mimetypes.guess_type(file)[0]};base64,"
+ base64.b64encode(file_.read()).decode()
)

payload = json.dumps(
{
"image": image_as_base64,
Expand Down
3 changes: 2 additions & 1 deletion edenai_apis/apis/clarifai/clarifai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ def image__logo_detection(
with open(file, "rb") as file_:
file_content = file_.read()
try:
width, height = Img.open(file).size
with Img.open(file) as img:
width, height = img.size
except UnidentifiedImageError:
raise ProviderException("This image type is not supported.")

Expand Down
51 changes: 24 additions & 27 deletions edenai_apis/apis/deepl/deepl_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,30 @@ def translation__document_translation(
mimetype = mimetypes.guess_type(file)[0]
extension = mimetypes.guess_extension(mimetype)

file_ = open(file, "rb")

files = {
"file": file_,
}

data = {"target_lang": target_language, "source_lang": source_language}

try:
response = requests.post(
f"{self.url}document", headers=self.header, data=data, files=files
)
except:
raise ProviderException(
"Something went wrong when performing document translation!!", 500
)
if response.status_code >= 400:
raise ProviderException(
message=http.client.responses[response.status_code],
code=response.status_code,
)
try:
original_response = response.json()
except json.JSONDecodeError:
raise ProviderException("Internal server error", 500)

file_.close()
with open(file, "rb") as file_:
files = {
"file": file_,
}

data = {"target_lang": target_language, "source_lang": source_language}

try:
response = requests.post(
f"{self.url}document", headers=self.header, data=data, files=files
)
except:
raise ProviderException(
"Something went wrong when performing document translation!!", 500
)
if response.status_code >= 400:
raise ProviderException(
message=http.client.responses[response.status_code],
code=response.status_code,
)
try:
original_response = response.json()
except json.JSONDecodeError:
raise ProviderException("Internal server error", 500)

if response.status_code != 200:
raise ProviderException(
Expand Down
Loading

0 comments on commit 512bd39

Please sign in to comment.