File size: 1,405 Bytes
f268b5a
 
 
 
 
 
 
e65749f
e050fd8
 
85d9e2b
e050fd8
85d9e2b
e050fd8
e65749f
 
e050fd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()
        
        # Validate repository access
        try:
            self.api.repo_info(repo_id=repo_id, repo_type="space")
        except Exception as e:
            raise RuntimeError(f"Failed to access repository {repo_id}: {e}")

    def save_to_hub(self, file_content, path_in_repo, commit_message):
        """Save file to HuggingFace Hub with retries and better error handling"""
        max_retries = 3
        retry_delay = 1  # seconds
        
        for attempt in range(max_retries):
            try:
                self.api.upload_file(
                    repo_id=self.repo_id,
                    repo_type="space",
                    path_or_fileobj=file_content,
                    path_in_repo=path_in_repo,
                    commit_message=commit_message
                )
                return True
            except Exception as e:
                if attempt == max_retries - 1:
                    raise RuntimeError(f"Failed to save file after {max_retries} attempts: {e}")
                print(f"Attempt {attempt + 1} failed, retrying in {retry_delay} seconds...")
                time.sleep(retry_delay)
                retry_delay *= 2  # Exponential backoff