Skip to content

Commit

Permalink
Convert more assertions / valueerrors to usererrors
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Jan 1, 2024
1 parent 8cec0e6 commit 0675537
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
6 changes: 5 additions & 1 deletion daras_ai/extract_face.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import numpy as np


class FaceNotFoundException(ValueError):
pass


def extract_and_reposition_face_cv2(
orig_img,
*,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion daras_ai_v2/asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions daras_ai_v2/face_restoration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion daras_ai_v2/vector_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions recipes/FaceInpainting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0675537

Please sign in to comment.