Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .env +1 -0
- app.py +170 -0
- requirements.txt +4 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GOOGLE_API_KEY = "AIzaSyCLzew1lp8AgmA3269u17nBHu9ZPSn0oWM"
|
app.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import tempfile
|
10 |
+
from pathlib import Path
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
import os
|
16 |
+
|
17 |
+
GOOGLE_API_KEY=os.getenv("GOOGLE_API_KEY")
|
18 |
+
if GOOGLE_API_KEY:
|
19 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
20 |
+
|
21 |
+
# Page configuration
|
22 |
+
st.set_page_config(
|
23 |
+
page_title="AI Video Summarizer Tool",
|
24 |
+
page_icon="π₯",
|
25 |
+
layout="wide"
|
26 |
+
)
|
27 |
+
|
28 |
+
st.title("AI Video Summarizer Tool π₯")
|
29 |
+
st.write("This tool can summarize uploaded vidoes as well as videos from YouTube. It is powered by Gemini.")
|
30 |
+
|
31 |
+
|
32 |
+
@st.cache_resource
|
33 |
+
def initialize_gemini_agent():
|
34 |
+
return Agent(
|
35 |
+
name="AI Video Summarizer",
|
36 |
+
model=Gemini(id="gemini-2.0-flash-exp"),
|
37 |
+
tools=[DuckDuckGo()],
|
38 |
+
markdown=True,
|
39 |
+
)
|
40 |
+
|
41 |
+
## Initialize the agent
|
42 |
+
video_summarizer_agent=initialize_gemini_agent()
|
43 |
+
|
44 |
+
video_type_selection = st.selectbox('Please Select Input Type', ['Enter YouTube URL', 'Upload a video'])
|
45 |
+
|
46 |
+
if video_type_selection == 'Upload a video':
|
47 |
+
|
48 |
+
# File uploader
|
49 |
+
uploaded_video_file = st.file_uploader(
|
50 |
+
"Upload a video file", type=['mp4', 'mov', 'avi'], help="Upload a video for summarization"
|
51 |
+
)
|
52 |
+
|
53 |
+
if uploaded_video_file:
|
54 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_summarization_video:
|
55 |
+
temp_summarization_video.write(uploaded_video_file.read())
|
56 |
+
summarization_video_path = temp_summarization_video.name
|
57 |
+
|
58 |
+
st.video(summarization_video_path, format="video/mp4", start_time=0)
|
59 |
+
|
60 |
+
user_query = st.text_area(
|
61 |
+
"What insights are you seeking from the video?",
|
62 |
+
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
63 |
+
help="Provide specific questions or insights you want from the video."
|
64 |
+
)
|
65 |
+
|
66 |
+
if st.button("π Analyze Video", key="analyze_video_button"):
|
67 |
+
if not user_query:
|
68 |
+
st.warning("Please enter a question or insight to analyze the video.")
|
69 |
+
else:
|
70 |
+
try:
|
71 |
+
with st.spinner("Processing video and gathering insights..."):
|
72 |
+
# Upload and process video file
|
73 |
+
processed_video = upload_file(summarization_video_path)
|
74 |
+
while processed_video.state.name == "PROCESSING":
|
75 |
+
time.sleep(1)
|
76 |
+
processed_video = get_file(processed_video.name)
|
77 |
+
|
78 |
+
# Prompt generation for analysis
|
79 |
+
analysis_prompt = (
|
80 |
+
f"""
|
81 |
+
Analyze the uploaded video for content and context.
|
82 |
+
Respond to the following query using video insights and supplementary web research:
|
83 |
+
{user_query}
|
84 |
+
|
85 |
+
Provide a detailed, user-friendly, and actionable response.
|
86 |
+
"""
|
87 |
+
)
|
88 |
+
|
89 |
+
# AI agent processing
|
90 |
+
response = video_summarizer_agent.run(analysis_prompt, videos=[processed_video])
|
91 |
+
|
92 |
+
# Display the result
|
93 |
+
st.subheader("Results of Video Analysis")
|
94 |
+
st.markdown(response.content)
|
95 |
+
|
96 |
+
except Exception as error:
|
97 |
+
st.error(f"An error occurred during analysis: {error}")
|
98 |
+
finally:
|
99 |
+
# Clean up temporary video file
|
100 |
+
Path(summarization_video_path).unlink(missing_ok=True)
|
101 |
+
else:
|
102 |
+
st.info("Upload a video file to begin analysis.")
|
103 |
+
|
104 |
+
# Customize text area height
|
105 |
+
st.markdown(
|
106 |
+
"""
|
107 |
+
<style>
|
108 |
+
.stTextArea textarea {
|
109 |
+
height: 100px;
|
110 |
+
}
|
111 |
+
</style>
|
112 |
+
""",
|
113 |
+
unsafe_allow_html=True
|
114 |
+
)
|
115 |
+
|
116 |
+
elif video_type_selection == 'Enter YouTube URL':
|
117 |
+
# Input for YouTube video URL
|
118 |
+
video_url = st.text_input(
|
119 |
+
"Enter a YouTube video URL",
|
120 |
+
placeholder="Paste the URL of the video you'd like to summarize",
|
121 |
+
help="The app will analyze the content of the video based on your query."
|
122 |
+
)
|
123 |
+
|
124 |
+
if video_url:
|
125 |
+
user_query = st.text_area(
|
126 |
+
"What insights are you seeking from the video?",
|
127 |
+
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
128 |
+
help="Provide specific questions or insights you want from the video."
|
129 |
+
)
|
130 |
+
|
131 |
+
if st.button("π Analyze Video", key="analyze_video_button"):
|
132 |
+
if not user_query:
|
133 |
+
st.warning("Please enter a question or insight to analyze the video.")
|
134 |
+
else:
|
135 |
+
try:
|
136 |
+
with st.spinner("Analyzing video and gathering insights..."):
|
137 |
+
# Generate the analysis prompt
|
138 |
+
analysis_prompt = (
|
139 |
+
f"""
|
140 |
+
Analyze the YouTube video at the following URL: {video_url}.
|
141 |
+
Respond to the following query using the video content and supplementary web research:
|
142 |
+
{user_query}
|
143 |
+
|
144 |
+
Provide a detailed, user-friendly, and actionable response.
|
145 |
+
"""
|
146 |
+
)
|
147 |
+
|
148 |
+
# AI agent processing
|
149 |
+
response = video_summarizer_agent.run(analysis_prompt)
|
150 |
+
|
151 |
+
# Display the result
|
152 |
+
st.subheader("Results of Video Analysis")
|
153 |
+
st.markdown(response.content)
|
154 |
+
|
155 |
+
except Exception as error:
|
156 |
+
st.error(f"An error occurred during analysis: {error}")
|
157 |
+
else:
|
158 |
+
st.info("Enter a YouTube video URL to begin analysis.")
|
159 |
+
|
160 |
+
# Customize text area height
|
161 |
+
st.markdown(
|
162 |
+
"""
|
163 |
+
<style>
|
164 |
+
.stTextArea textarea {
|
165 |
+
height: 100px;
|
166 |
+
}
|
167 |
+
</style>
|
168 |
+
""",
|
169 |
+
unsafe_allow_html=True
|
170 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
phidata
|
3 |
+
google-generativeai
|
4 |
+
duckduckgo-search
|