ash-171 commited on
Commit
e2d43c5
·
verified ·
1 Parent(s): ba5a9e5

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +28 -36
streamlit_app.py CHANGED
@@ -1,17 +1,16 @@
1
  import streamlit as st
2
- import os
3
- import re
4
  from src.tools.accent_tool import AccentAnalyzerTool
5
  from src.app.main_agent import create_agent
6
  from langchain_core.messages import HumanMessage, AIMessage
 
7
 
8
  st.set_page_config(page_title="Accent Analyzer Agent", page_icon="💬", layout="centered")
 
9
  st.warning("⚠️ High latency due to CPU usage. Once migrated to GPU, response time will improve significantly.")
10
 
11
  st.title("English Accent Analyzer (Conversational)")
12
- st.subheader("Ask me to analyze a video URL, e.g.: \n\n `Analyze this video: https://github.com/ash-171/Data-mp4/raw/refs/heads/main/NWRNVTFlRGlnV0FfNDgwcA_out.mp4`")
13
 
14
- # Load tool and agent
15
  @st.cache_resource
16
  def load_tool_and_agent():
17
  with st.spinner("Loading AI models and tools... This might take a moment."):
@@ -23,17 +22,9 @@ def load_tool_and_agent():
23
 
24
  accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent()
25
 
26
- # Session state initialization
27
  if "chat_history" not in st.session_state:
28
  st.session_state.chat_history = []
29
 
30
- if "pending_user_input" not in st.session_state:
31
- st.session_state.pending_user_input = None
32
-
33
- if "pending_spinner_msg" not in st.session_state:
34
- st.session_state.pending_spinner_msg = ""
35
-
36
- # Prompt configuration
37
  if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
38
  prompt_label = "Ask more about the video..."
39
  input_key = "followup"
@@ -41,39 +32,40 @@ else:
41
  prompt_label = "Paste your prompt here..."
42
  input_key = "initial"
43
 
44
- # Chat input
45
  user_input = st.chat_input(prompt_label, key=input_key)
46
 
47
- # Queue input for processing
 
 
 
48
  if user_input:
49
  st.session_state.chat_history.append(HumanMessage(content=user_input))
50
- st.session_state.pending_user_input = user_input
51
  if re.search(r'https?://\S+', user_input):
52
  accent_tool_obj.last_transcript = ""
53
- st.session_state.pending_spinner_msg = "Analyzing new video..."
 
 
54
  else:
55
- st.session_state.pending_spinner_msg = "Responding based on transcript..."
56
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- # Display chat history
59
  for msg in st.session_state.chat_history:
60
  with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
61
  st.markdown(msg.content)
62
 
63
- # Process pending input (after chat is rendered)
64
- if st.session_state.pending_user_input:
65
- with st.spinner(st.session_state.pending_spinner_msg):
66
- try:
67
- if re.search(r'https?://\S+', st.session_state.pending_user_input):
68
- response = analysis_agent.invoke(st.session_state.chat_history)[-1].content
69
- else:
70
- response = follow_up_agent.invoke(st.session_state.chat_history).content
71
- except Exception as e:
72
- response = f"Error: {str(e)}"
73
-
74
- st.session_state.chat_history.append(AIMessage(content=response))
75
-
76
- # Clear pending flags
77
- st.session_state.pending_user_input = None
78
- st.session_state.pending_spinner_msg = ""
79
- st.rerun()
 
1
  import streamlit as st
 
 
2
  from src.tools.accent_tool import AccentAnalyzerTool
3
  from src.app.main_agent import create_agent
4
  from langchain_core.messages import HumanMessage, AIMessage
5
+ import re
6
 
7
  st.set_page_config(page_title="Accent Analyzer Agent", page_icon="💬", layout="centered")
8
+
9
  st.warning("⚠️ High latency due to CPU usage. Once migrated to GPU, response time will improve significantly.")
10
 
11
  st.title("English Accent Analyzer (Conversational)")
12
+ st.subheader("Ask me to analyze a video URL, e.g.: \n\n Analyze this video: https://github.com/ash-171/Data-mp4/raw/refs/heads/main/NWRNVTFlRGlnV0FfNDgwcA_out.mp4")
13
 
 
14
  @st.cache_resource
15
  def load_tool_and_agent():
16
  with st.spinner("Loading AI models and tools... This might take a moment."):
 
22
 
23
  accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent()
24
 
 
25
  if "chat_history" not in st.session_state:
26
  st.session_state.chat_history = []
27
 
 
 
 
 
 
 
 
28
  if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
29
  prompt_label = "Ask more about the video..."
30
  input_key = "followup"
 
32
  prompt_label = "Paste your prompt here..."
33
  input_key = "initial"
34
 
 
35
  user_input = st.chat_input(prompt_label, key=input_key)
36
 
37
+ # Variable to defer assistant response
38
+ deferred_response = None
39
+ deferred_spinner_msg = ""
40
+
41
  if user_input:
42
  st.session_state.chat_history.append(HumanMessage(content=user_input))
43
+
44
  if re.search(r'https?://\S+', user_input):
45
  accent_tool_obj.last_transcript = ""
46
+ deferred_spinner_msg = "Analyzing new video..."
47
+ def run_agent():
48
+ return analysis_agent.invoke(st.session_state.chat_history)[-1].content
49
  else:
50
+ deferred_spinner_msg = "Responding based on transcript..."
51
+ def run_agent():
52
+ return follow_up_agent.invoke(st.session_state.chat_history).content
53
+
54
+ # Run response generation inside spinner after chat is rendered
55
+ def process_response():
56
+ with st.spinner(deferred_spinner_msg):
57
+ try:
58
+ result = run_agent()
59
+ except Exception as e:
60
+ result = f"Error: {str(e)}"
61
+ st.session_state.chat_history.append(AIMessage(content=result))
62
+ st.rerun()
63
 
64
+ # Display full chat history (before running spinner)
65
  for msg in st.session_state.chat_history:
66
  with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
67
  st.markdown(msg.content)
68
 
69
+ # Only process response at the bottom, after chat is shown
70
+ if user_input:
71
+ process_response()