|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
os.environ['HF_OUTPUT_REPO'] = 'votepurchase/pony'
|
|
|
|
|
|
|
|
def upload_to_repo(path, filename):
|
|
from huggingface_hub import HfApi, hf_hub_url
|
|
hf_token = os.environ.get("HF_TOKEN")
|
|
repo_id = os.environ.get("HF_OUTPUT_REPO")
|
|
api = HfApi()
|
|
try:
|
|
api.upload_file(path_or_fileobj=path, path_in_repo=filename, repo_id=repo_id, token=hf_token)
|
|
url = hf_hub_url(repo_id=repo_id, filename=filename)
|
|
except Exception as e:
|
|
print(f"Error: Failed to upload to {repo_id}. ")
|
|
return None
|
|
return url
|
|
|
|
def upload_image(path, filename):
|
|
url = upload_to_repo(path, filename)
|
|
print(url)
|
|
return url
|
|
|
|
|
|
def compose_image_url(repo_id, filename):
|
|
from huggingface_hub import hf_hub_url
|
|
url = hf_hub_url(repo_id=repo_id, filename=filename)
|
|
return url
|
|
|
|
|
|
def is_exists_image(filename):
|
|
from huggingface_hub import HfApi
|
|
hf_token = os.environ.get("HF_TOKEN")
|
|
repo_id = os.environ.get("HF_OUTPUT_REPO")
|
|
api = HfApi()
|
|
try:
|
|
is_exists = api.file_exists(repo_id=repo_id, filename=filename, token=hf_token)
|
|
except Exception as e:
|
|
return None
|
|
return is_exists
|
|
|
|
|
|
def download_image(filename):
|
|
from huggingface_hub import HfApi
|
|
hf_token = os.environ.get("HF_TOKEN")
|
|
repo_id = os.environ.get("HF_OUTPUT_REPO")
|
|
api = HfApi()
|
|
try:
|
|
if is_exists_image:
|
|
api.hf_hub_download(repo_id=repo_id, filename=filename, local_dir=".", token=hf_token)
|
|
except Exception as e:
|
|
return
|
|
|
|
|
|
|