accent-detection / streamlit_app.py
ash-171's picture
Update streamlit_app.py
0187514 verified
raw
history blame
6.18 kB
# import streamlit as st
# from src.tools.accent_tool import AccentAnalyzerTool
# from src.app.main_agent import create_agent
# from langchain_core.messages import HumanMessage, AIMessage
# import re
# st.set_page_config(page_title="Accent Analyzer Agent", page_icon="💬", layout="centered")
# st.warning("⚠️ High latency due to CPU usage. Once migrated to GPU, response time will improve significantly.")
# st.title("English Accent Analyzer (Conversational)")
# 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`")
# @st.cache_resource
# def load_tool_and_agent():
# tool = AccentAnalyzerTool()
# analysis_agent, follow_up_agent = create_agent(tool)
# return tool, analysis_agent, follow_up_agent
# accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent()
# if "chat_history" not in st.session_state:
# st.session_state.chat_history = []
# if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
# prompt_label = "Ask more about the video..."
# input_key = "followup"
# else:
# prompt_label = "Paste your prompt here..."
# input_key = "initial"
# user_input = st.chat_input(prompt_label, key=input_key)
# # Variable to defer assistant response
# deferred_response = None
# deferred_spinner_msg = ""
# if user_input:
# st.session_state.chat_history.append(HumanMessage(content=user_input))
# if re.search(r'https?://\S+', user_input):
# accent_tool_obj.last_transcript = ""
# deferred_spinner_msg = "Analyzing new video..."
# def run_agent():
# return analysis_agent.invoke(st.session_state.chat_history)[-1].content
# else:
# deferred_spinner_msg = "Responding based on transcript..."
# def run_agent():
# return follow_up_agent.invoke(st.session_state.chat_history).content
# # Run response generation inside spinner after chat is rendered
# def process_response():
# with st.spinner(deferred_spinner_msg):
# try:
# result = run_agent()
# except Exception as e:
# result = f"Error: {str(e)}"
# st.session_state.chat_history.append(AIMessage(content=result))
# st.rerun()
# # Display full chat history (before running spinner)
# for msg in st.session_state.chat_history:
# with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
# st.markdown(msg.content)
# # Only process response at the bottom, after chat is shown
# if user_input:
# process_response()
import streamlit as st
from src.tools.accent_tool import AccentAnalyzerTool
from src.app.main_agent import create_agent
from langchain_core.messages import HumanMessage, AIMessage
import re
import os
st.set_page_config(page_title="Accent Analyzer Agent", page_icon="💬", layout="centered")
st.warning("⚠️ High latency due to CPU usage. Once migrated to GPU, response time will improve significantly.")
st.title("English Accent Analyzer (Conversational)")
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`")
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "last_transcript_available" not in st.session_state:
st.session_state.last_transcript_available = False
if "processing_input" not in st.session_state:
st.session_state.processing_input = False
@st.cache_resource
def load_tool_and_agent_cached():
with st.spinner("Loading AI models and tools... This might take a moment."):
if not os.environ.get("HF_TOKEN"):
st.warning("HF_TOKEN environment variable not found. Some Hugging Face models might require authentication and may fail to load.")
tool = AccentAnalyzerTool()
analysis_agent, follow_up_agent = create_agent(tool)
return tool, analysis_agent, follow_up_agent
accent_tool_obj, analysis_agent, follow_up_agent = load_tool_and_agent_cached()
if hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
st.session_state.last_transcript_available = True
else:
st.session_state.last_transcript_available = False
prompt_label = "Ask more about the video..." if st.session_state.last_transcript_available else "Paste your prompt here..."
user_input = st.chat_input(prompt_label, disabled=st.session_state.processing_input)
if user_input and not st.session_state.processing_input:
st.session_state.processing_input = True
st.session_state.chat_history.append(HumanMessage(content=user_input))
if re.search(r'https?://\S+', user_input):
accent_tool_obj.last_transcript = ""
st.session_state.last_transcript_available = False
agent_to_run = analysis_agent
spinner_msg = "Analyzing new video and transcribing audio..."
else:
agent_to_run = follow_up_agent
spinner_msg = "Generating response based on transcript..."
with st.spinner(spinner_msg):
try:
if agent_to_run == analysis_agent:
response_content = agent_to_run.invoke(st.session_state.chat_history)[-1].content
else:
response_content = follow_up_agent.invoke(st.session_state.chat_history).content
if agent_to_run == analysis_agent and hasattr(accent_tool_obj, "last_transcript") and accent_tool_obj.last_transcript:
st.session_state.last_transcript_available = True
except Exception as e:
response_content = f"Error: {str(e)}"
st.error(f"An error occurred during processing: {e}")
st.session_state.chat_history.append(AIMessage(content=response_content))
st.session_state.processing_input = False
st.rerun()
for msg in st.session_state.chat_history:
with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
st.markdown(msg.content)
if not st.session_state.chat_history:
st.info("Start by pasting a public MP4 video URL above!")