Manasa1 commited on
Commit
456bd3e
Β·
verified Β·
1 Parent(s): 68aa964

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -30
app.py CHANGED
@@ -1,21 +1,20 @@
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 google.generativeai as genai
7
 
8
  import time
9
  from pathlib import Path
10
-
11
  import tempfile
12
-
13
  from dotenv import load_dotenv
14
  load_dotenv()
15
 
16
  import os
17
 
18
- API_KEY=os.getenv("GOOGLE_API_KEY")
19
  if API_KEY:
20
  genai.configure(api_key=API_KEY)
21
 
@@ -29,24 +28,44 @@ st.set_page_config(
29
  st.title("Phidata Video AI Summarizer Agent πŸŽ₯πŸŽ€πŸ–¬")
30
  st.header("Powered by Gemini 2.0 Flash Exp")
31
 
32
-
33
  @st.cache_resource
34
  def initialize_agent():
35
  return Agent(
36
  name="Video AI Summarizer",
37
  model=Gemini(id="gemini-2.0-flash-exp"),
38
- tools=[DuckDuckGo()],
39
  markdown=True,
40
  )
41
 
42
- ## Initialize the agent
43
- multimodal_Agent=initialize_agent()
 
 
 
 
 
 
44
 
45
- # File uploader
46
  video_file = st.file_uploader(
47
  "Upload a video file", type=['mp4', 'mov', 'avi'], help="Upload a video for AI analysis"
48
  )
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  if video_file:
51
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
52
  temp_video.write(video_file.read())
@@ -54,13 +73,7 @@ if video_file:
54
 
55
  st.video(video_path, format="video/mp4", start_time=0)
56
 
57
- user_query = st.text_area(
58
- "What insights are you seeking from the video?",
59
- placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
60
- help="Provide specific questions or insights you want from the video."
61
- )
62
-
63
- if st.button("πŸ” Analyze Video", key="analyze_video_button"):
64
  if not user_query:
65
  st.warning("Please enter a question or insight to analyze the video.")
66
  else:
@@ -72,16 +85,8 @@ if video_file:
72
  time.sleep(1)
73
  processed_video = get_file(processed_video.name)
74
 
75
- # Prompt generation for analysis
76
- analysis_prompt = (
77
- f"""
78
- Analyze the uploaded video for content and context.
79
- Respond to the following query using video insights and supplementary web research:
80
- {user_query}
81
-
82
- Provide a detailed, user-friendly, and actionable response.
83
- """
84
- )
85
 
86
  # AI agent processing
87
  response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
@@ -95,8 +100,30 @@ if video_file:
95
  finally:
96
  # Clean up temporary video file
97
  Path(video_path).unlink(missing_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  else:
99
- st.info("Upload a video file to begin analysis.")
100
 
101
  # Customize text area height
102
  st.markdown(
@@ -109,5 +136,3 @@ st.markdown(
109
  """,
110
  unsafe_allow_html=True
111
  )
112
-
113
-
 
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 phi.tools.youtube_tools import YouTubeTools # Import YouTubeTools for YouTube summarization
6
+ from google.generativeai import upload_file, get_file
7
  import google.generativeai as genai
8
 
9
  import time
10
  from pathlib import Path
 
11
  import tempfile
 
12
  from dotenv import load_dotenv
13
  load_dotenv()
14
 
15
  import os
16
 
17
+ API_KEY = os.getenv("GOOGLE_API_KEY")
18
  if API_KEY:
19
  genai.configure(api_key=API_KEY)
20
 
 
28
  st.title("Phidata Video AI Summarizer Agent πŸŽ₯πŸŽ€πŸ–¬")
29
  st.header("Powered by Gemini 2.0 Flash Exp")
30
 
31
+ # Initialize the agent
32
  @st.cache_resource
33
  def initialize_agent():
34
  return Agent(
35
  name="Video AI Summarizer",
36
  model=Gemini(id="gemini-2.0-flash-exp"),
37
+ tools=[DuckDuckGo(), YouTubeTools()], # Add YouTubeTools here for YouTube video summarization
38
  markdown=True,
39
  )
40
 
41
+ multimodal_Agent = initialize_agent()
42
+
43
+ # Input field for user query
44
+ user_query = st.text_area(
45
+ "What insights are you seeking from the video?",
46
+ placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
47
+ help="Provide specific questions or insights you want from the video."
48
+ )
49
 
50
+ # Check for either video file upload or YouTube URL
51
  video_file = st.file_uploader(
52
  "Upload a video file", type=['mp4', 'mov', 'avi'], help="Upload a video for AI analysis"
53
  )
54
 
55
+ youtube_url = st.text_input("Or, paste a YouTube URL to summarize:")
56
+
57
+ # Unified analysis prompt
58
+ def generate_analysis_prompt(user_query, video_source):
59
+ return (
60
+ f"""
61
+ Analyze the {video_source} for content and context.
62
+ Respond to the following query using video insights and supplementary web research:
63
+ {user_query}
64
+ Provide a detailed, user-friendly, and actionable response.
65
+ """
66
+ )
67
+
68
+ # Process uploaded video
69
  if video_file:
70
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
71
  temp_video.write(video_file.read())
 
73
 
74
  st.video(video_path, format="video/mp4", start_time=0)
75
 
76
+ if st.button("πŸ” Analyze Uploaded Video", key="analyze_uploaded_video_button"):
 
 
 
 
 
 
77
  if not user_query:
78
  st.warning("Please enter a question or insight to analyze the video.")
79
  else:
 
85
  time.sleep(1)
86
  processed_video = get_file(processed_video.name)
87
 
88
+ # Use the unified prompt for analysis (Uploaded video)
89
+ analysis_prompt = generate_analysis_prompt(user_query, "uploaded video")
 
 
 
 
 
 
 
 
90
 
91
  # AI agent processing
92
  response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
 
100
  finally:
101
  # Clean up temporary video file
102
  Path(video_path).unlink(missing_ok=True)
103
+
104
+ # Process YouTube URL
105
+ elif youtube_url:
106
+ if st.button("πŸ” Summarize YouTube Video", key="summarize_youtube_button"):
107
+ if not user_query:
108
+ st.warning("Please enter a question or insight to analyze the YouTube video.")
109
+ else:
110
+ try:
111
+ with st.spinner("Fetching video captions and summarizing..."):
112
+ # Use the unified prompt for summarization (YouTube URL)
113
+ analysis_prompt = generate_analysis_prompt(user_query, "YouTube URL")
114
+
115
+ # Summarize the YouTube video using the agent
116
+ response = multimodal_Agent.get_response(f"{analysis_prompt} {youtube_url}")
117
+
118
+ # Display the result
119
+ st.subheader("Video Summary")
120
+ st.markdown(response["content"], unsafe_allow_html=True)
121
+
122
+ except Exception as error:
123
+ st.error(f"An error occurred: {error}")
124
+
125
  else:
126
+ st.info("Upload a video file or paste a YouTube URL to begin analysis.")
127
 
128
  # Customize text area height
129
  st.markdown(
 
136
  """,
137
  unsafe_allow_html=True
138
  )