spaces-explorer / app.py
jkorstad's picture
Update app.py
aed7a0e verified
raw
history blame
11.3 kB
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 search_spaces(search_text="", category="All Categories", offset=0, page_size=30):
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())]
total_spaces = len(spaces_df)
spaces = spaces_df.nlargest(total_spaces, 'likes')[['title', 'likes', 'url', 'category']]
# Get category stats
total_likes = spaces_df['likes'].sum()
# Format the results as HTML with clickable links and stats
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);'>Showing {min(offset + page_size, total_spaces)} of {total_spaces} Spaces</p>
<p style='color: var(--color-text-primary);'>Total Likes: {total_likes:,}</p>
</div>
<div style='max-height: 800px; overflow-y: auto;' id='spaces-container'>
<div style='display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; padding: 10px;'>
"""
# Only show the spaces from offset to offset + page_size
visible_spaces = spaces.iloc[offset:offset + page_size]
for _, row in visible_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%;
position: relative;'>
<h3 style='margin-top: 0; margin-bottom: 10px;'>
<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;
width: 100%;'></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;'>
{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>"
has_more = offset + page_size < total_spaces
return html_content, has_more, total_spaces - (offset + page_size)
def create_app():
with gr.Blocks(
title="Hugging Face Spaces Explorer",
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="blue",
)
) as app:
current_offset = gr.State(value=0)
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():
with gr.Column(scale=1):
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()
load_more_btn = gr.Button("Load More", visible=False)
remaining_text = gr.Markdown(visible=False)
def update_display(search_text, category, offset):
content, has_more, remaining = search_spaces(search_text, category, offset)
return {
spaces_display: content,
load_more_btn: gr.update(visible=has_more), # Changed from gr.Button.update
remaining_text: gr.update( # Changed from gr.Markdown.update
visible=has_more,
value=f"*{remaining} more spaces available*"
)
}
def load_more(search_text, category, offset):
new_offset = offset + 30
return update_display(search_text, category, new_offset) | {'current_offset': new_offset}
# Initial load
app.load(
fn=lambda: update_display("", "All Categories", 0),
outputs=[spaces_display, load_more_btn, remaining_text]
)
# Update display when category or search changes
category_dropdown.change(
fn=lambda x, y, _: update_display(x, y, 0) | {'current_offset': 0},
inputs=[search_input, category_dropdown, current_offset],
outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
)
search_input.change(
fn=lambda x, y, _: update_display(x, y, 0) | {'current_offset': 0},
inputs=[search_input, category_dropdown, current_offset],
outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
)
# Load More button handler
load_more_btn.click(
fn=load_more,
inputs=[search_input, category_dropdown, current_offset],
outputs=[spaces_display, load_more_btn, remaining_text, current_offset]
)
return app
# Launch the app
app = create_app()
app.launch(share=True)