Skip to content

Commit

Permalink
Fix e2b websocket issue (#177)
Browse files Browse the repository at this point in the history
* Fix e2b Websocket issue

Remove health/liveness checking code

* Bump pydantic to 2.7.4 to include a bug fix; Rewrite save_video() using cv2 (get rid of moviepy); Add retry for common e2b exceptions

* Update template name
  • Loading branch information
humpydonkey authored Jul 30, 2024
1 parent 23d4af5 commit 9f53e01
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 177 deletions.
147 changes: 17 additions & 130 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ rich = "^13.7.1"
langsmith = "^0.1.58"
ipykernel = "^6.29.4"
e2b = "^0.17.1"
e2b-code-interpreter = "0.0.11a2"
e2b-code-interpreter = "0.0.11a17"
tenacity = "^8.3.0"
pillow-heif = "^0.16.0"
pytube = "15.0.0"
anthropic = "^0.31.0"
pydantic = "2.7.4"

[tool.poetry.group.dev.dependencies]
autoflake = "1.*"
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/tools/test_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by CodiumAI
from pathlib import Path

import numpy as np

from vision_agent.tools.tools import save_video


class TestSaveVideo:
def test_saves_frames_without_output_path(self):
frames = [
np.random.randint(0, 256, (480, 640, 3), dtype=np.uint8) for _ in range(10)
]
output_path = save_video(frames)
assert Path(output_path).exists()

def test_saves_frames_with_output_path(self, tmp_path):
frames = [
np.random.randint(0, 256, (480, 640, 3), dtype=np.uint8) for _ in range(10)
]
video_output_path = str(tmp_path / "output.mp4")
output_path = save_video(frames, video_output_path)

assert output_path == video_output_path
assert Path(output_path).exists()

# Handles an empty list of frames gracefully
def test_handles_empty_frames_list(self):
frames = []
output_path = save_video(frames)
assert Path(output_path).exists()
25 changes: 15 additions & 10 deletions vision_agent/tools/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import cv2
import numpy as np
import requests
from moviepy.editor import ImageSequenceClip
from PIL import Image, ImageDraw, ImageFont
from pillow_heif import register_heif_opener # type: ignore
from pytube import YouTube # type: ignore
Expand Down Expand Up @@ -1044,15 +1043,21 @@ def save_video(
if fps <= 0:
_LOGGER.warning(f"Invalid fps value: {fps}. Setting fps to 4 (default value).")
fps = 4
with ImageSequenceClip(frames, fps=fps) as video:
if output_video_path:
f = open(output_video_path, "wb")
else:
f = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) # type: ignore
video.write_videofile(f.name, codec="libx264")
f.close()
_save_video_to_result(f.name)
return f.name

if not output_video_path:
output_video_path = tempfile.NamedTemporaryFile(
suffix=".mp4", delete=False
).name

height, width, layers = frames[0].shape if frames else (0, 0, 0)
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # type: ignore
video = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
for frame in frames:
video.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
video.release()

_save_video_to_result(output_video_path)
return output_video_path


def _save_video_to_result(video_uri: str) -> None:
Expand Down
Loading

0 comments on commit 9f53e01

Please sign in to comment.