From 97b222d7c42ce8b6d45c4f739acce5ca542570e4 Mon Sep 17 00:00:00 2001 From: Chukwuma Nwaugha Date: Tue, 5 Nov 2024 17:18:51 +0000 Subject: [PATCH] refactor: clean up sidebar and improve metadata subscription handling --- .gitignore | 6 +++++- app/index.py | 14 +++++--------- app/pages/audiocast.py | 10 ++++------ app/src/utils/metadata_subscription.py | 15 +++++++++------ app/src/utils/render_audiocast_utils.py | 8 ++++++-- shared_utils_pkg/session_manager.py | 3 +-- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 1fe4775..75ee30c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,8 @@ __pycache__ reference_code keys/ -.ruff_cache/ \ No newline at end of file +.ruff_cache/ + +node_modules/ + +.DS_Store diff --git a/app/index.py b/app/index.py index 8732fd4..88e8858 100644 --- a/app/index.py +++ b/app/index.py @@ -16,19 +16,15 @@ async def main(): st.subheader("Listen to anything, anytime, leveraging AI") st.sidebar.info("A VeedoAI project. (c) 2024") - # Sidebar for content type selection - st.sidebar.title("Audiocast Info") - session_id = init_session_state() if st.session_state.content_category: - st.sidebar.subheader( - f"Content Category: {st.session_state.content_category.capitalize()}" - ) + # Sidebar for content type selection + st.sidebar.title("Audiocast Info") + + st.sidebar.subheader(f"Content Category: {st.session_state.content_category.capitalize()}") else: - st.sidebar.markdown( - "> Your preferences and audiocast metadata will appear here" - ) + st.sidebar.markdown("> Your preferences and audiocast metadata will appear here") # Declare chat interface container uichat = st.empty() diff --git a/app/pages/audiocast.py b/app/pages/audiocast.py index 34e301e..e12d1b4 100644 --- a/app/pages/audiocast.py +++ b/app/pages/audiocast.py @@ -2,7 +2,6 @@ import streamlit as st from _init_project import print_project_meta - from src.utils.custom_components import copy_button from src.utils.render_audiocast_utils import ( get_audiocast, @@ -15,18 +14,17 @@ async def render_audiocast_page(): st.set_page_config(page_title="Audiora | Share Page", page_icon="🎧", layout="wide") + + # Display audiocast content + st.title("🎧 Audiora") + st.subheader("Share Page ") st.sidebar.info("A VeedoAI project. (c) 2024") session_id = st.query_params.get("session_id") if session_id: - # Display audiocast content - st.title("🎧 Audiora") - st.subheader("Share Page ") st.markdown(f"##### Viewing audiocast: _{session_id}_") - st.sidebar.info("A VeedoAI project. (c) 2024") - try: with st.spinner("Loading audiocast..."): audiocast = get_audiocast(session_id) diff --git a/app/src/utils/metadata_subscription.py b/app/src/utils/metadata_subscription.py index 458d3c9..333c54a 100644 --- a/app/src/utils/metadata_subscription.py +++ b/app/src/utils/metadata_subscription.py @@ -5,22 +5,25 @@ from shared_utils_pkg.session_manager import SessionManager -async def subscribe_to_audio_generation(session_id: str): +def subscribe_to_audio_generation(session_id: str): """Subscribe to audio generation metadata""" q = Queue() + + def handler(info: str | None): + if info: + q.put(info, block=False) + db = SessionManager(session_id) - doc_watch = db.subscribe_to_metadata_info( - lambda info: info and q.put(info, block=False) - ) + doc_watch = db.subscribe_to_metadata_info(handler) with st.empty(): while True: try: - info = q.get(timeout=1) + info = q.get(timeout=2) if not info: break st.info(info) except Exception: break - doc_watch.unsubscribe() + return doc_watch diff --git a/app/src/utils/render_audiocast_utils.py b/app/src/utils/render_audiocast_utils.py index bce3c36..04b4f99 100644 --- a/app/src/utils/render_audiocast_utils.py +++ b/app/src/utils/render_audiocast_utils.py @@ -4,9 +4,10 @@ import httpx import streamlit as st - -from env_var import APP_URL, API_URL +from src.utils.metadata_subscription import subscribe_to_audio_generation from src.utils.render_waveform import render_waveform + +from env_var import API_URL, APP_URL from shared_utils_pkg.audiocast_utils import GenerateAudioCastRequest, GenerateAudiocastDict from shared_utils_pkg.chat_utils import ContentCategory @@ -32,6 +33,8 @@ async def generate_audiocast( summary: str, content_category: ContentCategory, ): + doc_watch = subscribe_to_audio_generation(session_id) + audiocast_req = GenerateAudioCastRequest( sessionId=session_id, summary=summary, @@ -43,6 +46,7 @@ async def generate_audiocast( timeout=None, ) response.raise_for_status() + doc_watch.unsubscribe() return cast(GenerateAudiocastDict, response.json()) diff --git a/shared_utils_pkg/session_manager.py b/shared_utils_pkg/session_manager.py index 78ad4f5..4fc7251 100644 --- a/shared_utils_pkg/session_manager.py +++ b/shared_utils_pkg/session_manager.py @@ -126,8 +126,7 @@ def on_snapshot(doc_snapshot, _changes, _read_time): for doc in doc_snapshot: if doc.exists and doc.id == self.doc_id: data = doc.to_dict() - info = data.get("metadata", {}).get("info") - print(f"Document metadata info: {info}") + info = (data.get("metadata", {}) or {}).get("info") callback(info) return doc_ref.on_snapshot(on_snapshot)