Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,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 google.generativeai import upload_file, get_file
|
| 6 |
+
import time
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import tempfile
|
| 10 |
+
import os
|
| 11 |
+
import yt_dlp
|
| 12 |
+
|
| 13 |
+
from dotenv import load_dotenv
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 17 |
+
if API_KEY:
|
| 18 |
+
genai.configure(api_key=API_KEY)
|
| 19 |
+
|
| 20 |
+
# Page Configuration
|
| 21 |
+
st.set_page_config(
|
| 22 |
+
page_title="Multimodal AI agent - Video summarizer",
|
| 23 |
+
layout="wide"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
st.title("Phidata Video Summarizer AI agent")
|
| 27 |
+
st.header("Powered by Gemini 2.0 flash exp")
|
| 28 |
+
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def initialize_agent():
|
| 31 |
+
return Agent(
|
| 32 |
+
name="Video AI summarizer",
|
| 33 |
+
model=Gemini(id="gemini-2.0-flash-exp"),
|
| 34 |
+
tools=[DuckDuckGo()],
|
| 35 |
+
markdown=True,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Initialize the agent
|
| 39 |
+
multimodal_Agent = initialize_agent()
|
| 40 |
+
|
| 41 |
+
def download_tiktok_video(url: str, output_path: str):
|
| 42 |
+
"""Downloads a TikTok video using yt-dlp."""
|
| 43 |
+
ydl_opts = {
|
| 44 |
+
'outtmpl': output_path, # Path where the video will be saved
|
| 45 |
+
'format': 'best', # Download the best quality available
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 50 |
+
ydl.download([url])
|
| 51 |
+
return True, f"Video downloaded successfully to {output_path}"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return False, str(e)
|
| 54 |
+
|
| 55 |
+
# URL input for TikTok video
|
| 56 |
+
video_url = st.text_input("Enter TikTok video URL")
|
| 57 |
+
|
| 58 |
+
if st.button('Download and Analyze Video'):
|
| 59 |
+
if not video_url:
|
| 60 |
+
st.warning('PLEASE ENTER A VALID URL')
|
| 61 |
+
else:
|
| 62 |
+
# Download the TikTok video
|
| 63 |
+
with st.spinner("Downloading video..."):
|
| 64 |
+
temp_video_path = os.path.join(tempfile.gettempdir(), 'tiktok_video.mp4')
|
| 65 |
+
success, message = download_tiktok_video(video_url, temp_video_path)
|
| 66 |
+
|
| 67 |
+
if not success:
|
| 68 |
+
st.error(f"Error downloading video: {message}")
|
| 69 |
+
else:
|
| 70 |
+
st.success(message)
|
| 71 |
+
|
| 72 |
+
# Display the video
|
| 73 |
+
st.video(temp_video_path, format="video/mp4", start_time=0)
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
with st.spinner("Processing video and gathering insights"):
|
| 77 |
+
# Upload and process the video
|
| 78 |
+
processed_video = upload_file(temp_video_path)
|
| 79 |
+
while processed_video.state.name == "PROCESSING":
|
| 80 |
+
time.sleep(1)
|
| 81 |
+
processed_video = get_file(processed_video.name)
|
| 82 |
+
|
| 83 |
+
# Analysis prompt
|
| 84 |
+
analysis_prompt = (
|
| 85 |
+
f'''
|
| 86 |
+
Analyze the video thoroughly and provide a comprehensive storyline. The total generation should not be more than 500 words. As a whole from the video, detail the following aspects:
|
| 87 |
+
|
| 88 |
+
1. **Story:** How the set scene introduced and tone is set. What is happening in the scene? Describe key visuals and actions.
|
| 89 |
+
2. **Characters**: Identify each character, noting their expressions, attire, actions, and interactions. Highlight emotional nuances and gestures.
|
| 90 |
+
1. **Narration or Voiceover**: Describe what types of narrations or voiceovers are used in the video.
|
| 91 |
+
3. **Transitions and Pacing**: Describe scene changes, including cuts, fades, zooms, or shifts in focus, and how they affect the flow.
|
| 92 |
+
4. **Mood and Tone**: Capture the overall mood and tone of each scene, mentioning any music or sound effects that enhance these elements.
|
| 93 |
+
5. **Visible Texts or Brandings**: Capture all the visible texts from the videos and brandings too only if some brandings of product or service is done.
|
| 94 |
+
|
| 95 |
+
Conclude with a cohesive summary that ties the scenes together, highlighting all promotional themes, ensuring each brand and key text is accurately referenced in the storyline.
|
| 96 |
+
'''
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# AI agent processing
|
| 100 |
+
response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
|
| 101 |
+
|
| 102 |
+
st.subheader('Analysis Result')
|
| 103 |
+
st.markdown(response.content)
|
| 104 |
+
|
| 105 |
+
except Exception as error:
|
| 106 |
+
st.error(f"An error occurred: {error}")
|
| 107 |
+
finally:
|
| 108 |
+
# Delete the downloaded TikTok video
|
| 109 |
+
try:
|
| 110 |
+
os.remove(temp_video_path)
|
| 111 |
+
st.info("Downloaded TikTok video deleted.")
|
| 112 |
+
except Exception as e:
|
| 113 |
+
st.warning(f"Failed to delete video: {e}")
|