[Feature] Support file:// URL format for multimodal inputs (#14490)
Co-authored-by: Yuhao Yang <47235274+yhyang201@users.noreply.github.com>
This commit is contained in:
co-authored by
Yuhao Yang
parent
71babdef51
commit
9bb1260558
@@ -70,7 +70,7 @@ from typing import (
|
|||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
from unittest import SkipTest
|
from unittest import SkipTest
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import unquote, urlparse
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import orjson
|
import orjson
|
||||||
@@ -860,6 +860,9 @@ def load_audio(
|
|||||||
audio_file = BytesIO(response.content)
|
audio_file = BytesIO(response.content)
|
||||||
response.close()
|
response.close()
|
||||||
audio, original_sr = sf.read(audio_file)
|
audio, original_sr = sf.read(audio_file)
|
||||||
|
elif audio_file.startswith("file://"):
|
||||||
|
audio_file = unquote(urlparse(audio_file).path)
|
||||||
|
audio, original_sr = sf.read(audio_file)
|
||||||
elif isinstance(audio_file, str):
|
elif isinstance(audio_file, str):
|
||||||
audio, original_sr = sf.read(audio_file)
|
audio, original_sr = sf.read(audio_file)
|
||||||
else:
|
else:
|
||||||
@@ -905,6 +908,9 @@ def load_image(
|
|||||||
image.load() # Force loading to avoid issues after closing the stream
|
image.load() # Force loading to avoid issues after closing the stream
|
||||||
finally:
|
finally:
|
||||||
response.close()
|
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")):
|
elif image_file.lower().endswith(("png", "jpg", "jpeg", "webp", "gif")):
|
||||||
image = Image.open(image_file)
|
image = Image.open(image_file)
|
||||||
elif image_file.startswith("data:"):
|
elif image_file.startswith("data:"):
|
||||||
@@ -925,6 +931,10 @@ def get_image_bytes(image_file: Union[str, bytes]):
|
|||||||
timeout = int(os.getenv("REQUEST_TIMEOUT", "3"))
|
timeout = int(os.getenv("REQUEST_TIMEOUT", "3"))
|
||||||
response = requests.get(image_file, timeout=timeout)
|
response = requests.get(image_file, timeout=timeout)
|
||||||
return response.content
|
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")):
|
elif image_file.lower().endswith(("png", "jpg", "jpeg", "webp", "gif")):
|
||||||
with open(image_file, "rb") as f:
|
with open(image_file, "rb") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
@@ -974,8 +984,11 @@ def load_video(video_file: Union[str, bytes], use_gpu: bool = True):
|
|||||||
tmp_file.write(video_bytes)
|
tmp_file.write(video_bytes)
|
||||||
tmp_file.close()
|
tmp_file.close()
|
||||||
vr = VideoReader(tmp_file.name, ctx=ctx)
|
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
|
# `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)
|
vr = VideoReader(video_file, ctx=ctx)
|
||||||
else:
|
else:
|
||||||
video_bytes = pybase64.b64decode(video_file, validate=True)
|
video_bytes = pybase64.b64decode(video_file, validate=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user