Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions python/sglang/srt/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
TypeVar,
Union,
)
from urllib.parse import urlparse
from urllib.parse import unquote, urlparse

import numpy as np
import orjson
Expand Down Expand Up @@ -874,6 +874,9 @@ def load_image(
image.load() # Force loading to avoid issues after closing the stream
finally:
response.close()
elif image_file.startswith("file://"):
image_file = unquote(urlparse(image_file).path)
image = Image.open(image_file)
elif image_file.lower().endswith(("png", "jpg", "jpeg", "webp", "gif")):
image = Image.open(image_file)
elif image_file.startswith("data:"):
Expand All @@ -894,6 +897,10 @@ def get_image_bytes(image_file: Union[str, bytes]):
timeout = int(os.getenv("REQUEST_TIMEOUT", "3"))
response = requests.get(image_file, timeout=timeout)
return response.content
elif image_file.startswith("file://"):
image_file = unquote(urlparse(image_file).path)
with open(image_file, "rb") as f:
return f.read()
elif image_file.lower().endswith(("png", "jpg", "jpeg", "webp", "gif")):
with open(image_file, "rb") as f:
return f.read()
Expand Down Expand Up @@ -943,8 +950,11 @@ def load_video(video_file: Union[str, bytes], use_gpu: bool = True):
tmp_file.write(video_bytes)
tmp_file.close()
vr = VideoReader(tmp_file.name, ctx=ctx)
elif video_file.startswith("file://"):
video_file = unquote(urlparse(video_file).path)
vr = VideoReader(video_file, ctx=ctx)
# `urlparse` supports file:// paths, and so does VideoReader
elif os.path.isfile(urlparse(video_file).path):
elif os.path.isfile(unquote(urlparse(video_file).path)):
vr = VideoReader(video_file, ctx=ctx)
Comment on lines +953 to 958
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for handling local file paths and file:// URIs can be simplified. The two elif blocks are redundant and can be combined into a single, more robust check. This also resolves the inconsistency where one branch modifies video_file and the other doesn't, and it makes it clear that a plain file path is always passed to VideoReader.

The comment on line 956 also appears to be incorrect with the new logic, as the path is extracted from file:// URIs before being passed to VideoReader.

Suggested change
elif video_file.startswith("file://"):
video_file = unquote(urlparse(video_file).path)
vr = VideoReader(video_file, ctx=ctx)
# `urlparse` supports file:// paths, and so does VideoReader
elif os.path.isfile(urlparse(video_file).path):
elif os.path.isfile(unquote(urlparse(video_file).path)):
vr = VideoReader(video_file, ctx=ctx)
# Handle local file paths, including file:// URIs
elif os.path.isfile(parsed_path := unquote(urlparse(video_file).path)):
vr = VideoReader(parsed_path, ctx=ctx)

else:
video_bytes = pybase64.b64decode(video_file, validate=True)
Expand Down
Loading