Spaces:
Sleeping
Sleeping
Create streamlit_app.py
Browse files- streamlit_app.py +66 -0
streamlit_app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.title("English Accent Analyzer (Conversational)")
|
10 |
+
st.subheader("Ask me to analyze a video URL, e.g.: \n\n `Analyze this video: https://your-link.com/video.mp4`")
|
11 |
+
|
12 |
+
@st.cache_resource
|
13 |
+
def load_tool_and_agent():
|
14 |
+
tool = AccentAnalyzerTool()
|
15 |
+
analysis_agent, follow_up_agent = create_agent(tool)
|
16 |
+
return tool, analysis_agent, follow_up_agent
|
17 |
+
|
18 |
+
accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent()
|
19 |
+
|
20 |
+
if "chat_history" not in st.session_state:
|
21 |
+
st.session_state.chat_history = []
|
22 |
+
|
23 |
+
if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
|
24 |
+
prompt_label = "Ask more about the video..."
|
25 |
+
input_key = "followup"
|
26 |
+
else:
|
27 |
+
prompt_label = "Paste your prompt here..."
|
28 |
+
input_key = "initial"
|
29 |
+
|
30 |
+
user_input = st.chat_input(prompt_label, key=input_key)
|
31 |
+
|
32 |
+
# Variable to defer assistant response
|
33 |
+
deferred_response = None
|
34 |
+
deferred_spinner_msg = ""
|
35 |
+
|
36 |
+
if user_input:
|
37 |
+
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
38 |
+
|
39 |
+
if re.search(r'https?://\S+', user_input):
|
40 |
+
accent_tool_obj.last_transcript = ""
|
41 |
+
deferred_spinner_msg = "Analyzing new video..."
|
42 |
+
def run_agent():
|
43 |
+
return analysis_agent.invoke(st.session_state.chat_history)[-1].content
|
44 |
+
else:
|
45 |
+
deferred_spinner_msg = "Responding based on transcript..."
|
46 |
+
def run_agent():
|
47 |
+
return follow_up_agent.invoke(st.session_state.chat_history).content
|
48 |
+
|
49 |
+
# Run response generation inside spinner after chat is rendered
|
50 |
+
def process_response():
|
51 |
+
with st.spinner(deferred_spinner_msg):
|
52 |
+
try:
|
53 |
+
result = run_agent()
|
54 |
+
except Exception as e:
|
55 |
+
result = f"Error: {str(e)}"
|
56 |
+
st.session_state.chat_history.append(AIMessage(content=result))
|
57 |
+
st.rerun()
|
58 |
+
|
59 |
+
# Display full chat history (before running spinner)
|
60 |
+
for msg in st.session_state.chat_history:
|
61 |
+
with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
|
62 |
+
st.markdown(msg.content)
|
63 |
+
|
64 |
+
# Only process response at the bottom, after chat is shown
|
65 |
+
if user_input:
|
66 |
+
process_response()
|