shrewd-agent / tools /load_file.py
Pycrolis
feat(tools): add utility to load file from URL or local path
604617b
raw
history blame contribute delete
527 Bytes
import io
from urllib.parse import urlparse
import requests
def load_file(path_or_url: str) -> io.BytesIO:
if _is_url(path_or_url):
response = requests.get(path_or_url)
response.raise_for_status()
return io.BytesIO(response.content)
else:
with open(path_or_url, 'rb') as f:
return io.BytesIO(f.read())
def _is_url(path_or_url):
try:
result = urlparse(path_or_url)
return result.scheme in ('http', 'https')
except ValueError:
return False