Skip to content

Commit

Permalink
handle exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayof committed Aug 24, 2024
1 parent 653bfd2 commit 14126f7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 2 additions & 5 deletions vision_agent/clients/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from requests import Session
from requests.adapters import HTTPAdapter
from requests.exceptions import ConnectionError, RequestException, Timeout

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -38,11 +37,10 @@ def post(self, url: str, payload: Dict[str, Any]) -> Dict[str, Any]:
response.raise_for_status()
result: Dict[str, Any] = response.json()
_LOGGER.info(json.dumps(result))
except (ConnectionError, Timeout, RequestException) as err:
_LOGGER.warning(f"Error: {err}.")
except json.JSONDecodeError:
resp_text = response.text
_LOGGER.warning(f"Response seems incorrect: '{resp_text}'.")
raise
return result

def get(self, url: str) -> Dict[str, Any]:
Expand All @@ -53,9 +51,8 @@ def get(self, url: str) -> Dict[str, Any]:
response.raise_for_status()
result: Dict[str, Any] = response.json()
_LOGGER.info(json.dumps(result))
except (ConnectionError, Timeout, RequestException) as err:
_LOGGER.warning(f"Error: {err}.")
except json.JSONDecodeError:
resp_text = response.text
_LOGGER.warning(f"Response seems incorrect: '{resp_text}'.")
raise
return result
10 changes: 9 additions & 1 deletion vision_agent/clients/landing_public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
from uuid import UUID
from typing import List

from requests.exceptions import HTTPError

from vision_agent.clients.http import BaseHTTP
from vision_agent.utils.type_defs import LandingaiAPIKey
from vision_agent.utils.exceptions import FineTuneModelNotFound
from vision_agent.tools.tools_types import BboxInputBase64, PromptTask, JobStatus


Expand All @@ -27,4 +30,9 @@ def launch_fine_tuning_job(

def check_fine_tuning_job(self, job_id: UUID) -> JobStatus:
url = f"v1/agent/jobs/fine-tuning/{job_id}/status"
return JobStatus(self.get(url)["status"])
try:
get_job = self.get(url)
except HTTPError as err:
if err.response.status_code == 404:
raise FineTuneModelNotFound()
return JobStatus(get_job["status"])
6 changes: 6 additions & 0 deletions vision_agent/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ class FineTuneModelIsNotReady(Exception):
If this is raised, it's recommended to wait 5 seconds before trying to use
the model again.
"""


class FineTuneModelNotFound(Exception):
"""Exception raised when the fine-tune model is not found.
If this is raised, it's recommended to try another model id.
"""

0 comments on commit 14126f7

Please sign in to comment.