Skip to content

Commit 1bcb9bb

Browse files
authored
Better error messages (#2721)
1 parent 9ace086 commit 1bcb9bb

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

swift/llm/template/vision_utils.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,18 @@ def load_file(path: Union[str, _T]) -> Union[BytesIO, _T]:
113113
request_kwargs['timeout'] = timeout
114114
content = requests.get(path, **request_kwargs).content
115115
res = BytesIO(content)
116-
elif os.path.exists(path):
116+
elif os.path.exists(path) or (not path.startswith('data:') and len(path) <= 200):
117+
path = os.path.abspath(os.path.expanduser(path))
117118
with open(path, 'rb') as f:
118119
res = BytesIO(f.read())
119120
else: # base64_str
120-
import binascii
121-
try:
122-
data = path
123-
if data.startswith('data:'):
124-
match_ = re.match(r'data:(.+?);base64,(.+)', data)
125-
assert match_ is not None
126-
data = match_.group(2)
127-
data = base64.b64decode(data)
128-
res = BytesIO(data)
129-
except (ValueError, binascii.Error) as error:
130-
if len(path) < 200:
131-
raise ValueError(f'invalid image: "{path}"')
132-
else:
133-
raise ValueError(f'invalid image: {error}')
121+
data = path
122+
if data.startswith('data:'):
123+
match_ = re.match(r'data:(.+?);base64,(.+)', data)
124+
assert match_ is not None
125+
data = match_.group(2)
126+
data = base64.b64decode(data)
127+
res = BytesIO(data)
134128
elif isinstance(path, bytes):
135129
res = BytesIO(path)
136130
return res

0 commit comments

Comments
 (0)