File size: 6,155 Bytes
bda8450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import streamlit as st
from phi.agent import Agent
from phi.model.google import Gemini
from phi.tools.duckduckgo import DuckDuckGo
from google.generativeai import upload_file, get_file
import google.generativeai as genai
import time
from pathlib import Path
import tempfile
from dotenv import load_dotenv
import os
from phi.model.groq import Groq
from phi.tools.youtube_tools import YouTubeTools

# Load environment variables
load_dotenv()

# Configure API keys
API_KEY = os.getenv("GOOGLE_API_KEY")
groq_api_key = os.getenv("GROQ_API_KEY")
if API_KEY:
    genai.configure(api_key=API_KEY)

# Page configuration
st.set_page_config(
    page_title="Multimodal AI Applications",
    page_icon="🌐",
    layout="wide"
)

# Tabs for the two applications
tab1, tab2 = st.tabs(["Video Summarizer", "YouTube Video Analyzer"])

# Tab 1: Video Summarizer with Gemini
with tab1:
    st.title("Phidata Video AI Summarizer Agent πŸŽ₯πŸŽ€πŸ–¬")
    st.header("Powered by Gemini 2.0 Flash Exp")

    @st.cache_resource
    def initialize_agent():
        return Agent(
            name="Video AI Summarizer",
            model=Gemini(id="gemini-2.0-flash-exp"),
            tools=[DuckDuckGo()],
            markdown=True,
        )

    multimodal_Agent = initialize_agent()

    video_file = st.file_uploader(
        "Upload a video file", 
        type=['mp4'], 
        help="Upload a video for AI analysis"
    )

    if video_file:
        with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
            temp_video.write(video_file.read())
            video_path = temp_video.name

        st.video(video_path, format="video/mp4", start_time=0)

        user_query = st.text_area(
            "What insights are you seeking from the video?",
            placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed. This AI agent can search the internet.",
            help="Provide specific questions or insights you want from the video."
        )

        if st.button("πŸ” Analyze Video", key="analyze_video_button"):
            if not user_query:
                st.warning("Please enter a question or insight to analyze the video.")
            else:
                try:
                    with st.spinner("Processing video and gathering insights..."):
                        processed_video = upload_file(video_path)

                        while processed_video.state.name == "PROCESSING":
                            time.sleep(1)
                            processed_video = get_file(processed_video.name)

                        analysis_prompt = (
                            f"""
                            Analyze the uploaded video for content and context. Provide a detailed summary. Search the internet for similar topics discussed in the uploaded video.
                            Respond to the following query using video insights and supplementary web research: {user_query}
                            Provide a detailed, user-friendly, and actionable response. 
                            """
                        )

                        response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])

                        st.subheader("Analysis Result")
                        st.markdown(response.content)

                except Exception as error:
                    st.error(f"An error occurred during analysis: {error}")

                finally:
                    Path(video_path).unlink(missing_ok=True)
    else:
        st.info("Upload a video file to begin analysis.")

# Tab 2: YouTube Video Analyzer with Groq
with tab2:
    st.title("YouTube Video Analyzer")
    st.header("Analyze Youtube video using video link 🎬 🧷")

    try:
        youtube_agent = Agent(
            model=Groq(id="llama3-8b-8192", api_key=groq_api_key),
            tools=[YouTubeTools(), DuckDuckGo()],
            show_tool_calls=True,
            get_video_captions=True,
            get_video_data = True,
            description="You are a YouTube agent. Obtain the captions of a YouTube video, analyze its content and context, provide detailed summaries with key points, conduct web research on similar topics, and answer user questions.",
        )
    except Exception as e:
        st.error(f"Error initializing the agent: {e}")
        st.stop()

    video_url = st.text_input("Enter YouTube Video URL:", "")
    user_query = st.text_area("Enter your question about the video (optional):", "")

    if st.button("✨ Analyze the Video", key="Analyze_Video_Button"):
        if video_url:
            with st.spinner("Analyzing..."):
                try:
                    prompt_prefix = """Analyze the YouTube video provided by the URL. Provide a detailed summary of the video, including key points. 
                    Structure the output as follows:
                    **Detailed Summary:**
                    [Detailed summary of the video content]
                    **Key Points:**
                    * [Key point 1]
                    * [Key point 2]
                    * [Key point 3]
                    ... (and so on)
                    Conduct web research on similar topics discussed in the video to provide additional context.
                    """

                    if user_query:
                        prompt = f"""{prompt_prefix}
                                 Respond to the following query using video insights and supplementary web research:
                                          {user_query}
                                 Provide a detailed, user-friendly, and actionable response.
                                 Video URL: {video_url}"""
                    else:
                        prompt = f"""{prompt_prefix}
                                        Video URL: {video_url}"""

                    output = youtube_agent.run(prompt)

                    st.markdown(output.content)

                except Exception as e:
                    st.error(f"Error analyzing the video: {e}")
        else:
            st.warning("Please enter a YouTube video URL.")