import os from github import Github import base64 import shutil import zipfile from io import BytesIO # Global Variables # HF ------------ hf_folder_path = '/home/user/app/embeddings' zip_name = 'embeddings' # Github ------- github_token = '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' # Replace with your GitHub token repo_name = 'AdityaMetkar/Patseer-Database' # Replace with your repository, e.g., 'octocat/Hello-World' folder_path = 'Manual Database/embeddings.zip' # Replace with the path to the folder in the repository # Authenticate to GitHub g = Github(github_token) repo = g.get_repo(repo_name) # Functions ------------------------------- def zip_folder(): shutil.make_archive(zip_name, 'zip', hf_folder_path) return zip_name + '.zip' def update_db(): try: # Check if the file already exists in the repository existing_file = repo.get_contents(folder_path) compressed_zip = zip_folder() with open(compressed_zip, 'rb') as file: file_content = file.read() # Update the existing file repo.update_file(existing_file.path, "New DB Update", file_content, existing_file.sha) print(f"Updated {folder_path} in GitHub repository.") except Exception as e: print(f"Error: {e}") def download_db(): if not os.path.exists(hf_folder_path): os.makedirs(hf_folder_path) file_content = repo.get_contents(folder_path) try: # Download the zip file content from GitHub file_content = repo.get_contents(folder_path) zip_data = base64.b64decode(file_content.content) # Extract the downloaded zip file directly to hf_folder_path using shutil with zipfile.ZipFile(BytesIO(zip_data)) as zip_ref: for file in zip_ref.namelist(): zip_ref.extract(file, hf_folder_path) print(f"Successfully unzipped files to {hf_folder_path}") except Exception as e: print(f"Error: {e}") # Download the folder # download_folder() # update_db()