Skip to content

Commit

Permalink
refactor: clean up sidebar and improve metadata subscription handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Nov 5, 2024
1 parent eac192a commit 97b222d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ __pycache__
reference_code

keys/
.ruff_cache/
.ruff_cache/

node_modules/

.DS_Store
14 changes: 5 additions & 9 deletions app/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 4 additions & 6 deletions app/pages/audiocast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions app/src/utils/metadata_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions app/src/utils/render_audiocast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -43,6 +46,7 @@ async def generate_audiocast(
timeout=None,
)
response.raise_for_status()
doc_watch.unsubscribe()

return cast(GenerateAudiocastDict, response.json())

Expand Down
3 changes: 1 addition & 2 deletions shared_utils_pkg/session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 97b222d

Please sign in to comment.