Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +119 -41
streamlit_app.py
CHANGED
@@ -1,68 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
st.session_state.chat_history = []
|
24 |
|
25 |
if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
|
26 |
-
|
27 |
-
input_key = "followup"
|
28 |
else:
|
29 |
-
|
30 |
-
input_key = "initial"
|
31 |
-
|
32 |
-
user_input = st.chat_input(prompt_label, key=input_key)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
deferred_spinner_msg = ""
|
37 |
|
38 |
-
if user_input:
|
39 |
-
st.session_state.
|
|
|
40 |
|
41 |
if re.search(r'https?://\S+', user_input):
|
42 |
-
accent_tool_obj.last_transcript = ""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
else:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
for msg in st.session_state.chat_history:
|
63 |
with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
|
64 |
st.markdown(msg.content)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
process_response()
|
|
|
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 |
+
# tool = AccentAnalyzerTool()
|
17 |
+
# analysis_agent, follow_up_agent = create_agent(tool)
|
18 |
+
# return tool, analysis_agent, follow_up_agent
|
19 |
+
|
20 |
+
# accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent()
|
21 |
+
|
22 |
+
# if "chat_history" not in st.session_state:
|
23 |
+
# st.session_state.chat_history = []
|
24 |
+
|
25 |
+
# if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
|
26 |
+
# prompt_label = "Ask more about the video..."
|
27 |
+
# input_key = "followup"
|
28 |
+
# else:
|
29 |
+
# prompt_label = "Paste your prompt here..."
|
30 |
+
# input_key = "initial"
|
31 |
+
|
32 |
+
# user_input = st.chat_input(prompt_label, key=input_key)
|
33 |
+
|
34 |
+
# # Variable to defer assistant response
|
35 |
+
# deferred_response = None
|
36 |
+
# deferred_spinner_msg = ""
|
37 |
+
|
38 |
+
# if user_input:
|
39 |
+
# st.session_state.chat_history.append(HumanMessage(content=user_input))
|
40 |
+
|
41 |
+
# if re.search(r'https?://\S+', user_input):
|
42 |
+
# accent_tool_obj.last_transcript = ""
|
43 |
+
# deferred_spinner_msg = "Analyzing new video..."
|
44 |
+
# def run_agent():
|
45 |
+
# return analysis_agent.invoke(st.session_state.chat_history)[-1].content
|
46 |
+
# else:
|
47 |
+
# deferred_spinner_msg = "Responding based on transcript..."
|
48 |
+
# def run_agent():
|
49 |
+
# return follow_up_agent.invoke(st.session_state.chat_history).content
|
50 |
+
|
51 |
+
# # Run response generation inside spinner after chat is rendered
|
52 |
+
# def process_response():
|
53 |
+
# with st.spinner(deferred_spinner_msg):
|
54 |
+
# try:
|
55 |
+
# result = run_agent()
|
56 |
+
# except Exception as e:
|
57 |
+
# result = f"Error: {str(e)}"
|
58 |
+
# st.session_state.chat_history.append(AIMessage(content=result))
|
59 |
+
# st.rerun()
|
60 |
+
|
61 |
+
# # Display full chat history (before running spinner)
|
62 |
+
# for msg in st.session_state.chat_history:
|
63 |
+
# with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
|
64 |
+
# st.markdown(msg.content)
|
65 |
+
|
66 |
+
# # Only process response at the bottom, after chat is shown
|
67 |
+
# if user_input:
|
68 |
+
# process_response()
|
69 |
+
|
70 |
import streamlit as st
|
71 |
from src.tools.accent_tool import AccentAnalyzerTool
|
72 |
from src.app.main_agent import create_agent
|
73 |
from langchain_core.messages import HumanMessage, AIMessage
|
74 |
import re
|
75 |
+
import os
|
76 |
|
77 |
st.set_page_config(page_title="Accent Analyzer Agent", page_icon="💬", layout="centered")
|
78 |
|
79 |
st.warning("⚠️ High latency due to CPU usage. Once migrated to GPU, response time will improve significantly.")
|
|
|
80 |
st.title("English Accent Analyzer (Conversational)")
|
81 |
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`")
|
82 |
|
83 |
+
if "chat_history" not in st.session_state:
|
84 |
+
st.session_state.chat_history = []
|
85 |
+
if "last_transcript_available" not in st.session_state:
|
86 |
+
st.session_state.last_transcript_available = False
|
87 |
+
if "processing_input" not in st.session_state:
|
88 |
+
st.session_state.processing_input = False
|
89 |
+
|
90 |
@st.cache_resource
|
91 |
+
def load_tool_and_agent_cached():
|
92 |
+
with st.spinner("Loading AI models and tools... This might take a moment."):
|
93 |
+
if not os.environ.get("HF_TOKEN"):
|
94 |
+
st.warning("HF_TOKEN environment variable not found. Some Hugging Face models might require authentication and may fail to load.")
|
95 |
|
96 |
+
tool = AccentAnalyzerTool()
|
97 |
+
analysis_agent, follow_up_agent = create_agent(tool)
|
98 |
+
return tool, analysis_agent, follow_up_agent
|
99 |
|
100 |
+
accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent_cached()
|
|
|
101 |
|
102 |
if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
|
103 |
+
st.session_state.last_transcript_available = True
|
|
|
104 |
else:
|
105 |
+
st.session_state.last_transcript_available = False
|
|
|
|
|
|
|
106 |
|
107 |
+
prompt_label = "Ask more about the video..." if st.session_state.last_transcript_available else "Paste your prompt here..."
|
108 |
+
user_input = st.chat_input(prompt_label, disabled=st.session_state.processing_input)
|
|
|
109 |
|
110 |
+
if user_input and not st.session_state.processing_input:
|
111 |
+
st.session_state.processing_input = True
|
112 |
+
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
113 |
|
114 |
if re.search(r'https?://\S+', user_input):
|
115 |
+
accent_tool_obj.last_transcript = ""
|
116 |
+
st.session_state.last_transcript_available = False
|
117 |
+
agent_to_run = analysis_agent
|
118 |
+
spinner_msg = "Analyzing new video and transcribing audio..."
|
119 |
else:
|
120 |
+
agent_to_run = follow_up_agent
|
121 |
+
spinner_msg = "Generating response based on transcript..."
|
122 |
+
|
123 |
+
with st.spinner(spinner_msg):
|
124 |
+
try:
|
125 |
+
if agent_to_run == analysis_agent:
|
126 |
+
response_content = agent_to_run.invoke(st.session_state.chat_history)[-1].content
|
127 |
+
else:
|
128 |
+
response_content = follow_up_agent.invoke(st.session_state.chat_history).content
|
129 |
+
|
130 |
+
if agent_to_run == analysis_agent and hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
|
131 |
+
st.session_state.last_transcript_available = True
|
132 |
+
|
133 |
+
except Exception as e:
|
134 |
+
response_content = f"Error: {str(e)}"
|
135 |
+
st.error(f"An error occurred during processing: {e}")
|
136 |
+
|
137 |
+
st.session_state.chat_history.append(AIMessage(content=response_content))
|
138 |
+
st.session_state.processing_input = False
|
139 |
+
st.rerun()
|
140 |
+
|
141 |
for msg in st.session_state.chat_history:
|
142 |
with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
|
143 |
st.markdown(msg.content)
|
144 |
|
145 |
+
if not st.session_state.chat_history:
|
146 |
+
st.info("Start by pasting a public MP4 video URL above!")
|
|