HF_RepoSense / hf_utils.py
naman1102's picture
button
5b7f342
raw
history blame
1.66 kB
from huggingface_hub import snapshot_download
import os
import shutil
def download_space_repo(space_id: str, local_dir: str = "repo_files"):
"""
Downloads all files from a Hugging Face Space repository.
Args:
space_id (str): The ID of the Hugging Face Space (e.g., "naman1102/Final_Assignment_Template").
local_dir (str): Local directory to store the downloaded files.
"""
print(f"Downloading Space '{space_id}'...")
# Download the snapshot of the space repo
repo_path = snapshot_download(repo_id=space_id, repo_type="space")
# Remove existing directory if it exists
if os.path.exists(local_dir):
shutil.rmtree(local_dir)
# Copy contents to target directory
shutil.copytree(repo_path, local_dir)
print(f"All files from Space '{space_id}' downloaded to: {local_dir}")
# Example usage
# download_space_repo("finegrain/finegrain-image-enhancer")
from huggingface_hub import list_spaces
def search_top_spaces(query: str, limit: int = 5):
"""
Search and return top Hugging Face Space repo IDs based on a keyword.
Args:
query (str): The keyword to search for (e.g., "image", "chatbot").
limit (int): Maximum number of results to return.
Returns:
List of repo IDs.
"""
results = list(list_spaces(search=query, sort="likes", direction=-1)) # Convert generator to list
top_spaces = [space.id for space in results[:limit]]
return top_spaces
# Example usage
# top_image_spaces = search_top_spaces("tic tac toe", limit=10)
# print("Top games-related Spaces:")
# for space_id in top_image_spaces:
# print("-", space_id)