Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +57 -48
streamlit_app.py
CHANGED
@@ -13,6 +13,9 @@
|
|
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
|
@@ -67,80 +70,86 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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
|
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 |
-
|
97 |
-
analysis_agent, follow_up_agent = create_agent(tool)
|
98 |
-
return tool, analysis_agent, follow_up_agent
|
99 |
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
|
|
102 |
if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
|
103 |
-
|
|
|
104 |
else:
|
105 |
-
|
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 |
-
|
111 |
-
|
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.
|
117 |
-
agent_to_run = analysis_agent
|
118 |
-
spinner_msg = "Analyzing new video and transcribing audio..."
|
119 |
else:
|
120 |
-
|
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 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
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 |
+
# Page config
|
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."):
|
98 |
if not os.environ.get("HF_TOKEN"):
|
99 |
st.warning("HF_TOKEN environment variable not found. Some Hugging Face models might require authentication and may fail to load.")
|
100 |
+
tool = AccentAnalyzerTool()
|
101 |
+
analysis_agent, follow_up_agent = create_agent(tool)
|
102 |
+
return tool, analysis_agent, follow_up_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"
|
118 |
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 |
+
# Handle new user input (just queue for now)
|
126 |
+
if user_input:
|
127 |
+
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
128 |
+
st.session_state.pending_input = user_input
|
129 |
if re.search(r'https?://\S+', user_input):
|
130 |
+
accent_tool_obj.last_transcript = ""
|
131 |
+
st.session_state.pending_spinner_msg = "Analyzing new video..."
|
|
|
|
|
132 |
else:
|
133 |
+
st.session_state.pending_spinner_msg = "Responding based on transcript..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
st.rerun()
|
135 |
|
136 |
+
# Display chat history
|
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 |
+
# If input is queued, process it now
|
142 |
+
if st.session_state.pending_input:
|
143 |
+
with st.spinner(st.session_state.pending_spinner_msg):
|
144 |
+
try:
|
145 |
+
if re.search(r'https?://\S+', st.session_state.pending_input):
|
146 |
+
result = analysis_agent.invoke(st.session_state.chat_history)[-1].content
|
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()
|