1
0
Fork 0
onnx-web/api/onnx_web/convert/client/http.py

53 lines
1.3 KiB
Python
Raw Normal View History

2023-12-10 00:46:47 +00:00
from logging import getLogger
from typing import Dict, Optional
from ..utils import (
ConversionContext,
build_cache_paths,
download_progress,
get_first_exists,
)
from .base import BaseClient
logger = getLogger(__name__)
class HttpClient(BaseClient):
name = "http"
protocol = "https://"
insecure_protocol = "http://"
headers: Dict[str, str]
def __init__(
self, _conversion: ConversionContext, headers: Optional[Dict[str, str]] = None
):
self.headers = headers or {}
2023-12-10 00:46:47 +00:00
def download(
self,
conversion: ConversionContext,
name: str,
source: str,
2023-12-10 00:46:47 +00:00
format: Optional[str] = None,
dest: Optional[str] = None,
2023-12-10 01:15:28 +00:00
**kwargs,
2023-12-10 00:46:47 +00:00
) -> str:
cache_paths = build_cache_paths(
2023-12-10 00:46:47 +00:00
conversion,
name,
client=HttpClient.name,
format=format,
dest=dest,
)
cached = get_first_exists(cache_paths)
if cached:
return cached
if source.startswith(HttpClient.protocol):
logger.info("downloading model from: %s", source)
elif source.startswith(HttpClient.insecure_protocol):
logger.warning("downloading model from insecure source: %s", source)
return download_progress(source, cache_paths[0])