Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from google import genai
|
| 4 |
+
from google.genai import types
|
| 5 |
+
from google.genai.types import Part
|
| 6 |
+
from tenacity import retry, stop_after_attempt, wait_random_exponential
|
| 7 |
+
|
| 8 |
+
# Retrieve API key from environment variable
|
| 9 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 10 |
+
if not GOOGLE_API_KEY:
|
| 11 |
+
raise ValueError("Please set the GOOGLE_API_KEY environment variable.")
|
| 12 |
+
|
| 13 |
+
# Initialize the Gemini API client via AI Studio
|
| 14 |
+
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 15 |
+
|
| 16 |
+
# Use the Gemini 2.0 Flash model as required
|
| 17 |
+
MODEL_NAME = "gemini-2.0-flash-001"
|
| 18 |
+
|
| 19 |
+
@retry(wait=wait_random_exponential(multiplier=1, max=60), stop=stop_after_attempt(3))
|
| 20 |
+
def call_gemini(video_url: str, prompt: str) -> str:
|
| 21 |
+
"""
|
| 22 |
+
Call the Gemini model with the provided video URL and prompt.
|
| 23 |
+
The video is referenced by its URI (expecting a publicly accessible URL) and passed as a Part.
|
| 24 |
+
"""
|
| 25 |
+
response = client.models.generate_content(
|
| 26 |
+
model=MODEL_NAME,
|
| 27 |
+
contents=[
|
| 28 |
+
Part.from_uri(file_uri=video_url, mime_type="video/webm"),
|
| 29 |
+
prompt,
|
| 30 |
+
],
|
| 31 |
+
)
|
| 32 |
+
return response.text
|
| 33 |
+
|
| 34 |
+
def analyze_video(video_url: str) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Perform iterative, agentic video analysis.
|
| 37 |
+
In each iteration, the Gemini model refines its analysis based on previous output.
|
| 38 |
+
"""
|
| 39 |
+
analysis = ""
|
| 40 |
+
num_iterations = 3
|
| 41 |
+
|
| 42 |
+
for i in range(num_iterations):
|
| 43 |
+
if i == 0:
|
| 44 |
+
prompt = (
|
| 45 |
+
"You are a video analysis agent focusing on security and surveillance. "
|
| 46 |
+
"Provide a detailed summary of the video, highlighting any key events, suspicious activities, or anomalies."
|
| 47 |
+
)
|
| 48 |
+
else:
|
| 49 |
+
prompt = (
|
| 50 |
+
f"Based on the previous analysis: \"{analysis}\". "
|
| 51 |
+
"Provide further elaboration and refined insights, focusing on potential security threats, anomalous events, "
|
| 52 |
+
"and any details that may help a security team understand the situation better."
|
| 53 |
+
)
|
| 54 |
+
try:
|
| 55 |
+
analysis = call_gemini(video_url, prompt)
|
| 56 |
+
except Exception as e:
|
| 57 |
+
analysis += f"\n[Error during iteration {i+1}: {e}]"
|
| 58 |
+
break # Exit if an error occurs
|
| 59 |
+
return analysis
|
| 60 |
+
|
| 61 |
+
def gradio_interface(video_url: str) -> str:
|
| 62 |
+
"""
|
| 63 |
+
Gradio interface function that takes a video URL and returns the analysis.
|
| 64 |
+
"""
|
| 65 |
+
if not video_url:
|
| 66 |
+
return "Please provide a valid video URL."
|
| 67 |
+
return analyze_video(video_url)
|
| 68 |
+
|
| 69 |
+
# Define and launch the Gradio interface
|
| 70 |
+
iface = gr.Interface(
|
| 71 |
+
fn=gradio_interface,
|
| 72 |
+
inputs=gr.Textbox(label="Video URL (publicly accessible, e.g., YouTube link)"),
|
| 73 |
+
outputs=gr.Textbox(label="Security & Surveillance Analysis"),
|
| 74 |
+
title="AI Video Analysis and Summariser Agent",
|
| 75 |
+
description=(
|
| 76 |
+
"This agentic video analysis tool uses Google's Gemini 2.0 Flash model via AI Studio "
|
| 77 |
+
"to iteratively analyze a video for security and surveillance insights. It makes repeated "
|
| 78 |
+
"LLM calls to refine its analysis of the video content."
|
| 79 |
+
)
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
iface.launch()
|