feat: Add utils for logging and writing when interacting with HuggingFaceHub

This commit is contained in:
teleprint-me 2024-05-17 20:26:21 -04:00
parent dbdf6c2b1d
commit ba13d64bb3
No known key found for this signature in database
GPG key ID: B0D11345E65C4D48

View file

@ -1,17 +1,24 @@
import logging
import os
import pathlib
import requests
class HuggingFaceHub:
def __init__(self, auth_token: None | str):
def __init__(self, auth_token: None | str, logger: None | logging.Logger):
# Set headers if authentication is available
if auth_token is None:
self._headers = {}
else:
self._headers = {"Authorization": f"Bearer {auth_token}"}
# Set the logger
if logger is None:
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("huggingface-hub")
self.logger = logger
# Persist across requests
self._session = requests.Session()
@ -34,6 +41,12 @@ class HuggingFaceHub:
def base_url(self) -> str:
return self._base_url
@staticmethod
def write_file(self, content: bytes, save_path: pathlib.Path) -> None:
with open(save_path, 'wb') as f:
f.write(content)
self.logger.info(f"File {save_path} downloaded successfully")
def resolve_path(self, repo: str, file: str) -> str:
return f"{self._base_url}/{repo}/resolve/main/{file}"