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?
2024-01-12T10:14:22Z
Hi@MKABALAN, there is no built-in way to filter like this. However what you can do is to sort the search bylastModifiedand 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 outputs12665.
2024-01-13T13:45:24Z
This does the trick thank you!
2024-01-14T01:46:08Z
This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.