File size: 2,002 Bytes
a02f5b2
 
 
 
 
a704218
a02f5b2
 
a704218
 
 
 
a02f5b2
 
 
a704218
 
 
 
a02f5b2
67ba1ec
a704218
 
a02f5b2
67ba1ec
a704218
 
 
a02f5b2
 
a704218
a02f5b2
 
 
 
 
 
 
 
 
a704218
 
a02f5b2
a704218
a02f5b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a704218
 
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
55
56
57
58
59
60
61
62
63
from huggingface_hub import HfApi, upload_file
import os
import uuid
from datetime import datetime
import tempfile
from dotenv import load_dotenv

class HuggingFaceStorageService:
    def __init__(self):
        load_dotenv()
        self.repo_id = os.getenv("HF_REPO_ID")
        self.token = os.getenv("HF_TOKEN")
        self.api = HfApi()
        
    
    def upload_file_to_hf(self, file_content: bytes, folder: str, filename: str = None) -> str:
        if folder not in ["resumes", "cover-letters"]:
            raise ValueError("Folder must be 'resumes' or 'cover-letters'")

        if filename is None:
            filename = f"{uuid.uuid4().hex}"

        # Create a unique path with date
        timestamp = datetime.now().strftime("%Y/%m/%d")
        file_path = f"{folder}/{timestamp}/{filename}.pdf"

        # Save bytes to temp file
        with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(filename)[1]) as temp_file:
            temp_file.write(file_content)
            temp_file_path = temp_file.name

        try:
            # Upload to HuggingFace Hub
            upload_file(
                path_or_fileobj=temp_file_path,
                path_in_repo=file_path,
                repo_id=self.repo_id,
                token=self.token,
                repo_type="dataset"
            )

            # Return the direct URL
            return f"https://huggingface.co/datasets/{self.repo_id}/resolve/main/{file_path}"

        finally:
            os.unlink(temp_file_path)
    
    # Removed cover letter upload method since we only store resumes
    
    def delete_file(self, file_path: str) -> bool:
        try:
            self.api.delete_file(
                path_in_repo=file_path,
                repo_id=self.repo_id,
                token=self.token,
                repo_type="dataset"
            )
            return True
        except Exception as e:
            print(f"Failed to delete file: {str(e)}")
            return False