Skip to content

Commit

Permalink
remove state object
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Oct 29, 2024
1 parent ada9779 commit e0c9b19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
47 changes: 18 additions & 29 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,31 @@ async def main():
# Main chat interface
st.title("🎧 AudioCaster")

# Define application states
STATES = {
"INITIAL": not st.session_state.messages
and not st.session_state.user_specification,
"CHATTING": bool(st.session_state.messages)
and not st.session_state.user_specification,
"GENERATING": bool(st.session_state.user_specification)
and not st.session_state.current_audiocast,
"COMPLETED": bool(st.session_state.current_audiocast),
}

# Declare chat interface container
uichat = st.empty()
# Render based on current state
if STATES["GENERATING"]:
uichat.empty()

uichat.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)

elif STATES["COMPLETED"]:
st.info("Audiocast generation completed!")
render_audiocast()
else:
if not st.session_state.user_specification:
with uichat.container():
st.write(
"Tell me what you'd like to listen to, and I'll create an audiocast for you!"
)

if STATES["CHATTING"]:
if st.session_state.messages:
render_chat_history()
elif (
STATES["INITIAL"]
and not st.session_state.example_prompt
and not st.session_state.prompt
):
elif not st.session_state.example_prompt and not st.session_state.prompt:
display_example_cards()

if st.session_state.content_category:
content_category = st.session_state.content_category

if st.session_state.example_prompt:
handle_example_prompt(content_category)

if st.session_state.prompt:
prompt = st.session_state.prompt
st.session_state.prompt = None
ai_message = handle_user_prompt(prompt, content_category)

if isinstance(ai_message, str):
await evaluate_final_response(ai_message, content_category)

Expand All @@ -86,6 +63,18 @@ async def main():
st.session_state.messages.append({"role": "user", "content": prompt})
st.session_state.prompt = prompt
st.rerun()
else:
uichat.empty()
uichat = st.empty()
if not st.session_state.current_audiocast:
uichat.info("Using your audiocast specifications")

summary = st.session_state.user_specification
content_category = st.session_state.content_category
await use_audiocast_request(summary, content_category)
else:
st.info("Audiocast generation completed!")
render_audiocast()


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion src/utils/chat_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ async def use_audiocast_request(summary: str, content_category: ContentCategory)
GenerateAudioCastRequest(summary=summary, category=content_category)
)
print(f"Generate AudioCast Response: {audiocast_response}")

st.session_state.current_audiocast = audiocast_response
st.session_state.messages = [] # Clear messages
st.rerun()
except Exception as e:
st.info("Error while generating your audiocast. Please start afresh!")
st.error(f"Error generating audiocast: {str(e)}")
st.session_state.user_specification = None

st.session_state.user_specification = None
if st.button("Restart", use_container_width=True):
st.rerun()

0 comments on commit e0c9b19

Please sign in to comment.