spaces-explorer / app.py
jkorstad's picture
Create app.py
3403bc2 verified
raw
history blame
2.81 kB
import gradio as gr
import pandas as pd
def search_spaces(search_text, category):
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())]
spaces = spaces_df.nlargest(20, 'likes')[['title', 'likes', 'url', 'category']]
# Get category stats
total_spaces = len(spaces_df)
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: #f5f5f5; border-radius: 5px;'>
<h3>Statistics:</h3>
<p>Total Spaces: {total_spaces}</p>
<p>Total Likes: {total_likes:,}</p>
</div>
<div style='max-height: 500px; overflow-y: auto;'>
"""
for _, row in spaces.iterrows():
html_content += f"""
<div style='margin: 10px; padding: 15px; border: 1px solid #ddd; border-radius: 5px; background-color: white;'>
<h3><a href='{row['url']}' target='_blank' style='color: #2196F3; text-decoration: none;'>{row['title']}</a></h3>
<p>Category: {row['category']}</p>
<p>❤️ {row['likes']:,} likes</p>
</div>
"""
html_content += "</div>"
return html_content
# Create the Gradio interface
def create_app():
with gr.Blocks(title="Hugging Face Spaces Explorer", theme=gr.themes.Soft()) as app:
gr.Markdown("""
# 🤗 Hugging Face Spaces Explorer
Explore and discover popular Hugging Face Spaces by category
""")
with gr.Row():
with gr.Column(scale=1):
# Category selection
category_dropdown = gr.Dropdown(
choices=["All Categories"] + sorted(df['category'].unique()),
label="Select Category",
value="All Categories"
)
# Search box
search_input = gr.Textbox(
label="Search Spaces",
placeholder="Enter search terms..."
)
# Display area for spaces
spaces_display = gr.HTML(value=search_spaces("", "All Categories"))
# Update display when category or search changes
category_dropdown.change(
fn=search_spaces,
inputs=[search_input, category_dropdown],
outputs=spaces_display
)
search_input.change(
fn=search_spaces,
inputs=[search_input, category_dropdown],
outputs=spaces_display
)
return app
# Launch the app
app = create_app()
app.launch(share=True)