Python filter list_models() based on lastModified field

Hello, I am using hf HUB API to fetch data after calling list_models().
I don’t want to fetch data based on limit, I want it based on lastModified field in the model info.
For example:
list models that have lastModified > ‘2023-01-01’
Is there a way to do it?

Hi @MKABALAN, there is no built-in way to filter like this. However what you can do is to sort the search by lastModified and then iterate over the results until you reach the limit you’ve want. Since the results are paginated and lazy-loaded, you won’t have to fetch all models before filtering them. Here is an example to get the number of models that have been updated over the last week:

from datetime import datetime

from huggingface_hub import list_models


recent_models = []
for model in list_models(sort="lastModified", direction=-1):
    if model.last_modified is None or model.last_modified < datetime(2024, 1, 5, tzinfo=model.last_modified.tzinfo):
        break
    recent_models.append(model)

print(len(recent_models))

Which outputs 12665.

This does the trick thank you!

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.