File size: 3,879 Bytes
7070944
 
eb10328
7070944
dc72cab
7070944
dc72cab
 
 
 
 
7070944
dc72cab
 
 
 
 
 
 
7070944
f250035
dc72cab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f250035
dc72cab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7070944
dc72cab
 
 
 
 
7070944
dc72cab
 
7070944
 
dc72cab
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import gradio as gr
import pandas as pd
from huggingface_hub import HfApi

api = HfApi()

# Mapping user-friendly labels to API values
LIBRARY_OPTIONS = ["All", "Gradio", "Streamlit", "Flask", "FastAPI", "Transformers", "Diffusers"]
LICENSE_OPTIONS = ["All", "Apache-2.0", "MIT", "BSD-3-Clause", "GPL-3.0"]
HARDWARE_OPTIONS = ["All", "CPU", "GPU"]
VISIBILITY_OPTIONS = ["All", "Public", "Private"]

SORT_OPTIONS = {
    "Last Modified": SpaceSort.LAST_MODIFIED,
    "First Indexed": SpaceSort.FIRST_INDEXED,
    "Likes": SpaceSort.LIKES,
    "Runs": SpaceSort.RUNS
}
DIRECTION_OPTIONS = ["Descending", "Ascending"]


def search_spaces(query, library, license, tags, visibility, hardware, sort_by, direction, limit):
    # Prepare filters
    lib_filter = None if library == "All" else getattr(SpaceLibraries, library.upper(), None)
    license_filter = None if license == "All" else license
    hardware_filter = None
    if hardware == "CPU":
        hardware_filter = SpaceHardware.CPU
    elif hardware == "GPU":
        hardware_filter = SpaceHardware.GPU
    vis_filter = None
    if visibility == "Public":
        vis_filter = False  # private=False
    elif visibility == "Private":
        vis_filter = True
    # Tags: comma-separated
    tag_list = [t.strip() for t in tags.split(",")] if tags else None
    sort_value = SORT_OPTIONS.get(sort_by, SpaceSort.LAST_MODIFIED)
    reverse = False if direction == "Descending" else True

    spaces = api.list_spaces(
        search=query or None,
        library_filter=lib_filter,
        license=license_filter,
        hardware=hardware_filter,
        private=vis_filter,
        sort=sort_value,
        direction="asc" if reverse else "desc",
        limit=limit,
        task=tag_list  # filters by tags/tasks
    )

    # Build list of dicts
    data = []
    for s in spaces:
        data.append({
            "Name": s.id,
            "Author": s.author,
            "Library": s.spaceType,
            "SDK": s.sdk,
            "Tags": ", ".join(s.tags) if hasattr(s, 'tags') else "",
            "Hardware": s.hardware if hasattr(s, 'hardware') else "",
            "Visibility": "Private" if s.private else "Public",
            "Likes": s.likes,
            "Runs": s.runs,
            "Last Modified": s.lastModified,
            "URL": f"https://huggingface.co/spaces/{s.id}"
        })
    return data

with gr.Blocks() as demo:
    gr.Markdown("# πŸ” HF Spaces Explorer with Advanced Filters")
    with gr.Row():
        query = gr.Textbox(label="Search Query", placeholder="Enter keywords...")
        library = gr.Dropdown(LIBRARY_OPTIONS, label="Library", value="All")
        license = gr.Dropdown(LICENSE_OPTIONS, label="License", value="All")
    with gr.Row():
        tags = gr.Textbox(label="Tags (comma-separated)", placeholder="e.g. text-generation, image-classification")
        visibility = gr.Dropdown(VISIBILITY_OPTIONS, label="Visibility", value="All")
        hardware = gr.Dropdown(HARDWARE_OPTIONS, label="Hardware", value="All")
    with gr.Row():
        sort_by = gr.Dropdown(list(SORT_OPTIONS.keys()), label="Sort By", value="Last Modified")
        direction = gr.Radio(DIRECTION_OPTIONS, label="Direction", value="Descending")
        limit = gr.Slider(1, 100, label="Max Results", value=20, step=1)
    search_btn = gr.Button("πŸ”Ž Search")
    results = gr.Dataframe(headers=["Name", "Author", "Library", "SDK", "Tags", "Hardware", "Visibility", "Likes", "Runs", "Last Modified", "URL"], label="Results")

    search_btn.click(
        fn=search_spaces,
        inputs=[query, library, license, tags, visibility, hardware, sort_by, direction, limit],
        outputs=results
    )

    gr.Markdown("---")
    gr.Markdown("Enhanced with tag, hardware & visibility filters. Built on `huggingface_hub` and Gradio Blocks.")

if __name__ == "__main__":
    demo.launch(share=True)