fixed a few bug - multimodal agent working perfect now

This commit is contained in:
Madhu 2025-03-17 19:41:15 +05:30
parent fc02a7f3dd
commit 352d8f2162

View file

@ -1,8 +1,7 @@
import streamlit as st import streamlit as st
from agno.agent import Agent from agno.agent import Agent
from agno.models.google import Gemini from agno.models.google import Gemini
from agno.tools.duckduckgo import DuckDuckGoTools from agno.media import Video
from google.generativeai import upload_file, get_file
import time import time
from pathlib import Path from pathlib import Path
import tempfile import tempfile
@ -15,22 +14,25 @@ st.set_page_config(
st.title("Multimodal AI Agent 🧬") st.title("Multimodal AI Agent 🧬")
# Get Gemini API key from user
gemini_api_key = st.text_input("Enter your Gemini API Key", type="password")
# Initialize single agent with both capabilities # Initialize single agent with both capabilities
@st.cache_resource @st.cache_resource
def initialize_agent(): def initialize_agent(api_key):
return Agent( return Agent(
name="Multimodal Analyst", name="Multimodal Analyst",
model=Gemini(id="gemini-2.0-flash-exp"), model=Gemini(id="gemini-2.0-flash", api_key=api_key),
tools=[DuckDuckGoTools()],
markdown=True, markdown=True,
) )
agent = initialize_agent() if gemini_api_key:
agent = initialize_agent(gemini_api_key)
# File uploader # File uploader
uploaded_file = st.file_uploader("Upload a video file", type=['mp4', 'mov', 'avi']) uploaded_file = st.file_uploader("Upload a video file", type=['mp4', 'mov', 'avi'])
if uploaded_file: if uploaded_file:
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file: with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
tmp_file.write(uploaded_file.read()) tmp_file.write(uploaded_file.read())
video_path = tmp_file.name video_path = tmp_file.name
@ -49,10 +51,7 @@ if uploaded_file:
else: else:
try: try:
with st.spinner("Processing video and researching..."): with st.spinner("Processing video and researching..."):
video_file = upload_file(video_path) video = Video(filepath=video_path)
while video_file.state.name == "PROCESSING":
time.sleep(2)
video_file = get_file(video_file.name)
prompt = f""" prompt = f"""
First analyze this video and then answer the following question using both First analyze this video and then answer the following question using both
@ -61,7 +60,7 @@ if uploaded_file:
Provide a comprehensive response focusing on practical, actionable information. Provide a comprehensive response focusing on practical, actionable information.
""" """
result = agent.run(prompt, videos=[video_file]) result = agent.run(prompt, videos=[video])
st.subheader("Result") st.subheader("Result")
st.markdown(result.content) st.markdown(result.content)
@ -70,8 +69,10 @@ if uploaded_file:
st.error(f"An error occurred: {str(e)}") st.error(f"An error occurred: {str(e)}")
finally: finally:
Path(video_path).unlink(missing_ok=True) Path(video_path).unlink(missing_ok=True)
else: else:
st.info("Please upload a video to begin analysis.") st.info("Please upload a video to begin analysis.")
else:
st.warning("Please enter your Gemini API key to continue.")
st.markdown(""" st.markdown("""
<style> <style>