Spaces:
Running
Running
File size: 1,821 Bytes
f268b5a 4438381 0ce7dfd bb3ddcf f268b5a e65749f 4438381 85d9e2b 4438381 85d9e2b 4438381 e65749f 4438381 bb3ddcf 4438381 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from pathlib import Path
from huggingface_hub import HfApi
from .token_handler import TokenHandler
import time
import tempfile
import os
class HubStorage:
def __init__(self, repo_id):
self.repo_id = repo_id
self.api = HfApi()
def get_file_content(self, path_in_repo):
"""Get content of a file from the hub"""
try:
# Download the file content as bytes
path = self.api.hf_hub_download(
repo_id=self.repo_id,
filename=path_in_repo,
repo_type="space"
)
# Read file content
with open(path, 'r') as f:
return f.read()
except Exception as e:
print(f"Error reading file {path_in_repo}: {str(e)}")
return None
def save_to_hub(self, file_content, path_in_repo, commit_message="Update file"):
"""Save content to hub"""
try:
# If file_content is a string (like JSON), write it to a temporary file first
if isinstance(file_content, str):
with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_file:
temp_file.write(file_content)
temp_path = temp_file.name
else:
# If it's already a file path, use it directly
temp_path = file_content
# Upload to hub
self.api.upload_file(
path_or_fileobj=temp_path,
path_in_repo=path_in_repo,
repo_id=self.repo_id,
repo_type="space",
commit_message=commit_message
)
os.unlink(temp_path)
return True
except Exception as e:
print(f"Error saving to hub: {str(e)}")
return False |