File size: 1,658 Bytes
b138e3b
 
 
 
5b7f342
b138e3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)