Spaces:
Running
Running
import gradio as gr | |
from huggingface_hub import HfApi | |
from datetime import datetime, timedelta | |
import pandas as pd | |
# Initialize the Hugging Face API | |
api = HfApi() | |
def get_recent_models(min_likes, days_ago): | |
# Get the current date and date from `days_ago` days ago | |
today = datetime.utcnow().replace(tzinfo=None) | |
start_date = (today - timedelta(days=days_ago)).replace(tzinfo=None) | |
# Initialize an empty list to store the filtered models | |
recent_models = [] | |
# Use a generator to fetch models in batches, sorted by likes in descending order | |
for model in api.list_models(sort="likes", direction=-1): | |
if model.likes > 10: | |
if hasattr(model, "created_at") and model.created_at: | |
# Ensure created_at is offset-naive | |
created_at_date = model.created_at.replace(tzinfo=None) | |
if created_at_date >= start_date and model.likes >= min_likes: | |
task = model.pipeline_tag if hasattr(model, "pipeline_tag") else "N/A" | |
recent_models.append({ | |
"Model ID": f'<a href="https://huggingface.co/{model.modelId}" target="_blank">{model.modelId}</a>', | |
"Likes": model.likes, | |
"Creation Date": model.created_at.strftime("%Y-%m-%d %H:%M"), | |
"Task": task | |
}) | |
else: | |
# Since the models are sorted by likes in descending order, | |
# we can stop once we hit a model with 10 or fewer likes | |
break | |
# Convert the list of dictionaries to a pandas DataFrame | |
df = pd.DataFrame(recent_models) | |
return df | |
# Define the Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# Recently Created Models on Hugging Face") | |
gr.Markdown("Filter recent models from the Hugging Face Hub based on minimum likes and the number of days ago they were created.") | |
with gr.Row(): | |
likes_slider = gr.Slider(minimum=0, maximum=100, step=10, value=10, label="Minimum Likes") | |
days_slider = gr.Slider(minimum=1, maximum=7, step=1, value=1, label="Days Ago") | |
btn = gr.Button("Run") | |
with gr.Column(): | |
df = gr.DataFrame( | |
headers=["Model ID", "Likes", "Creation Date", "Task"], | |
wrap=True, | |
datatype=["html", "number", "str", "str"], | |
interactive=True | |
) | |
btn.click(fn=get_recent_models, inputs=[likes_slider, days_slider], outputs=df) | |
if __name__ == "__main__": | |
demo.launch() |