Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ MODEL_NAME = "gemini-2.0-flash-001"
|
|
| 21 |
def call_gemini(video_file: str, prompt: str) -> str:
|
| 22 |
"""
|
| 23 |
Call the Gemini model with the provided video file and prompt.
|
| 24 |
-
The video is read as bytes and passed with MIME type "video/mp4",
|
| 25 |
and the prompt is wrapped as a text part.
|
| 26 |
"""
|
| 27 |
with open(video_file, "rb") as f:
|
|
@@ -35,6 +35,16 @@ def call_gemini(video_file: str, prompt: str) -> str:
|
|
| 35 |
)
|
| 36 |
return response.text
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def hhmmss_to_seconds(time_str: str) -> float:
|
| 39 |
"""
|
| 40 |
Convert a HH:MM:SS formatted string into seconds.
|
|
@@ -50,7 +60,7 @@ def hhmmss_to_seconds(time_str: str) -> float:
|
|
| 50 |
|
| 51 |
def get_key_frames(video_file: str, summary: str, user_query: str) -> list:
|
| 52 |
"""
|
| 53 |
-
Ask Gemini to output key timestamps and descriptions
|
| 54 |
The prompt instructs the model to output one line per event in the format:
|
| 55 |
HH:MM:SS - description
|
| 56 |
We then parse these lines and extract the corresponding frames using OpenCV.
|
|
@@ -61,24 +71,20 @@ def get_key_frames(video_file: str, summary: str, user_query: str) -> list:
|
|
| 61 |
"List the key timestamps in the video and a brief description of the event at that time. "
|
| 62 |
"Output one line per event in the following format: HH:MM:SS - description. Do not include any extra text."
|
| 63 |
)
|
| 64 |
-
# Append the summary (and user query if provided) so the model has context.
|
| 65 |
prompt += f" Video Summary: {summary}"
|
| 66 |
if user_query:
|
| 67 |
prompt += f" Focus on: {user_query}"
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
except Exception as e:
|
| 80 |
-
print("Error in key frame extraction:", e)
|
| 81 |
-
key_frames = []
|
| 82 |
|
| 83 |
extracted_frames = []
|
| 84 |
cap = cv2.VideoCapture(video_file)
|
|
@@ -104,24 +110,20 @@ def get_key_frames(video_file: str, summary: str, user_query: str) -> list:
|
|
| 104 |
|
| 105 |
def analyze_video(video_file: str, user_query: str) -> (str, list):
|
| 106 |
"""
|
| 107 |
-
Perform
|
| 108 |
First, call Gemini with a simple prompt to get a brief summary.
|
| 109 |
-
Then, call Gemini to list key timestamps
|
| 110 |
|
| 111 |
Returns:
|
| 112 |
- A Markdown report summarizing the video.
|
| 113 |
- A gallery list of key frames (each as a tuple of (image, caption)).
|
| 114 |
"""
|
| 115 |
-
# Use a very simple prompt for summary.
|
| 116 |
summary_prompt = "Summarize this video."
|
| 117 |
if user_query:
|
| 118 |
summary_prompt += f" Also focus on: {user_query}"
|
| 119 |
-
|
| 120 |
-
summary = call_gemini(video_file, summary_prompt)
|
| 121 |
-
except Exception as e:
|
| 122 |
-
summary = f"[Error in summary extraction: {e}]"
|
| 123 |
-
markdown_report = f"## Video Analysis Report\n\n**Summary:**\n\n{summary}\n"
|
| 124 |
|
|
|
|
| 125 |
key_frames_gallery = get_key_frames(video_file, summary, user_query)
|
| 126 |
if not key_frames_gallery:
|
| 127 |
markdown_report += "\n*No key frames were extracted.*\n"
|
|
@@ -134,7 +136,7 @@ def analyze_video(video_file: str, user_query: str) -> (str, list):
|
|
| 134 |
def gradio_interface(video_file, user_query: str) -> (str, list):
|
| 135 |
"""
|
| 136 |
Gradio interface function that accepts an uploaded video file and an optional query,
|
| 137 |
-
then returns a Markdown report and a gallery of
|
| 138 |
"""
|
| 139 |
if not video_file:
|
| 140 |
return "Please upload a valid video file.", []
|
|
|
|
| 21 |
def call_gemini(video_file: str, prompt: str) -> str:
|
| 22 |
"""
|
| 23 |
Call the Gemini model with the provided video file and prompt.
|
| 24 |
+
The video file is read as bytes and passed with MIME type "video/mp4",
|
| 25 |
and the prompt is wrapped as a text part.
|
| 26 |
"""
|
| 27 |
with open(video_file, "rb") as f:
|
|
|
|
| 35 |
)
|
| 36 |
return response.text
|
| 37 |
|
| 38 |
+
def safe_call_gemini(video_file: str, prompt: str) -> str:
|
| 39 |
+
"""
|
| 40 |
+
Wrapper for call_gemini that catches exceptions and returns a fallback string.
|
| 41 |
+
"""
|
| 42 |
+
try:
|
| 43 |
+
return call_gemini(video_file, prompt)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print("Gemini call failed:", e)
|
| 46 |
+
return "No summary available."
|
| 47 |
+
|
| 48 |
def hhmmss_to_seconds(time_str: str) -> float:
|
| 49 |
"""
|
| 50 |
Convert a HH:MM:SS formatted string into seconds.
|
|
|
|
| 60 |
|
| 61 |
def get_key_frames(video_file: str, summary: str, user_query: str) -> list:
|
| 62 |
"""
|
| 63 |
+
Ask Gemini to output key timestamps and descriptions as plain text.
|
| 64 |
The prompt instructs the model to output one line per event in the format:
|
| 65 |
HH:MM:SS - description
|
| 66 |
We then parse these lines and extract the corresponding frames using OpenCV.
|
|
|
|
| 71 |
"List the key timestamps in the video and a brief description of the event at that time. "
|
| 72 |
"Output one line per event in the following format: HH:MM:SS - description. Do not include any extra text."
|
| 73 |
)
|
|
|
|
| 74 |
prompt += f" Video Summary: {summary}"
|
| 75 |
if user_query:
|
| 76 |
prompt += f" Focus on: {user_query}"
|
| 77 |
|
| 78 |
+
# Use the safe call to get a response or fallback text.
|
| 79 |
+
key_frames_response = safe_call_gemini(video_file, prompt)
|
| 80 |
+
lines = key_frames_response.strip().split("\n")
|
| 81 |
+
key_frames = []
|
| 82 |
+
for line in lines:
|
| 83 |
+
if " - " in line:
|
| 84 |
+
parts = line.split(" - ", 1)
|
| 85 |
+
timestamp = parts[0].strip()
|
| 86 |
+
description = parts[1].strip()
|
| 87 |
+
key_frames.append({"timestamp": timestamp, "description": description})
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
extracted_frames = []
|
| 90 |
cap = cv2.VideoCapture(video_file)
|
|
|
|
| 110 |
|
| 111 |
def analyze_video(video_file: str, user_query: str) -> (str, list):
|
| 112 |
"""
|
| 113 |
+
Perform video analysis on the uploaded file.
|
| 114 |
First, call Gemini with a simple prompt to get a brief summary.
|
| 115 |
+
Then, call Gemini to list key timestamps and descriptions.
|
| 116 |
|
| 117 |
Returns:
|
| 118 |
- A Markdown report summarizing the video.
|
| 119 |
- A gallery list of key frames (each as a tuple of (image, caption)).
|
| 120 |
"""
|
|
|
|
| 121 |
summary_prompt = "Summarize this video."
|
| 122 |
if user_query:
|
| 123 |
summary_prompt += f" Also focus on: {user_query}"
|
| 124 |
+
summary = safe_call_gemini(video_file, summary_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
markdown_report = f"## Video Analysis Report\n\n**Summary:**\n\n{summary}\n"
|
| 127 |
key_frames_gallery = get_key_frames(video_file, summary, user_query)
|
| 128 |
if not key_frames_gallery:
|
| 129 |
markdown_report += "\n*No key frames were extracted.*\n"
|
|
|
|
| 136 |
def gradio_interface(video_file, user_query: str) -> (str, list):
|
| 137 |
"""
|
| 138 |
Gradio interface function that accepts an uploaded video file and an optional query,
|
| 139 |
+
then returns a Markdown report and a gallery of key frame images with captions.
|
| 140 |
"""
|
| 141 |
if not video_file:
|
| 142 |
return "Please upload a valid video file.", []
|