From 57636343f8d099653de2e4e026865c668f255fb3 Mon Sep 17 00:00:00 2001 From: sean1832 Date: Sun, 26 Mar 2023 20:16:20 +1100 Subject: [PATCH] feat: add cache for caption and retry attempt for caption fetch --- src/SumGPT.py | 1 - src/util.py | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/SumGPT.py b/src/SumGPT.py index bdc334e..bc9f445 100644 --- a/src/SumGPT.py +++ b/src/SumGPT.py @@ -94,7 +94,6 @@ if st.button("🚀 Run", disabled=exceeded): start_time = time.time() st.cache_data.clear() - util.clear_cache('summary') API_KEY = st.session_state['OPENAI_API_KEY'] if not API_KEY and not GPT.misc.validate_api_key(API_KEY): st.error("❌ Please enter a valid [OpenAI API key](https://beta.openai.com/account/api-keys).") diff --git a/src/util.py b/src/util.py index f4fd97d..9be04ec 100644 --- a/src/util.py +++ b/src/util.py @@ -42,9 +42,10 @@ def _extract_xml_caption(xml: str, is_auto_lang: bool) -> str: text_content = text return text_content.strip() - +@st.cache_data(show_spinner=False) def _get_caption(url: str, lang_code: str | List[str] = 'a.en') -> str: """Extracts the transcript from a YouTube video.""" + attempt = 3 yt = YouTube(url) caption = None selected_lang = None @@ -57,15 +58,18 @@ def _get_caption(url: str, lang_code: str | List[str] = 'a.en') -> str: except KeyError: continue # try next language + info_display = st.empty() + if caption is None: source_captions = yt.captions - try: - if source_captions == {}: - st.error(f'❌ No captions found in this video. Please try another one.') - except KeyError: - st.error(f'❌ Caption language currently not supported.\n\n' - f'{source_captions}\n\n' - f'Please [report this issue on Here](https://github.com/sean1832/SumGPT/issues)') + for i in range(attempt): + try: + if source_captions == {}: + info_display.error(f'❌ No captions found in this video. Please try another one.') + except KeyError: + info_display.error(f'❌ Caption language currently not supported.\n\n' + f'{source_captions}\n\n' + f'Please [report this issue on Here](https://github.com/sean1832/SumGPT/issues)') st.stop() else: