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