Spaces:
Running
Running
Update streamlit_app.py
Browse files- streamlit_app.py +32 -113
streamlit_app.py
CHANGED
@@ -1,97 +1,16 @@
|
|
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."):
|
17 |
-
# if not os.environ.get("HF_TOKEN"):
|
18 |
-
# st.warning("HF_TOKEN environment variable not found. Some Hugging Face models might require authentication and may fail to load.")
|
19 |
-
# tool = AccentAnalyzerTool()
|
20 |
-
# analysis_agent, follow_up_agent = create_agent(tool)
|
21 |
-
# return tool, analysis_agent, follow_up_agent
|
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"
|
31 |
-
# else:
|
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()
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
import streamlit as st
|
76 |
-
import os
|
77 |
-
import re
|
78 |
-
|
79 |
from src.tools.accent_tool import AccentAnalyzerTool
|
80 |
from src.app.main_agent import create_agent
|
81 |
from langchain_core.messages import HumanMessage, AIMessage
|
|
|
82 |
|
83 |
-
|
84 |
-
st.set_page_config(
|
85 |
-
page_title="Accent Analyzer Agent",
|
86 |
-
page_icon="💬",
|
87 |
-
layout="centered"
|
88 |
-
)
|
89 |
|
90 |
st.warning("⚠️ High latency due to CPU usage. Once migrated to GPU, response time will improve significantly.")
|
|
|
91 |
st.title("English Accent Analyzer (Conversational)")
|
92 |
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`")
|
93 |
|
94 |
-
# Load tools and agents
|
95 |
@st.cache_resource
|
96 |
def load_tool_and_agent():
|
97 |
with st.spinner("Loading AI models and tools... This might take a moment."):
|
@@ -103,15 +22,9 @@ def load_tool_and_agent():
|
|
103 |
|
104 |
accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent()
|
105 |
|
106 |
-
# Initialize session state variables
|
107 |
if "chat_history" not in st.session_state:
|
108 |
st.session_state.chat_history = []
|
109 |
-
if "pending_input" not in st.session_state:
|
110 |
-
st.session_state.pending_input = None
|
111 |
-
if "pending_spinner_msg" not in st.session_state:
|
112 |
-
st.session_state.pending_spinner_msg = ""
|
113 |
|
114 |
-
# Determine prompt type
|
115 |
if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
|
116 |
prompt_label = "Ask more about the video..."
|
117 |
input_key = "followup"
|
@@ -119,37 +32,43 @@ else:
|
|
119 |
prompt_label = "Paste your prompt here..."
|
120 |
input_key = "initial"
|
121 |
|
122 |
-
# Chat input field
|
123 |
user_input = st.chat_input(prompt_label, key=input_key)
|
124 |
|
125 |
-
#
|
|
|
|
|
|
|
126 |
if user_input:
|
127 |
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
128 |
-
|
129 |
if re.search(r'https?://\S+', user_input):
|
130 |
accent_tool_obj.last_transcript = ""
|
131 |
-
|
|
|
|
|
132 |
else:
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
for msg in st.session_state.chat_history:
|
138 |
with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
|
139 |
st.markdown(msg.content)
|
140 |
|
141 |
-
#
|
142 |
-
if
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
else:
|
148 |
-
result = follow_up_agent.invoke(st.session_state.chat_history).content
|
149 |
-
except Exception as e:
|
150 |
-
result = f"Error: {str(e)}"
|
151 |
-
st.session_state.chat_history.append(AIMessage(content=result))
|
152 |
-
# Clear flags
|
153 |
-
st.session_state.pending_input = None
|
154 |
-
st.session_state.pending_spinner_msg = ""
|
155 |
-
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()
|
72 |
+
|
73 |
+
|
74 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|