Spaces:
Sleeping
Sleeping
File size: 2,120 Bytes
9ba9756 |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
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()
|