Skip to content

Commit

Permalink
Merge branch 'main' into release/3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jintao-Huang committed Dec 20, 2024
2 parents 35fab5d + 1bcb9bb commit 6a34e96
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions swift/llm/template/vision_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,18 @@ def load_file(path: Union[str, _T]) -> Union[BytesIO, _T]:
request_kwargs['timeout'] = timeout
content = requests.get(path, **request_kwargs).content
res = BytesIO(content)
elif os.path.exists(path):
elif os.path.exists(path) or (not path.startswith('data:') and len(path) <= 200):
path = os.path.abspath(os.path.expanduser(path))
with open(path, 'rb') as f:
res = BytesIO(f.read())
else: # base64_str
import binascii
try:
data = path
if data.startswith('data:'):
match_ = re.match(r'data:(.+?);base64,(.+)', data)
assert match_ is not None
data = match_.group(2)
data = base64.b64decode(data)
res = BytesIO(data)
except (ValueError, binascii.Error) as error:
if len(path) < 200:
raise ValueError(f'invalid image: "{path}"')
else:
raise ValueError(f'invalid image: {error}')
data = path
if data.startswith('data:'):
match_ = re.match(r'data:(.+?);base64,(.+)', data)
assert match_ is not None
data = match_.group(2)
data = base64.b64decode(data)
res = BytesIO(data)
elif isinstance(path, bytes):
res = BytesIO(path)
return res
Expand Down

0 comments on commit 6a34e96

Please sign in to comment.