File size: 13,298 Bytes
3403bc2
 
fb53d5e
 
 
 
 
 
63ac6d8
fb53d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f144f7c
 
092a34e
fb53d5e
 
 
 
3ed607f
fb53d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95a0f32
fb53d5e
95a0f32
2f406ee
95a0f32
2f406ee
3ae1405
fb53d5e
2f406ee
fb53d5e
 
3403bc2
5aaa481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67d9fd3
c0b662b
3403bc2
 
 
 
 
 
 
 
c0b662b
 
1fa9151
67d9fd3
 
3403bc2
c0b662b
 
3403bc2
 
c0b662b
3403bc2
0cdd985
 
481e2a4
67d9fd3
c0b662b
481e2a4
3403bc2
67d9fd3
 
c0b662b
 
67d9fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0b662b
 
67d9fd3
 
 
 
 
3403bc2
 
c0b662b
 
3403bc2
0cdd985
01f7e08
0cdd985
 
 
 
 
67d9fd3
 
 
 
0cdd985
 
 
 
 
01f7e08
 
c0b662b
481e2a4
 
0cdd985
 
67d9fd3
 
481e2a4
 
 
0cdd985
 
01f7e08
 
0cdd985
 
3403bc2
 
67d9fd3
 
 
 
5aaa481
 
67d9fd3
 
5aaa481
 
 
 
c0b662b
67d9fd3
3403bc2
 
c0b662b
67d9fd3
084d3d6
3403bc2
 
dcf581a
 
 
 
3403bc2
 
 
c0b662b
 
 
 
 
 
 
 
 
084d3d6
 
75255f7
67d9fd3
 
3403bc2
c0b662b
3403bc2
67d9fd3
 
 
3403bc2
084d3d6
3403bc2
67d9fd3
 
 
75255f7
 
67d9fd3
 
 
 
 
 
 
 
 
 
9c64835
67d9fd3
 
 
 
 
3403bc2
 
 
 
 
 
f1d940e
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import gradio as gr
import pandas as pd
from huggingface_hub import HfApi
from collections import defaultdict

# ------------------------------------------------------
# Get spaces with more details
api = HfApi()
spaces = api.list_spaces(limit=60000)  # Limiting to 60000 for now

# Create a DataFrame
data = []
for space in spaces:
    data.append({
        'id': space.id,
        'title': space.id.split('/')[-1],
        'author': space.author if space.author else space.id.split('/')[0],
        'likes': space.likes,
        'tags': space.tags if hasattr(space, 'tags') else [],
    })

df = pd.DataFrame(data)
print("Total spaces collected:", len(df))
print("\nSample of the data:")
print(df.head())

# ------------------------------------------------------

# Define categories and their keywords
categories = {
    'Text-to-Speech': ['tts', 'speech', 'voice', 'audio', 'kokoro'],
    'Transcription': ['transcribe', 'transcription'],
    'Agents': ['agent', 'agents', 'smol', 'multi-step', 'autobot', 'autoGPT' 'agentic'],
    'Image Gen/Editing': ['stable-diffusion', 'diffusion', 'flux', 'dalle', 'CLIP', 
                         'comic', 'gan', 'sdxl', 'pic', 'img', 'stable', 'midjourney', 
                         'diffusion', 'image', 'ControlNet', 'Control Net', 'dreambooth', 'blip', 'LoRA', 'img2img', 'style', 'art'],
    'Video': ['video', 'animation', 'motion', 'sora'],
    'Face/Portrait': ['face', 'portrait', 'gaze', 'facial'],
    'Chat/LLM': ['chat', 'llm', 'gpt', 'llama', 'text', 'language'],
    '3D': ['3d', 'mesh', 'point-cloud', 'depth'],
    'Audio': ['audio', 'tts', 'music', 'whisper', 'sound', 'voice'],
    'Vision': ['vision', 'detection', 'recognition', 'classifier'],
    'CLIP': ['image-to-text', 'describe-image'],
    'Games': ['game', 'games', 'play', 'playground'],
    'Finance': ['finance', 'stock', 'money', 'currency', 'bank', 'market'],
    'SAM': ['sam', 'segmentation', 'mask'],
    'Science': ['science', 'physics', 'chemistry', 'biology', 'math', 'astronomy', 'geology', 'meteorology', 'engineering', 'medicine', 'health', 'nutrition', 'environment', 'ecology', 'geography', 'geology', 'geophysics'],
    'Education': ['education', 'school', 'university', 'college', 'teaching', 'learning', 'study', 'research'],
    'Graph': ['graph', 'network', 'node', 'edge', 'path', 'tree', 'cycle', 'flow', 'matching', 'coloring', 'swarm'],
    'Research': ['research', 'study', 'experiment', 'paper', 'discovery', 'innovation', 'exploration', 'analysis'],
    'Document Analyis': ['pdf', 'RAG', 'idefecs'],
    'WebGPU': ['localModel', 'webGPU'],
    'Point Tracking': ['CoTracker', 'tapir', 'tapnet', 'point', 'track'],
    'Games': ['game', 'Unity', 'UE5', 'Unreal'],
    'Leaderboard': ['arena', 'leaderboard', 'timeline'],
    'Other': []  # Default category
}

def categorize_space(title, tags):
    title_lower = title.lower()
    # Convert tags to lowercase if tags exist
    tags_lower = [t.lower() for t in tags] if tags else []
    
    for category, keywords in categories.items():
        # Check both title and tags for keywords
        if any(keyword in title_lower for keyword in keywords) or \
           any(keyword in tag for keyword in keywords for tag in tags_lower):
            return category
    return 'Other'

# Add category to DataFrame
df['category'] = df.apply(lambda x: categorize_space(x['title'], x['tags']), axis=1)

# Show category distribution
category_counts = df['category'].value_counts()
print("\nCategory Distribution:")
print(category_counts)

# Show sample spaces from each category
print("\nSample spaces from each category:")
for category in categories.keys():
    print(f"\n{category}:")
    sample = df[df['category'] == category].head(3)
    print(sample[['title', 'likes']].to_string())
    
# ------------------------------------------------------
# Add total likes per category
category_likes = df.groupby('category')['likes'].sum().sort_values(ascending=False)
print("Total likes per category:")
print(category_likes)

print("\nTop 10 spaces in each category (sorted by likes):")
for category in categories.keys():
    print(f"\n=== {category} ===")
    top_10 = df[df['category'] == category].nlargest(10, 'likes')[['title', 'likes']]
    # Format output with padding for better readability
    print(top_10.to_string(index=False))

# ------------------------------------------------------

# Add space URLs
df['url'] = 'https://huggingface.co/spaces/' + df['id']

# Show the top 10 spaces from each category with their links
# print("Top 10 spaces in each category with links:")
# for category in categories.keys():
    # print(f"\n=== {category} ===")
    # top_10 = df[df['category'] == category].nlargest(10, 'likes')[['title', 'likes', 'url']]
    # Format output with padding for better readability
    # print(top_5.to_string(index=False))

# ------------------------------------------------------

def create_pagination_html(current_page, total_pages):
    """Create HTML for pagination links"""
    html = "<div style='text-align: center; margin: 20px 0;'>"
    
    # Calculate which page numbers to show
    if total_pages <= 7:
        pages_to_show = range(1, total_pages + 1)
    else:
        if current_page <= 4:
            pages_to_show = list(range(1, 6)) + ['...', total_pages]
        elif current_page >= total_pages - 3:
            pages_to_show = [1, '...'] + list(range(total_pages - 4, total_pages + 1))
        else:
            pages_to_show = [1, '...'] + list(range(current_page - 1, current_page + 2)) + ['...', total_pages]
    
    # Generate the HTML for page links
    for page in pages_to_show:
        if page == '...':
            html += f"<span style='margin: 0 5px;'>...</span>"
        else:
            if page == current_page:
                html += f"""
                <span style='margin: 0 5px; padding: 5px 10px; 
                           background-color: var(--color-accent-soft); 
                           border-radius: 5px; font-weight: bold;'>{page}</span>
                """
            else:
                html += f"""
                <button onclick='handlePageClick({page-1})' 
                        style='margin: 0 5px; padding: 5px 10px; 
                               border: 1px solid var(--color-border-primary);
                               border-radius: 5px; cursor: pointer;
                               background: none;'>{page}</button>
                """
    
    html += "</div>"
    return html

def search_spaces(search_text="", category="All Categories", page=1, limit=100):
    # Filter spaces
    if category == "All Categories":
        spaces_df = df
    else:
        spaces_df = df[df['category'] == category]
    
    if search_text:
        spaces_df = spaces_df[spaces_df['title'].str.lower().str.contains(search_text.lower())]
    
    # Sort by likes and get total count
    spaces_df = spaces_df.sort_values('likes', ascending=False)
    total_spaces = len(spaces_df)
    total_pages = max(1, (total_spaces + limit - 1) // limit)
    offset = (page - 1) * limit
    
    # Get the current page of spaces
    spaces = spaces_df.iloc[offset:offset + limit][['title', 'likes', 'url', 'category']]
    total_likes = spaces_df['likes'].sum()
    
    # Generate HTML content
    html_content = f"""
    <div style='margin-bottom: 20px; padding: 10px; background-color: var(--color-background-primary); 
                border: 1px solid var(--color-border-primary); border-radius: 5px;'>
        <h3 style='color: var(--color-text-primary);'>Statistics:</h3>
        <p style='color: var(--color-text-primary);'>Page {page} of {total_pages}</p>
        <p style='color: var(--color-text-primary);'>Showing {offset + 1}-{min(offset + limit, total_spaces)} of {total_spaces} Spaces</p>
        <p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
    </div>
    
    <div style='display: flex; justify-content: center; gap: 10px; margin: 20px 0;'>
    """
    
    # Add pagination buttons
    if page > 1:
        html_content += f"<button onclick='page_change({page-1})' class='page-btn'>Previous</button>"
    
    # Add numeric page buttons
    for p in range(max(1, page-2), min(total_pages+1, page+3)):
        if p == page:
            html_content += f"<button onclick='page_change({p})' class='page-btn active'>{p}</button>"
        else:
            html_content += f"<button onclick='page_change({p})' class='page-btn'>{p}</button>"
    
    if page < total_pages:
        html_content += f"<button onclick='page_change({page+1})' class='page-btn'>Next</button>"
    
    html_content += "</div>"
    
    # Add grid container with responsive design
    html_content += """
    <div style='max-height: 800px; overflow-y: auto;'>
        <div style='display: grid; 
                    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); 
                    gap: 15px; 
                    padding: 10px;
                    max-width: 100%;'>
    """
    
    # Add space cards
    for _, row in spaces.iterrows():
        html_content += f"""
        <div style='padding: 15px; 
                    border: 2px solid var(--color-border-primary); 
                    border-radius: 5px; 
                    background-color: var(--color-background-primary);
                    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                    display: flex;
                    flex-direction: column;
                    height: 100%;
                    min-width: 0;
                    word-wrap: break-word;'>
            <h3 style='margin-top: 0; margin-bottom: 10px; overflow: hidden; text-overflow: ellipsis;'>
                <a href='{row['url']}' target='_blank' 
                   style='color: #2196F3; 
                          text-decoration: none; 
                          font-weight: bold;'>{row['title']}</a>
            </h3>
            <div style='height: 2px; 
                        background: var(--color-border-primary);
                        margin: 10px 0;'></div>
            <p style='color: var(--color-text-primary); margin: 8px 0;'>
                <span style='background-color: var(--color-accent-soft); 
                           padding: 2px 8px; 
                           border-radius: 12px;
                           font-size: 0.9em;
                           display: inline-block;'>
                    {row['category']}
                </span>
            </p>
            <p style='color: var(--color-text-primary); 
                      margin-top: auto; 
                      padding-top: 10px;
                      border-top: 1px solid var(--color-border-primary);'>
                ❤️ {row['likes']:,} likes
            </p>
        </div>
        """
    
    html_content += "</div></div>"
    
    # Add JavaScript for page navigation
    html_content += """
    <script>
    function page_change(page) {
        const pageEvent = new CustomEvent('page_change', { detail: page });
        document.dispatchEvent(pageEvent);
    }
    </script>
    """
    
    return html_content

def create_app():
    with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
        current_page = gr.State(value=1)
        
        gr.Markdown("""
        # 🤗 Hugging Face Spaces Explorer
        Explore and discover popular Hugging Face Spaces by category. 
        Any currently uncategorized spaces will be listed under "Other" or "All Categories", 
        if you would like to help make Spaces easier to search and filter through feel free 
        to add on to my project or recommend additional filters!
        """)
        
        with gr.Row():
            category_dropdown = gr.Dropdown(
                choices=["All Categories"] + sorted(df['category'].unique()),
                label="Select Category",
                value="All Categories"
            )
            search_input = gr.Textbox(
                label="Search Spaces",
                placeholder="Enter search terms..."
            )
        
        spaces_display = gr.HTML()
        
        def update_spaces(search_text, category, page):
            return search_spaces(search_text, category, page)
        
        # Event handlers
        category_dropdown.change(
            fn=update_spaces,
            inputs=[search_input, category_dropdown, gr.State(1)],
            outputs=spaces_display
        )
        
        search_input.change(
            fn=update_spaces,
            inputs=[search_input, category_dropdown, gr.State(1)],
            outputs=spaces_display
        )
        
        # Add page change handler
        app.load(js="""
        function setupPageChangeListener() {
            document.addEventListener('page_change', function(e) {
                const page = e.detail;
                gradioApp().querySelector('#update_page').click();
            });
        }
        setupPageChangeListener();
        """)
        
        update_page = gr.Button(visible=False, elem_id="update_page")
        update_page.click(
            fn=update_spaces,
            inputs=[search_input, category_dropdown, current_page],
            outputs=spaces_display
        )
    
    return app

# Launch the app
app = create_app()
app.launch(share=True)