ISM2023w / init.py
debayan's picture
Upload folder using huggingface_hub
4e99679
import os
from pathlib import Path
# Directory where request by models are stored
DIR_OUTPUT_REQUESTS = Path("requested_models")
EVAL_REQUESTS_PATH = Path("eval_requests")
from pathlib import Path
from huggingface_hub import HfApi, Repository
TOKEN_HUB = os.environ.get("TOKEN_HUB", None)
QUEUE_REPO = os.environ.get("QUEUE_REPO", None)
QUEUE_PATH = os.environ.get("QUEUE_PATH", None)
hf_api = HfApi(
endpoint="https://huggingface.co",
token=TOKEN_HUB,
)
def upload_file(requested_model_name, path_or_fileobj):
dest_repo_file = Path(EVAL_REQUESTS_PATH) / path_or_fileobj.name
dest_repo_file = str(dest_repo_file)
hf_api.upload_file(
path_or_fileobj=path_or_fileobj,
path_in_repo=str(dest_repo_file),
repo_id=QUEUE_REPO,
token=TOKEN_HUB,
repo_type="dataset",
commit_message=f"Add {requested_model_name} to eval queue")
def get_all_requested_models(directory):
directory = Path(directory)
all_requested_models = list(directory.glob("*.txt"))
return all_requested_models
def get_csv_with_results(directory):
directory = Path(directory)
all_csv_files = list(directory.glob("*.csv"))
latest = [f for f in all_csv_files if f.stem.endswith("latest")]
if len(latest) != 1:
return None
return latest[0]
def is_model_on_hub(model_name, revision="main") -> bool:
try:
model_name = model_name.replace(" ","")
author = model_name.split("/")[0]
model_id = model_name.split("/")[1]
if len(author) == 0 or len(model_id) == 0:
return False, "is not a valid model name. Please use the format `author/model_name`."
except Exception as e:
return False, "is not a valid model name. Please use the format `author/model_name`."
try:
models = list(hf_api.list_models(author=author, search=model_id))
matched = [model_name for m in models if m.modelId == model_name]
if len(matched) != 1:
return False, "was not found on the hub!"
else:
return True, None
except Exception as e:
print(f"Could not get the model from the hub.: {e}")
return False, "was not found on hub!"