Messenger_back_database / hf_video_storage.py
Nrighton233j
Swap large-file storage from Google Drive to private HF dataset repo
dab4502
Raw
History Blame Contribute Delete
1.35 kB
"""
hf_video_storage.py — lazy-bubble large file storage backed by a private
HF dataset repo. Upload returns immediately; the recipient's download
pulls the bytes then permanently deletes the file from the repo.
"""
import os
import uuid
import requests
from huggingface_hub import HfApi
VIDEO_REPO = os.environ.get("VIDEO_DATASET_REPO", "Brighton233j/Video-Temp-Storage")
_HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("Hf_token")
_api = HfApi(token=_HF_TOKEN)
def upload_large_file(file_bytes, filename, mime_type=None):
ext = os.path.splitext(filename)[1] or ""
path_in_repo = f"{uuid.uuid4().hex}{ext}"
_api.upload_file(
path_or_fileobj=file_bytes,
path_in_repo=path_in_repo,
repo_id=VIDEO_REPO,
repo_type="dataset",
commit_message=f"Upload {filename}",
)
return {"id": path_in_repo, "name": filename}
def download_and_delete(file_id):
url = f"https://huggingface.co/datasets/{VIDEO_REPO}/resolve/main/{file_id}"
resp = requests.get(url, headers={"Authorization": f"Bearer {_HF_TOKEN}"})
resp.raise_for_status()
file_bytes = resp.content
_api.delete_file(
path_in_repo=file_id,
repo_id=VIDEO_REPO,
repo_type="dataset",
commit_message=f"Delete downloaded file {file_id}",
)
return file_bytes, file_id