Spaces:
Build error
Build error
Paul Ke commited on
Update streamlit_app.py
Browse files- streamlit_app.py +93 -0
streamlit_app.py
CHANGED
|
@@ -5,6 +5,18 @@ import string
|
|
| 5 |
import ffmpeg
|
| 6 |
from glob import glob
|
| 7 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Config
|
| 10 |
if "videos" not in st.session_state:
|
|
@@ -38,6 +50,42 @@ def _remove_all_videos():
|
|
| 38 |
except FileNotFoundError:
|
| 39 |
print("Couldn't delete file:", video)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# --- Main Functions ---
|
| 43 |
def download_video(url: str, save_path: str, **kwargs):
|
|
@@ -150,3 +198,48 @@ if st.session_state["videos"]:
|
|
| 150 |
)
|
| 151 |
except Exception as e:
|
| 152 |
st.error("Failed downloading the video", icon="😔")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import ffmpeg
|
| 6 |
from glob import glob
|
| 7 |
import streamlit as st
|
| 8 |
+
from phi.agent import Agent
|
| 9 |
+
from phi.model.google import Gemini
|
| 10 |
+
from phi.tools.duckduckgo import DuckDuckGo
|
| 11 |
+
from google.generativeai import upload_file, get_file
|
| 12 |
+
import time
|
| 13 |
+
import google.generativeai as genai
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
import tempfile
|
| 16 |
+
|
| 17 |
+
from dotenv import load_dotenv
|
| 18 |
+
load_dotenv()
|
| 19 |
+
|
| 20 |
|
| 21 |
# Config
|
| 22 |
if "videos" not in st.session_state:
|
|
|
|
| 50 |
except FileNotFoundError:
|
| 51 |
print("Couldn't delete file:", video)
|
| 52 |
|
| 53 |
+
#API KEY
|
| 54 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 55 |
+
if API_KEY:
|
| 56 |
+
genai.configure(api_key=API_KEY)
|
| 57 |
+
|
| 58 |
+
#Gemini Model
|
| 59 |
+
model_id = st.sidebar.text_input("Gemini Model", "gemini-2.0-flash-exp")
|
| 60 |
+
safety_settings = [ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE" }, ]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@st.cache_resource
|
| 64 |
+
def initialize_agent():
|
| 65 |
+
return Agent(
|
| 66 |
+
name="Video AI summarizer",
|
| 67 |
+
model=Gemini(id=model_id),
|
| 68 |
+
tools=[DuckDuckGo()],
|
| 69 |
+
markdown=True,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Initialize the agent
|
| 73 |
+
multimodal_Agent = initialize_agent()
|
| 74 |
+
|
| 75 |
+
def download_video(url: str, output_path: str):
|
| 76 |
+
"""Downloads a video using yt-dlp."""
|
| 77 |
+
ydl_opts = {
|
| 78 |
+
'outtmpl': output_path, # Path where the video will be saved
|
| 79 |
+
'format': 'best', # Download the best quality available
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 84 |
+
ydl.download([url])
|
| 85 |
+
return True, f"Video downloaded successfully to {output_path}"
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return False, str(e)
|
| 88 |
+
|
| 89 |
|
| 90 |
# --- Main Functions ---
|
| 91 |
def download_video(url: str, save_path: str, **kwargs):
|
|
|
|
| 198 |
)
|
| 199 |
except Exception as e:
|
| 200 |
st.error("Failed downloading the video", icon="😔")
|
| 201 |
+
|
| 202 |
+
# Analysis prompt
|
| 203 |
+
analysis_prompt = st.sidebar.text_area("Enter analysis")
|
| 204 |
+
|
| 205 |
+
if st.button('Generate the story'):
|
| 206 |
+
if not video_file:
|
| 207 |
+
st.sidebar.warning('PLEASE ENTER A VALID URL')
|
| 208 |
+
else:
|
| 209 |
+
# Download the video
|
| 210 |
+
with st.spinner("Retrieving video..."):
|
| 211 |
+
temp_video_path = os.path.join(tempfile.gettempdir(), 'video.mp4')
|
| 212 |
+
success, message = download_video(video_file, temp_video_path)
|
| 213 |
+
|
| 214 |
+
if not success:
|
| 215 |
+
st.sidebar.error(f"Error downloading video: {message}")
|
| 216 |
+
else:
|
| 217 |
+
st.sidebar.success(message)
|
| 218 |
+
|
| 219 |
+
# Display the video
|
| 220 |
+
st.sidebar.divider()
|
| 221 |
+
st.sidebar.video(temp_video_path, format="video/mp4", start_time=0, loop=True, autoplay=True)
|
| 222 |
+
|
| 223 |
+
try:
|
| 224 |
+
with st.spinner("Generating the story of the video"):
|
| 225 |
+
# Upload and process the video
|
| 226 |
+
processed_video = upload_file(temp_video_path)
|
| 227 |
+
while processed_video.state.name == "PROCESSING":
|
| 228 |
+
time.sleep(1)
|
| 229 |
+
processed_video = get_file(processed_video.name)
|
| 230 |
+
|
| 231 |
+
# AI agent processing
|
| 232 |
+
response = multimodal_Agent.run(analysis_prompt, videos=[processed_video], safety_settings=safety_settings)
|
| 233 |
+
|
| 234 |
+
st.subheader('Analysis Result')
|
| 235 |
+
st.markdown(response.content)
|
| 236 |
+
|
| 237 |
+
except Exception as error:
|
| 238 |
+
st.sidebar.error(f"An error occurred: {error}")
|
| 239 |
+
finally:
|
| 240 |
+
# Delete the downloaded video
|
| 241 |
+
try:
|
| 242 |
+
os.remove(temp_video_path)
|
| 243 |
+
# st.sidebar.info("Downloaded video deleted.")
|
| 244 |
+
except Exception as e:
|
| 245 |
+
st.sidebar.warning(f"Failed to delete video: {e}")
|