Skip to content

Commit

Permalink
refine and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Oct 28, 2024
1 parent 2cee394 commit e56d6c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 46 deletions.
9 changes: 8 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
evaluate_final_response,
handle_example_prompt,
handle_user_prompt,
use_audiocast_request,
)
from src.utils.chat_utils import display_example_cards
from src.utils.render_audiocast import render_audiocast
Expand All @@ -29,7 +30,13 @@ async def main():
# Main chat interface
st.title("🎧 AudioCaster")

if st.session_state.generating_audiocast:
if st.session_state.user_specification:
st.info("Generating audiocast from your specifications")

summary = st.session_state.user_specification
content_category = st.session_state.content_category
await use_audiocast_request(summary, content_category)

if st.session_state.current_audiocast:
render_audiocast()
else:
Expand Down
59 changes: 21 additions & 38 deletions src/utils/chat_thread.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import re

import streamlit as st
Expand Down Expand Up @@ -89,52 +88,36 @@ async def evaluate_final_response(ai_message: str, content_category: ContentCate

col1, col2 = st.columns(2)
with col1:

def onclick(v: str):
st.session_state.user_specification = v

if st.button(
"Generate Audiocast",
use_container_width=True,
on_click=UseAudiocast(summary, content_category).run,
on_click=onclick,
args=(summary,),
):
st.rerun()
with col2:
if st.button("Restart", use_container_width=True, on_click=reset_session):
st.rerun()


class UseAudiocast:
summary: str
content_category: ContentCategory

def __init__(self, summary: str, content_category: ContentCategory):
self.summary = summary
self.content_category = content_category

def run(self):
"""
Run command to start generating audiocast
"""
st.session_state.generating_audiocast = True

async def handler():
await self.__use_audiocast_request(self.summary, self.content_category)

asyncio.run(handler())

async def __use_audiocast_request(
self, summary: str, content_category: ContentCategory
):
"""
Call audiocast creating workflow
async def use_audiocast_request(summary: str, content_category: ContentCategory):
"""
Call audiocast creating workflow
Args:
summary (str): user request summary or user specification
content_category (ContentCategory): content category
"""
with st.spinner("Generating your audiocast..."):
audiocast_response = await generate_audiocast(
GenerateAudioCastRequest(
summary=summary,
category=content_category,
)
Args:
summary (str): user request summary or user specification
content_category (ContentCategory): content category
"""
with st.spinner("Generating your audiocast..."):
audiocast_response = await generate_audiocast(
GenerateAudioCastRequest(
summary=summary,
category=content_category,
)
print(f"Generate AudioCast Response: {audiocast_response}")
st.session_state.current_audiocast = audiocast_response
)
print(f"Generate AudioCast Response: {audiocast_response}")
st.session_state.current_audiocast = audiocast_response
21 changes: 14 additions & 7 deletions src/utils/session_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ def init_session_state():
"""Initialize session state"""
if "chat_session_id" not in st.session_state:
st.session_state.chat_session_id = str(uuid.uuid4())

if "messages" not in st.session_state:
st.session_state.messages = cast(List[ChatMessage], [])
if "current_audiocast" not in st.session_state:
st.session_state.current_audiocast = None
if "example_prompt" not in st.session_state:
st.session_state.example_prompt = None
if "prompt" not in st.session_state:
st.session_state.prompt = None

if "content_category" not in st.session_state:
st.session_state.content_category = cast(ContentCategory | None, None)
if "generating_audiocast" not in st.session_state:
st.session_state.generating_audiocast = False

if "user_specification" not in st.session_state:
st.session_state.user_specification = None
if "current_audiocast" not in st.session_state:
st.session_state.current_audiocast = None


def reset_session():
Expand All @@ -37,11 +40,15 @@ def reset_session():
#### Client must call st.rerun()
"""
st.session_state.messages = []
st.session_state.chat_session_id = str(uuid.uuid4())
st.session_state.current_audiocast = None

st.session_state.messages = []
st.session_state.example_prompt = None
st.session_state.prompt = None
st.session_state.generating_audiocast = False

st.session_state.content_category = None

st.session_state.user_specification = None
st.session_state.current_audiocast = None

st.cache_data.clear()

0 comments on commit e56d6c8

Please sign in to comment.