codelion commited on
Commit
d638712
·
verified ·
1 Parent(s): 0425992

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
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 in plain text.
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
- try:
70
- key_frames_response = call_gemini(video_file, prompt)
71
- lines = key_frames_response.strip().split("\n")
72
- key_frames = []
73
- for line in lines:
74
- if " - " in line:
75
- parts = line.split(" - ", 1)
76
- timestamp = parts[0].strip()
77
- description = parts[1].strip()
78
- key_frames.append({"timestamp": timestamp, "description": description})
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 a single-step video analysis.
108
  First, call Gemini with a simple prompt to get a brief summary.
109
- Then, call Gemini to list key timestamps with descriptions.
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
- try:
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 extracted key frames with captions.
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.", []