From e213230c1f396c4ff162cd4cac4778ebd49946eb Mon Sep 17 00:00:00 2001 From: Varit Patel Date: Mon, 11 May 2026 19:34:49 +0100 Subject: [PATCH] improvement: reduce complexity of the Summarize tab --- ui/tabs/summarize_tab.py | 92 +++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 30 deletions(-) diff --git a/ui/tabs/summarize_tab.py b/ui/tabs/summarize_tab.py index b03849d..f3ac363 100644 --- a/ui/tabs/summarize_tab.py +++ b/ui/tabs/summarize_tab.py @@ -6,38 +6,70 @@ from utils import api_get, api_post +def _get_video_choices(videos: list) -> dict: + """Build video selection choices from video list.""" + return {f"{v['title'][:60]} ({v['id'][:8]})": v for v in videos} + + +def _render_no_videos_message(): + """Display message when no indexed videos are available.""" + st.info("No indexed videos yet. Go to the Ingest tab to add some.") + + +def _render_summary_results(response: dict): + """Render the summary and chapter results from API response.""" + st.subheader(f"Summary: {response['title']}") + st.caption(f"Based on {response['chunk_count']} indexed segments") + st.markdown(response["overall_summary"]) + + chapter_summaries = response.get("chapter_summaries") + if chapter_summaries: + _render_chapter_summaries(chapter_summaries) + + +def _render_chapter_summaries(chapter_summaries: list): + """Render individual chapter summaries as expanders.""" + st.subheader("Chapter Summaries") + for chapter in chapter_summaries: + with st.expander(chapter["chapter"]): + st.write(chapter["summary"]) + + +def _fetch_indexed_videos() -> list: + """Fetch list of indexed videos from API.""" + videos_data = api_get("/videos", params={"status": "indexed", "limit": 100}) + if videos_data is None: + return [] + return videos_data.get("videos", []) + + +def _handle_generate_summary(video_id: str, include_chapters: bool) -> dict | None: + """Call API to generate summary and return response.""" + with st.spinner("Summarizing... this may take a minute for long videos."): + return api_post( + "/summarize", + {"video_id": video_id, "include_chapters": include_chapters}, + ) + + def render_summarize_tab(): + """Render the summarize tab UI.""" st.header("Summarize a Video") st.caption("Generate an overall summary and per-chapter breakdown using map-reduce.") - videos_data = api_get("/videos", params={"status": "indexed", "limit": 100}) + videos = _fetch_indexed_videos() + + if not videos: + _render_no_videos_message() + return + + video_choices = _get_video_choices(videos) + selected_label = st.selectbox("Select a video", list(video_choices.keys())) + selected_video = video_choices[selected_label] + + include_chapters = st.checkbox("Include chapter summaries", value=True) - if videos_data and videos_data.get("videos"): - video_choices = {f"{v['title'][:60]} ({v['id'][:8]})": v for v in videos_data["videos"]} - selected_label = st.selectbox("Select a video", list(video_choices.keys())) - selected_video = video_choices[selected_label] - - include_chapters = st.checkbox("Include chapter summaries", value=True) - - if st.button("📝 Generate Summary", type="primary"): - with st.spinner("Summarizing... this may take a minute for long videos."): - resp = api_post( - "/summarize", - { - "video_id": selected_video["id"], - "include_chapters": include_chapters, - }, - ) - - if resp: - st.subheader(f"Summary: {resp['title']}") - st.caption(f"Based on {resp['chunk_count']} indexed segments") - st.markdown(resp["overall_summary"]) - - if resp.get("chapter_summaries"): - st.subheader("Chapter Summaries") - for ch in resp["chapter_summaries"]: - with st.expander(ch["chapter"]): - st.write(ch["summary"]) - else: - st.info("No indexed videos yet. Go to the Ingest tab to add some.") + if st.button("📝 Generate Summary", type="primary"): + response = _handle_generate_summary(selected_video["id"], include_chapters) + if response: + _render_summary_results(response)