codelion commited on
Commit
9f6408c
·
verified ·
1 Parent(s): 99b4eed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -10
app.py CHANGED
@@ -82,9 +82,11 @@ def extract_key_frames(video_file: str, key_frames_response: str) -> list:
82
  # Strip Markdown code block if present
83
  cleaned_response = key_frames_response.strip()
84
  if cleaned_response.startswith("```json") and cleaned_response.endswith("```"):
85
- cleaned_response = cleaned_response[7:-3].strip() # Remove ```json and ```
86
  elif cleaned_response.startswith("```") and cleaned_response.endswith("```"):
87
- cleaned_response = cleaned_response[3:-3].strip() # Remove generic ```
 
 
88
 
89
  try:
90
  # Try parsing as JSON
@@ -92,15 +94,21 @@ def extract_key_frames(video_file: str, key_frames_response: str) -> list:
92
  if not isinstance(key_frames, list):
93
  raise ValueError("Response is not a list.")
94
  except json.JSONDecodeError as e:
95
- print(f"JSON parsing failed: {str(e)}. Raw response: {key_frames_response}")
96
- # Fallback: Attempt to parse plain text with timecodes (e.g., "00:00:03 - Scene")
97
  key_frames = []
98
- lines = key_frames_response.strip().split("\n")
99
  for line in lines:
 
 
 
100
  if " - " in line:
101
  timestamp, title = line.split(" - ", 1)
102
  key_frames.append({"timecode": timestamp.strip(), "title": title.strip()})
103
- elif ":" in line and len(line.split(":")) == 3: # Rough check for HH:MM:SS
 
 
 
104
  key_frames.append({"timecode": line.strip(), "title": "Untitled"})
105
 
106
  for frame in key_frames:
@@ -155,11 +163,26 @@ def analyze_video(video_file: str, user_query: str) -> tuple[str, list]:
155
  )
156
  summary = summary_response.text
157
 
158
- # Step 2: Extract key frames
159
  key_frames_prompt = (
160
  "Identify key frames in this video and return them as a JSON array. "
161
  "Each object must have 'timecode' (in HH:MM:SS format) and 'title' describing the scene. "
162
- "Ensure the response is valid JSON."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  )
164
  if user_query:
165
  key_frames_prompt += f" Focus on: {user_query}"
@@ -170,7 +193,7 @@ def analyze_video(video_file: str, user_query: str) -> tuple[str, list]:
170
  )
171
  key_frames = extract_key_frames(video_file, key_frames_response.text)
172
 
173
- # Generate Markdown report (without Video URI)
174
  markdown_report = (
175
  "## Video Analysis Report\n\n"
176
  f"**Summary:**\n{summary}\n"
@@ -180,7 +203,7 @@ def analyze_video(video_file: str, user_query: str) -> tuple[str, list]:
180
  for i, (_, caption) in enumerate(key_frames, 1):
181
  markdown_report += f"- Frame {i}: {caption}\n"
182
  else:
183
- markdown_report += "\n*No key frames extracted. The model may not have returned valid JSON.*\n"
184
 
185
  return markdown_report, key_frames
186
 
 
82
  # Strip Markdown code block if present
83
  cleaned_response = key_frames_response.strip()
84
  if cleaned_response.startswith("```json") and cleaned_response.endswith("```"):
85
+ cleaned_response = cleaned_response[7:-3].strip()
86
  elif cleaned_response.startswith("```") and cleaned_response.endswith("```"):
87
+ cleaned_response = cleaned_response[3:-3].strip()
88
+
89
+ print(f"Cleaned key frames response: {cleaned_response}") # Debug output
90
 
91
  try:
92
  # Try parsing as JSON
 
94
  if not isinstance(key_frames, list):
95
  raise ValueError("Response is not a list.")
96
  except json.JSONDecodeError as e:
97
+ print(f"JSON parsing failed: {str(e)}. Falling back to text parsing.")
98
+ # Fallback: Parse plain text with timecodes (e.g., "00:00:03 - Scene" or "00:00:03: Scene")
99
  key_frames = []
100
+ lines = cleaned_response.strip().split("\n")
101
  for line in lines:
102
+ line = line.strip()
103
+ if not line:
104
+ continue
105
  if " - " in line:
106
  timestamp, title = line.split(" - ", 1)
107
  key_frames.append({"timecode": timestamp.strip(), "title": title.strip()})
108
+ elif ": " in line and len(line.split(":")[0]) == 2: # Check for HH:MM:SS format
109
+ timestamp, title = line.split(": ", 1)
110
+ key_frames.append({"timecode": timestamp.strip(), "title": title.strip()})
111
+ elif len(line.split(":")) == 3: # Rough check for standalone HH:MM:SS
112
  key_frames.append({"timecode": line.strip(), "title": "Untitled"})
113
 
114
  for frame in key_frames:
 
163
  )
164
  summary = summary_response.text
165
 
166
+ # Step 2: Extract key frames with few-shot examples
167
  key_frames_prompt = (
168
  "Identify key frames in this video and return them as a JSON array. "
169
  "Each object must have 'timecode' (in HH:MM:SS format) and 'title' describing the scene. "
170
+ "Ensure the response is valid JSON. Here are examples of the expected format:\n"
171
+ "Example 1: For a video of a car chase:\n"
172
+ "```json\n"
173
+ "[\n"
174
+ " {\"timecode\": \"00:00:00\", \"title\": \"Car chase begins on highway\"},\n"
175
+ " {\"timecode\": \"00:00:10\", \"title\": \"Police car joins pursuit\"}\n"
176
+ "]\n"
177
+ "```\n"
178
+ "Example 2: For a nature video:\n"
179
+ "```json\n"
180
+ "[\n"
181
+ " {\"timecode\": \"00:00:05\", \"title\": \"Bird flies across screen\"},\n"
182
+ " {\"timecode\": \"00:00:15\", \"title\": \"Deer appears in forest\"}\n"
183
+ "]\n"
184
+ "```\n"
185
+ "Now, provide the key frames for this video in the same JSON format."
186
  )
187
  if user_query:
188
  key_frames_prompt += f" Focus on: {user_query}"
 
193
  )
194
  key_frames = extract_key_frames(video_file, key_frames_response.text)
195
 
196
+ # Generate Markdown report
197
  markdown_report = (
198
  "## Video Analysis Report\n\n"
199
  f"**Summary:**\n{summary}\n"
 
203
  for i, (_, caption) in enumerate(key_frames, 1):
204
  markdown_report += f"- Frame {i}: {caption}\n"
205
  else:
206
+ markdown_report += "\n*No key frames extracted. Check the console for the raw response.*\n"
207
 
208
  return markdown_report, key_frames
209