From 999d3cae6e63b11648fdaa987ab892bf47540a90 Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Mon, 29 Jul 2024 18:30:59 +0530 Subject: [PATCH] Handle None values in format_timestamp, session state, empty completion and SERP items --- daras_ai_v2/asr.py | 10 +++++++++- daras_ai_v2/enum_selector_widget.py | 2 +- daras_ai_v2/language_model.py | 2 ++ daras_ai_v2/serp_search.py | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/daras_ai_v2/asr.py b/daras_ai_v2/asr.py index eb7f03bd3..c09102fe4 100644 --- a/daras_ai_v2/asr.py +++ b/daras_ai_v2/asr.py @@ -1076,7 +1076,15 @@ def iterate_subtitles( yield segment_start, segment_end, segment_text -def format_timestamp(seconds: float, always_include_hours: bool, decimal_marker: str): +INFINITY_SECONDS = 99 * 3600 + 59 * 60 + 59 # 99:59:59 in seconds + + +def format_timestamp( + seconds: float | None, always_include_hours: bool, decimal_marker: str +): + if seconds is None: + # treat None as end of time + seconds = INFINITY_SECONDS assert seconds >= 0, "non-negative timestamp expected" milliseconds = round(seconds * 1000.0) diff --git a/daras_ai_v2/enum_selector_widget.py b/daras_ai_v2/enum_selector_widget.py index 495be07d5..049a426f8 100644 --- a/daras_ai_v2/enum_selector_widget.py +++ b/daras_ai_v2/enum_selector_widget.py @@ -21,7 +21,7 @@ def enum_multiselect( except AttributeError: deprecated = set() enums = [] - value = gui.session_state.get(key, []) + value = gui.session_state.get(key) or [] for e in enum_cls: if e in deprecated and e.name not in value: continue diff --git a/daras_ai_v2/language_model.py b/daras_ai_v2/language_model.py index 401d3d185..dffe49060 100644 --- a/daras_ai_v2/language_model.py +++ b/daras_ai_v2/language_model.py @@ -867,6 +867,8 @@ def _run_openai_chat( ) if stream: return _stream_openai_chunked(completion, used_model, messages) + if not completion or not completion.choices: + return [format_chat_entry(role=CHATML_ROLE_ASSISTANT, content="")] else: ret = [choice.message.dict() for choice in completion.choices] record_openai_llm_usage(used_model, completion, messages, ret) diff --git a/daras_ai_v2/serp_search.py b/daras_ai_v2/serp_search.py index d0eebd98e..b95586461 100644 --- a/daras_ai_v2/serp_search.py +++ b/daras_ai_v2/serp_search.py @@ -54,7 +54,7 @@ def get_links_from_serp_api( snippet=item.get("snippet") or "", ) for item in items - if (url := item.get("link") or item.get("website")) + if item and (url := item.get("link") or item.get("website")) ] return data, links