Skip to content

Commit

Permalink
more usererror handling
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Feb 21, 2024
1 parent 967cdd8 commit 5c3a327
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion recipes/DeforumSD.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from bots.models import Workflow
from daras_ai_v2.base import BasePage
from daras_ai_v2.enum_selector_widget import enum_selector
from daras_ai_v2.exceptions import UserError
from daras_ai_v2.gpu_server import call_celery_task_outfile
from daras_ai_v2.loom_video_widget import youtube_video
from daras_ai_v2.safety_checker import safety_checker
Expand Down Expand Up @@ -481,4 +482,4 @@ def run(self, state: dict):
except RuntimeError as e:
msg = "\n\n".join(e.args).lower()
if "key frame string not correctly formatted" in msg:
raise st.UserError(str(e)) from e
raise UserError(str(e)) from e
20 changes: 11 additions & 9 deletions routers/root.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import os.path
import subprocess
import tempfile
import typing
from time import time
Expand Down Expand Up @@ -34,7 +33,7 @@
from daras_ai_v2.bots import request_json
from daras_ai_v2.copy_to_clipboard_button_widget import copy_to_clipboard_scripts
from daras_ai_v2.db import FIREBASE_SESSION_COOKIE
from daras_ai_v2.exceptions import ffmpeg
from daras_ai_v2.exceptions import ffmpeg, UserError
from daras_ai_v2.manage_api_keys_widget import manage_api_keys
from daras_ai_v2.meta_content import build_meta_tags, raw_build_meta_tags
from daras_ai_v2.meta_preview_url import meta_preview_url
Expand Down Expand Up @@ -187,13 +186,16 @@ def file_upload(request: Request, form_data: FormData = Depends(request_form_fil
) as infile:
infile.write(data)
infile.flush()
if not check_wav_audio_format(infile.name):
with tempfile.NamedTemporaryFile(suffix=".wav") as outfile:
ffmpeg("-i", infile.name, *FFMPEG_WAV_ARGS, outfile.name)

filename += ".wav"
content_type = "audio/wav"
data = outfile.read()
try:
if not check_wav_audio_format(infile.name):
with tempfile.NamedTemporaryFile(suffix=".wav") as outfile:
ffmpeg("-i", infile.name, *FFMPEG_WAV_ARGS, outfile.name)

filename += ".wav"
content_type = "audio/wav"
data = outfile.read()
except UserError as e:
return Response(content=str(e), status_code=400)

if content_type.startswith("image/"):
with Image(blob=data) as img:
Expand Down

0 comments on commit 5c3a327

Please sign in to comment.