File size: 1,214 Bytes
a77a138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
from huggingface_hub import HfApi
from .token_handler import TokenHandler

class HubStorage:
    def __init__(self, repo_id):
        self.repo_id = repo_id
        self.api = HfApi()

    def get_file_content(self, file_path):
        """
        Get content of a file from the repository
        """
        try:
            content = self.api.hf_hub_download(
                repo_id=self.repo_id,
                repo_type="space",
                filename=file_path,
                text=True
            )
            return content
        except Exception as e:
            print(f"Error reading file {file_path}: {str(e)}")
            return None

    def save_to_hub(self, file_content, path_in_repo, commit_message):
        """
        Save a file to the hub
        """
        try:
            self.api.upload_file(
                path_or_fileobj=file_content,
                path_in_repo=path_in_repo,
                repo_id=self.repo_id,
                repo_type="space",
                commit_message=commit_message
            )
            return True
        except Exception as e:
            print(f"Error saving file to hub: {str(e)}")
            return False