Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Nov 1, 2024
1 parent 308d353 commit efedaa6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 80 deletions.
49 changes: 11 additions & 38 deletions pages/audiocast.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import asyncio
from pathlib import Path
from typing import cast

import pyperclip
import streamlit as st

from src.env_var import APP_URL
from src.utils.main_utils import get_audiocast
from src.utils.render_audiocast import parse_ai_script
from src.utils.waveform_utils import download_waveform_video, render_waveform


def navigate_to_home():
main_script = str(Path(__file__).parent.parent / "app.py")
st.switch_page(main_script)
from src.utils.render_audiocast_utils import (
GenerateAudiocastDict,
navigate_to_home,
render_audiocast_handler,
)


async def render_audiocast_page():
Expand All @@ -28,33 +25,9 @@ async def render_audiocast_page():

try:
with st.spinner("Loading audiocast..."):
audiocast = get_audiocast(session_id)

# Audio player
st.audio(audiocast["url"])

# Create placeholder for visualization
with st.expander("Show Waveform Visualization"):
viz = st.empty()
with viz.container():
try:
video_path = render_waveform(session_id, audiocast["url"])
if video_path:
# Download video
download_waveform_video(str(video_path))
except Exception as e:
st.error(f"Error rendering waveform: {str(e)}")

# Transcript
with st.expander("Show Transcript"):
st.markdown(parse_ai_script(audiocast["script"]))

# Metadata
st.sidebar.subheader("Audiocast Source")
st.sidebar.markdown(audiocast["source_content"])

share_url = f"{APP_URL}/audiocast?session_id={session_id}"
st.text_input("Share this audiocast:", share_url)
audiocast = cast(GenerateAudiocastDict, get_audiocast(session_id))

share_url = render_audiocast_handler(session_id, audiocast)

share_col, restart_row = st.columns(2, vertical_alignment="bottom")

Expand Down Expand Up @@ -83,8 +56,8 @@ async def render_audiocast_page():

st.markdown("---")

cola, _ = st.columns([3, 5])
with cola:
col1, _ = st.columns([3, 5])
with col1:
if st.button("← Back to Home", use_container_width=True):
navigate_to_home()

Expand Down
47 changes: 5 additions & 42 deletions src/utils/render_audiocast.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import re
from typing import TypedDict

import pyperclip
import streamlit as st

from src.env_var import APP_URL
from src.utils.render_audiocast_utils import (
GenerateAudiocastDict,
render_audiocast_handler,
)
from src.utils.session_state import reset_session
from src.utils.waveform_utils import download_waveform_video, render_waveform


class GenerateAudiocastDict(TypedDict):
url: str
script: str
source_content: str
created_at: str | None


def parse_ai_script(ai_script: str):
matches = re.findall(r"<(Speaker\d+)>(.*?)</Speaker\d+>", ai_script, re.DOTALL)
return "\n\n".join([f"**{speaker}**: {content}" for speaker, content in matches])


def render_audiocast(session_id: str):
Expand All @@ -29,31 +16,7 @@ def render_audiocast(session_id: str):
st.markdown("#### Your Audiocast")
current_audiocast: GenerateAudiocastDict = st.session_state.current_audiocast

# Audio player
st.audio(current_audiocast["url"])

# Create placeholder for visualization
with st.expander("Show Waveform Visualization"):
viz = st.empty()
with viz.container():
try:
video_path = render_waveform(session_id, current_audiocast["url"])
if video_path:
# Download video
download_waveform_video(str(video_path))
except Exception as e:
st.error(f"Error rendering waveform: {str(e)}")

# Transcript
with st.expander("Show Transcript"):
st.markdown(parse_ai_script(current_audiocast["script"]))

# Metadata
st.sidebar.subheader("Audiocast Source")
st.sidebar.markdown(current_audiocast["source_content"])

share_url = f"{APP_URL}/audiocast?session_id={session_id}"
st.text_input("Share this audiocast:", share_url)
share_url = render_audiocast_handler(session_id, current_audiocast)

share_col, restart_row = st.columns(2, vertical_alignment="bottom")

Expand Down
55 changes: 55 additions & 0 deletions src/utils/render_audiocast_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import re
from pathlib import Path
from typing import TypedDict

import streamlit as st

from src.env_var import APP_URL
from src.utils.waveform_utils import download_waveform_video, render_waveform


def navigate_to_home():
main_script = str(Path(__file__).parent.parent / "app.py")
st.switch_page(main_script)


def parse_ai_script(ai_script: str):
matches = re.findall(r"<(Speaker\d+)>(.*?)</Speaker\d+>", ai_script, re.DOTALL)
return "\n\n".join([f"**{speaker}**: {content}" for speaker, content in matches])


class GenerateAudiocastDict(TypedDict):
url: str
script: str
source_content: str
created_at: str | None


def render_audiocast_handler(session_id: str, audiocast: GenerateAudiocastDict):
# Audio player
st.audio(audiocast["url"])

# Create placeholder for visualization
with st.expander("Show Waveform Visualization"):
viz = st.empty()
with viz.container():
try:
video_path = render_waveform(session_id, audiocast["url"])
if video_path:
# Download video
download_waveform_video(str(video_path))
except Exception as e:
st.error(f"Error rendering waveform: {str(e)}")

# Transcript
with st.expander("Show Transcript"):
st.markdown(parse_ai_script(audiocast["script"]))

# Metadata
st.sidebar.subheader("Audiocast Source")
st.sidebar.markdown(audiocast["source_content"])

share_url = f"{APP_URL}/audiocast?session_id={session_id}"
st.text_input("Share this audiocast:", share_url)

return share_url

0 comments on commit efedaa6

Please sign in to comment.