Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from phi.agent import Agent
|
3 |
+
from phi.model.google import Gemini
|
4 |
+
from phi.model.groq import Groq
|
5 |
+
from phi.tools.duckduckgo import DuckDuckGo
|
6 |
+
from phi.tools.youtube_tools import YouTubeTools
|
7 |
+
from google.generativeai import upload_file, get_file
|
8 |
+
import google.generativeai as genai
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
import time
|
11 |
+
import tempfile
|
12 |
+
import os
|
13 |
+
from pathlib import Path
|
14 |
+
|
15 |
+
# Load environment variables
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
# Google API Key
|
19 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
20 |
+
if API_KEY:
|
21 |
+
genai.configure(api_key=API_KEY)
|
22 |
+
|
23 |
+
# Groq API Key
|
24 |
+
groq_api_key = os.getenv("Groq_Api_Key")
|
25 |
+
|
26 |
+
# Initialize Multimodal Agent
|
27 |
+
def initialize_multimodal_agent():
|
28 |
+
return Agent(
|
29 |
+
name="Video AI Summarizer",
|
30 |
+
model=Gemini(id="gemini-2.0-flash-exp"),
|
31 |
+
tools=[DuckDuckGo()],
|
32 |
+
markdown=True,
|
33 |
+
)
|
34 |
+
|
35 |
+
# Video Analysis Function
|
36 |
+
def analyze_video(video_file, user_query):
|
37 |
+
if not user_query:
|
38 |
+
return "Please enter a question or insight to analyze the video."
|
39 |
+
|
40 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
|
41 |
+
temp_video.write(video_file.read())
|
42 |
+
video_path = temp_video.name
|
43 |
+
|
44 |
+
# Upload and process video
|
45 |
+
try:
|
46 |
+
processed_video = upload_file(video_path)
|
47 |
+
while processed_video.state.name == "PROCESSING":
|
48 |
+
time.sleep(1)
|
49 |
+
processed_video = get_file(processed_video.name)
|
50 |
+
|
51 |
+
analysis_prompt = f"""
|
52 |
+
Analyze the uploaded video for content and context.
|
53 |
+
Respond to the following query using video insights and supplementary web research:
|
54 |
+
{user_query}
|
55 |
+
|
56 |
+
Provide a detailed, user-friendly, and actionable response.
|
57 |
+
"""
|
58 |
+
|
59 |
+
multimodal_agent = initialize_multimodal_agent()
|
60 |
+
response = multimodal_agent.run(analysis_prompt, videos=[processed_video])
|
61 |
+
|
62 |
+
# Clean up temporary video file
|
63 |
+
Path(video_path).unlink(missing_ok=True)
|
64 |
+
|
65 |
+
return response.content
|
66 |
+
|
67 |
+
except Exception as error:
|
68 |
+
Path(video_path).unlink(missing_ok=True)
|
69 |
+
return f"An error occurred during analysis: {error}"
|
70 |
+
|
71 |
+
# YouTube Summarization Function
|
72 |
+
def summarize_youtube(video_url):
|
73 |
+
if not video_url.strip():
|
74 |
+
return "Please enter a valid YouTube video URL."
|
75 |
+
|
76 |
+
try:
|
77 |
+
youtube_agent = Agent(
|
78 |
+
tools=[YouTubeTools()],
|
79 |
+
model=Groq(id="llama-3.3-70b-versatile", api_key=groq_api_key),
|
80 |
+
show_tool_calls=True,
|
81 |
+
description="You are a YouTube agent. Obtain the captions of a YouTube video and answer questions.",
|
82 |
+
)
|
83 |
+
|
84 |
+
response = youtube_agent.print_response(f"Summarize this video {video_url}", markdown=True)
|
85 |
+
return response
|
86 |
+
|
87 |
+
except Exception as e:
|
88 |
+
return f"An error occurred: {e}"
|
89 |
+
|
90 |
+
# Gradio Interface
|
91 |
+
with gr.Blocks() as demo:
|
92 |
+
gr.Markdown("# Phidata Video AI Summarizer Agent π₯π€π¬")
|
93 |
+
|
94 |
+
with gr.Tab("π₯ Video Upload"):
|
95 |
+
gr.Markdown("### π₯ Analyze Uploaded Video")
|
96 |
+
video_file = gr.File(label="Upload a video file", type="file", file_types=["mp4", "mov", "avi"])
|
97 |
+
user_query = gr.Textbox(label="What insights are you seeking from the video?", placeholder="Ask anything about the video content.")
|
98 |
+
analyze_button = gr.Button("π Analyze Video")
|
99 |
+
analysis_result = gr.Textbox(label="Analysis Result", interactive=False)
|
100 |
+
|
101 |
+
analyze_button.click(fn=analyze_video, inputs=[video_file, user_query], outputs=analysis_result)
|
102 |
+
|
103 |
+
with gr.Tab("π YouTube Summarizer"):
|
104 |
+
gr.Markdown("### π Summarize YouTube Videos")
|
105 |
+
video_url = gr.Textbox(label="Enter YouTube video URL:")
|
106 |
+
summarize_button = gr.Button("Summarize Video")
|
107 |
+
youtube_summary = gr.Textbox(label="Summary", interactive=False)
|
108 |
+
|
109 |
+
summarize_button.click(fn=summarize_youtube, inputs=video_url, outputs=youtube_summary)
|
110 |
+
|
111 |
+
# Launch the interface
|
112 |
+
demo.launch()
|