From 0675537d24d06bb248583d4125b9fe0ce582dcc6 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Mon, 1 Jan 2024 18:39:18 +0530 Subject: [PATCH] Convert more assertions / valueerrors to usererrors --- daras_ai/extract_face.py | 6 +++++- daras_ai_v2/asr.py | 2 +- daras_ai_v2/face_restoration.py | 1 + daras_ai_v2/vector_search.py | 3 ++- recipes/FaceInpainting.py | 20 ++++++++++++-------- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/daras_ai/extract_face.py b/daras_ai/extract_face.py index e81b35d6e..059e2fe57 100644 --- a/daras_ai/extract_face.py +++ b/daras_ai/extract_face.py @@ -1,6 +1,10 @@ import numpy as np +class FaceNotFoundException(ValueError): + pass + + def extract_and_reposition_face_cv2( orig_img, *, @@ -118,7 +122,7 @@ def face_oval_hull_generator(image_cv2): results = face_mesh.process(cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)) if not results.multi_face_landmarks: - raise ValueError("Face not found") + raise FaceNotFoundException("Face not found") for landmark_list in results.multi_face_landmarks: idx_to_coordinates = build_idx_to_coordinates_dict( diff --git a/daras_ai_v2/asr.py b/daras_ai_v2/asr.py index 214483fa8..e2e37d14f 100644 --- a/daras_ai_v2/asr.py +++ b/daras_ai_v2/asr.py @@ -757,7 +757,7 @@ def check_wav_audio_format(filename: str) -> bool: data = json.loads(subprocess.check_output(args, stderr=subprocess.STDOUT)) except subprocess.CalledProcessError as e: ffmpeg_output_error = ValueError(e.output, e) - raise ValueError( + raise UserError( "Invalid audio file. Please confirm the file is not corrupted and has a supported format (google 'ffmpeg supported audio file types')" ) from ffmpeg_output_error return ( diff --git a/daras_ai_v2/face_restoration.py b/daras_ai_v2/face_restoration.py index cfd2d1ec0..4c8a5fb77 100644 --- a/daras_ai_v2/face_restoration.py +++ b/daras_ai_v2/face_restoration.py @@ -6,6 +6,7 @@ import requests from daras_ai.image_input import upload_file_from_bytes +from daras_ai_v2.exceptions import UserError from daras_ai_v2.gpu_server import call_gpu_server_b64, GpuEndpoints from daras_ai_v2.stable_diffusion import sd_upscale diff --git a/daras_ai_v2/vector_search.py b/daras_ai_v2/vector_search.py index fbbc3b206..525eca3ef 100644 --- a/daras_ai_v2/vector_search.py +++ b/daras_ai_v2/vector_search.py @@ -22,6 +22,7 @@ guess_ext_from_response, get_mimetype_from_response, ) +from daras_ai_v2.exceptions import UserError from daras_ai_v2 import settings from daras_ai_v2.asr import AsrModels, run_asr, run_google_translate from daras_ai_v2.azure_doc_extract import ( @@ -270,7 +271,7 @@ def doc_url_to_file_metadata(f_url: str) -> FileMetadata: meta = gdrive_metadata(url_to_gdrive_file_id(f)) except HttpError as e: if e.status_code == 404: - raise FileNotFoundError( + raise UserError( f"Could not download the google doc at {f_url} " f"Please make sure to make the document public for viewing." ) from e diff --git a/recipes/FaceInpainting.py b/recipes/FaceInpainting.py index a9353ea07..62302c68a 100644 --- a/recipes/FaceInpainting.py +++ b/recipes/FaceInpainting.py @@ -6,13 +6,14 @@ import gooey_ui as st from bots.models import Workflow -from daras_ai.extract_face import extract_and_reposition_face_cv2 +from daras_ai.extract_face import FaceNotFoundException, extract_and_reposition_face_cv2 from daras_ai.image_input import ( upload_file_from_bytes, safe_filename, ) from daras_ai_v2 import stable_diffusion from daras_ai_v2.base import BasePage +from daras_ai_v2.exceptions import UserError from daras_ai_v2.extract_face import extract_face_img_bytes from daras_ai_v2.face_restoration import gfpgan from daras_ai_v2.functional import map_parallel @@ -255,13 +256,16 @@ def run(self, state: dict): input_image_url = state["input_image"] img_bytes = requests.get(input_image_url).content - re_img_bytes, face_mask_bytes = extract_face_img_bytes( - img_bytes, - out_size=(state["output_width"], state["output_height"]), - face_scale=state["face_scale"], - pos_x=state["face_pos_x"], - pos_y=state["face_pos_y"], - ) + try: + re_img_bytes, face_mask_bytes = extract_face_img_bytes( + img_bytes, + out_size=(state["output_width"], state["output_height"]), + face_scale=state["face_scale"], + pos_x=state["face_pos_x"], + pos_y=state["face_pos_y"], + ) + except FaceNotFoundException as e: + raise UserError(str(e)) from e state["resized_image"] = upload_file_from_bytes("re_img.png", re_img_bytes) state["face_mask"] = upload_file_from_bytes("face_mask.png", face_mask_bytes)