Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -281,7 +281,7 @@ def _get_video_info_and_timestamps(media_path: str, sample_count: int) -> Tuple[
|
|
| 281 |
|
| 282 |
timestamps: List[float] = []
|
| 283 |
if duration > 0 and sample_count > 0:
|
| 284 |
-
actual_sample_count = min(sample_count, max(1, int(duration)))
|
| 285 |
if actual_sample_count > 0:
|
| 286 |
step = duration / (actual_sample_count + 1)
|
| 287 |
timestamps = [step * (i + 1) for i in range(actual_sample_count)]
|
|
@@ -415,6 +415,9 @@ def upload_file_to_mistral(client, path: str, filename: str | None = None, purpo
|
|
| 415 |
progress(0.5 + 0.01 * attempt, desc=f"Uploading file to model service (attempt {attempt+1}/{max_retries})...")
|
| 416 |
|
| 417 |
with open(path, "rb") as fh:
|
|
|
|
|
|
|
|
|
|
| 418 |
res = client.files.upload(file={"file_name": fname, "content": fh}, purpose=purpose)
|
| 419 |
|
| 420 |
fid = getattr(res, "id", None) or (res.get("id") if isinstance(res, dict) else None)
|
|
@@ -518,7 +521,7 @@ def analyze_video_cohesive(client, video_path: str, prompt: str, progress=None)
|
|
| 518 |
)
|
| 519 |
return result, gallery_frame_paths
|
| 520 |
except Exception as e:
|
| 521 |
-
print(f"Warning: Video upload/full analysis failed ({e}). Extracting frames as fallback...")
|
| 522 |
if progress is not None:
|
| 523 |
progress(0.35, desc=f"Video upload failed ({type(e).__name__}). Extracting frames as fallback...")
|
| 524 |
|
|
@@ -527,7 +530,7 @@ def analyze_video_cohesive(client, video_path: str, prompt: str, progress=None)
|
|
| 527 |
)
|
| 528 |
|
| 529 |
if not frames_for_model_bytes:
|
| 530 |
-
return f"Error: could not upload video and no frames could be extracted for fallback. ({e})", []
|
| 531 |
|
| 532 |
image_entries = []
|
| 533 |
for i, fb in enumerate(frames_for_model_bytes, start=1):
|
|
@@ -850,8 +853,9 @@ def create_demo():
|
|
| 850 |
if not FFMPEG_BIN:
|
| 851 |
# If this is a video, and ffmpeg is missing, return early.
|
| 852 |
# Otherwise, proceed for image analysis.
|
| 853 |
-
|
| 854 |
-
|
|
|
|
| 855 |
return "error", "**Error:** FFmpeg is not found in your system PATH. Video analysis is unavailable. Please install FFmpeg.", current_main_preview_path, []
|
| 856 |
|
| 857 |
with open(raw_media_path, "rb") as f:
|
|
@@ -888,7 +892,7 @@ def create_demo():
|
|
| 888 |
progress(0.20, desc="Running image analysis")
|
| 889 |
result_text = analyze_image_structured(client, raw_bytes_for_analysis, prompt, progress=progress)
|
| 890 |
else:
|
| 891 |
-
return "error", "Error: Could not definitively determine media type for analysis after byte inspection and extension check. Please check the URL.", current_main_preview_path, []
|
| 892 |
|
| 893 |
status = "done" if not (isinstance(result_text, str) and result_text.lower().startswith("error")) else "error"
|
| 894 |
return status, result_text, current_main_preview_path, generated_screenshot_paths
|
|
|
|
| 281 |
|
| 282 |
timestamps: List[float] = []
|
| 283 |
if duration > 0 and sample_count > 0:
|
| 284 |
+
actual_sample_count = min(sample_count, max(1, int(duration))) # Ensure sample_count doesn't exceed duration
|
| 285 |
if actual_sample_count > 0:
|
| 286 |
step = duration / (actual_sample_count + 1)
|
| 287 |
timestamps = [step * (i + 1) for i in range(actual_sample_count)]
|
|
|
|
| 415 |
progress(0.5 + 0.01 * attempt, desc=f"Uploading file to model service (attempt {attempt+1}/{max_retries})...")
|
| 416 |
|
| 417 |
with open(path, "rb") as fh:
|
| 418 |
+
# The Mistral client's file upload typically expects a dict with 'file_name' and 'content'
|
| 419 |
+
# or a file-like object directly. Ensure compatibility here.
|
| 420 |
+
# Assuming the client expects 'file' parameter to be a dict as shown in placeholder
|
| 421 |
res = client.files.upload(file={"file_name": fname, "content": fh}, purpose=purpose)
|
| 422 |
|
| 423 |
fid = getattr(res, "id", None) or (res.get("id") if isinstance(res, dict) else None)
|
|
|
|
| 521 |
)
|
| 522 |
return result, gallery_frame_paths
|
| 523 |
except Exception as e:
|
| 524 |
+
print(f"Warning: Video upload/full analysis failed ({type(e).__name__}: {e}). Extracting frames as fallback...")
|
| 525 |
if progress is not None:
|
| 526 |
progress(0.35, desc=f"Video upload failed ({type(e).__name__}). Extracting frames as fallback...")
|
| 527 |
|
|
|
|
| 530 |
)
|
| 531 |
|
| 532 |
if not frames_for_model_bytes:
|
| 533 |
+
return f"Error: could not upload video and no frames could be extracted for fallback. ({type(e).__name__}: {e})", []
|
| 534 |
|
| 535 |
image_entries = []
|
| 536 |
for i, fb in enumerate(frames_for_model_bytes, start=1):
|
|
|
|
| 853 |
if not FFMPEG_BIN:
|
| 854 |
# If this is a video, and ffmpeg is missing, return early.
|
| 855 |
# Otherwise, proceed for image analysis.
|
| 856 |
+
# Determine media type by extension from raw_media_path
|
| 857 |
+
ext = ext_from_src(raw_media_path)
|
| 858 |
+
if ext in VIDEO_EXTENSIONS:
|
| 859 |
return "error", "**Error:** FFmpeg is not found in your system PATH. Video analysis is unavailable. Please install FFmpeg.", current_main_preview_path, []
|
| 860 |
|
| 861 |
with open(raw_media_path, "rb") as f:
|
|
|
|
| 892 |
progress(0.20, desc="Running image analysis")
|
| 893 |
result_text = analyze_image_structured(client, raw_bytes_for_analysis, prompt, progress=progress)
|
| 894 |
else:
|
| 895 |
+
return "error", "Error: Could not definitively determine media type for analysis after byte inspection and extension check. Please check the URL/file content.", current_main_preview_path, []
|
| 896 |
|
| 897 |
status = "done" if not (isinstance(result_text, str) and result_text.lower().startswith("error")) else "error"
|
| 898 |
return status, result_text, current_main_preview_path, generated_screenshot_paths
|