Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,82 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from phi.agent import Agent
|
| 3 |
from phi.model.google import Gemini
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import tempfile
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
tmp_file.write(uploaded_file.getvalue())
|
| 31 |
-
temp_path = tmp_file.name
|
| 32 |
-
|
| 33 |
-
# Display the uploaded image
|
| 34 |
-
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
| 35 |
-
|
| 36 |
-
# Input for dynamic task
|
| 37 |
-
task_input = st.text_area(
|
| 38 |
-
"Enter your task/question for the AI Agent:"
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
# Button to process the image and task
|
| 42 |
-
if st.button("Analyze Image") and task_input:
|
| 43 |
-
with st.spinner("AI is thinking... 🤖"):
|
| 44 |
-
try:
|
| 45 |
-
# Call the agent with the dynamic task and image path
|
| 46 |
-
response = agent.run(task_input, images=[temp_path])
|
| 47 |
-
|
| 48 |
-
# Display the response from the model
|
| 49 |
-
st.markdown("### AI Response:")
|
| 50 |
-
st.markdown(response.content)
|
| 51 |
-
except Exception as e:
|
| 52 |
-
st.error(f"An error occurred during analysis: {str(e)}")
|
| 53 |
-
finally:
|
| 54 |
-
# Clean up temp file
|
| 55 |
-
if os.path.exists(temp_path):
|
| 56 |
-
os.unlink(temp_path)
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
st.error(f"An error occurred while processing the image: {str(e)}")
|
| 60 |
-
|
| 61 |
-
if __name__ == "__main__":
|
| 62 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from phi.agent import Agent
|
| 3 |
from phi.model.google import Gemini
|
| 4 |
+
from phi.tools.duckduckgo import DuckDuckGo
|
| 5 |
+
from google.generativeai import upload_file, get_file
|
| 6 |
+
import time
|
| 7 |
+
from pathlib import Path
|
| 8 |
import tempfile
|
|
|
|
| 9 |
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Multimodal AI Agent",
|
| 12 |
+
page_icon="🧬",
|
| 13 |
+
layout="wide"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
st.title("Multimodal AI Agent 🧬")
|
| 17 |
+
|
| 18 |
+
# Initialize single agent with both capabilities
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def initialize_agent():
|
| 21 |
+
return Agent(
|
| 22 |
+
name="Multimodal Analyst",
|
| 23 |
+
model=Gemini(id="gemini-2.0-flash-exp"),
|
| 24 |
+
tools=[DuckDuckGo()],
|
| 25 |
+
markdown=True,
|
| 26 |
)
|
| 27 |
|
| 28 |
+
agent = initialize_agent()
|
| 29 |
+
|
| 30 |
+
# File uploader
|
| 31 |
+
uploaded_file = st.file_uploader("Upload a video file", type=['mp4', 'mov', 'avi'])
|
| 32 |
|
| 33 |
+
if uploaded_file:
|
| 34 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
| 35 |
+
tmp_file.write(uploaded_file.read())
|
| 36 |
+
video_path = tmp_file.name
|
| 37 |
+
|
| 38 |
+
st.video(video_path)
|
| 39 |
+
|
| 40 |
+
user_prompt = st.text_area(
|
| 41 |
+
"What would you like to know?",
|
| 42 |
+
placeholder="Ask any question related to the video - the AI Agent will analyze it and search the web if needed",
|
| 43 |
+
help="You can ask questions about the video content and get relevant information from the web"
|
| 44 |
)
|
| 45 |
+
|
| 46 |
+
if st.button("Analyze & Research"):
|
| 47 |
+
if not user_prompt:
|
| 48 |
+
st.warning("Please enter your question.")
|
| 49 |
+
else:
|
| 50 |
+
try:
|
| 51 |
+
with st.spinner("Processing video and researching..."):
|
| 52 |
+
video_file = upload_file(video_path)
|
| 53 |
+
while video_file.state.name == "PROCESSING":
|
| 54 |
+
time.sleep(2)
|
| 55 |
+
video_file = get_file(video_file.name)
|
| 56 |
+
|
| 57 |
+
prompt = f"""
|
| 58 |
+
First analyze this video and then answer the following question using both
|
| 59 |
+
the video analysis and web research: {user_prompt}
|
| 60 |
+
|
| 61 |
+
Provide a comprehensive response focusing on practical, actionable information.
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
result = agent.run(prompt, videos=[video_file])
|
| 65 |
+
|
| 66 |
+
st.subheader("Result")
|
| 67 |
+
st.markdown(result.content)
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"An error occurred: {str(e)}")
|
| 71 |
+
finally:
|
| 72 |
+
Path(video_path).unlink(missing_ok=True)
|
| 73 |
+
else:
|
| 74 |
+
st.info("Please upload a video to begin analysis.")
|
| 75 |
|
| 76 |
+
st.markdown("""
|
| 77 |
+
<style>
|
| 78 |
+
.stTextArea textarea {
|
| 79 |
+
height: 100px;
|
| 80 |
+
}
|
| 81 |
+
</style>
|
| 82 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|