Azazelle commited on
Commit
20372e0
ยท
verified ยท
1 Parent(s): 8ca51fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -60
app.py CHANGED
@@ -7,68 +7,37 @@ import pandas as pd
7
  api = HfApi()
8
 
9
  def get_recent_models(min_likes, days_ago, filter_string, search_string):
10
- # Get the current date and date from `days_ago` days ago
11
- today = datetime.utcnow().replace(tzinfo=None)
12
- start_date = (today - timedelta(days=days_ago)).replace(tzinfo=None)
 
 
 
13
 
14
  # Initialize an empty list to store the filtered models
15
  recent_models = []
16
 
17
- # Split filter and search strings into lists of substrings
18
- filter_substrings = filter_string.lower().split(';') if filter_string else []
19
- search_substrings = search_string.lower().split(';') if search_string else []
20
-
21
- # Use a generator to fetch models in batches, sorted by likes in descending order
22
  for model in api.list_models(sort="likes", direction=-1):
23
- if model.likes >= min_likes:
24
- if hasattr(model, "created_at") and model.created_at:
25
- # Ensure created_at is offset-naive
26
- created_at_date = model.created_at.replace(tzinfo=None)
27
- if search_substrings:
28
- if any(term in model.modelId.lower() for term in search_substrings):
29
- if filter_substrings:
30
- if not any(sub in model.modelId.lower() for sub in filter_substrings):
31
- if created_at_date >= start_date:
32
- task = model.pipeline_tag if hasattr(model, "pipeline_tag") else "N/A"
33
- recent_models.append({
34
- "Model ID": f'<a href="https://huggingface.co/{model.modelId}" target="_blank">{model.modelId}</a>',
35
- "Likes": model.likes,
36
- "Creation Date": model.created_at.strftime("%Y-%m-%d %H:%M"),
37
- "Task": task
38
- })
39
- else:
40
- if created_at_date >= start_date:
41
- task = model.pipeline_tag if hasattr(model, "pipeline_tag") else "N/A"
42
- recent_models.append({
43
- "Model ID": f'<a href="https://huggingface.co/{model.modelId}" target="_blank">{model.modelId}</a>',
44
- "Likes": model.likes,
45
- "Creation Date": model.created_at.strftime("%Y-%m-%d %H:%M"),
46
- "Task": task
47
- })
48
- else:
49
- if filter_substrings:
50
- if not any(sub in model.modelId.lower() for sub in filter_substrings):
51
- if created_at_date >= start_date:
52
- task = model.pipeline_tag if hasattr(model, "pipeline_tag") else "N/A"
53
- recent_models.append({
54
- "Model ID": f'<a href="https://huggingface.co/{model.modelId}" target="_blank">{model.modelId}</a>',
55
- "Likes": model.likes,
56
- "Creation Date": model.created_at.strftime("%Y-%m-%d %H:%M"),
57
- "Task": task
58
- })
59
- else:
60
- if created_at_date >= start_date:
61
- task = model.pipeline_tag if hasattr(model, "pipeline_tag") else "N/A"
62
- recent_models.append({
63
- "Model ID": f'<a href="https://huggingface.co/{model.modelId}" target="_blank">{model.modelId}</a>',
64
- "Likes": model.likes,
65
- "Creation Date": model.created_at.strftime("%Y-%m-%d %H:%M"),
66
- "Task": task
67
- })
68
- else:
69
- # Since the models are sorted by likes in descending order,
70
- # we can stop once we hit a model with 10 or fewer likes
71
  break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  # Convert the list of dictionaries to a pandas DataFrame
74
  df = pd.DataFrame(recent_models)
@@ -78,14 +47,21 @@ def get_recent_models(min_likes, days_ago, filter_string, search_string):
78
  # Define the Gradio interface
79
  with gr.Blocks() as demo:
80
  gr.Markdown("# Model Drops Tracker ๐Ÿš€")
81
- gr.Markdown("Overwhelmed by the rapid pace of model releases? ๐Ÿ˜… You're not alone! That's exactly why I built this tool. Easily filter recent models from the Hub by setting a minimum number of likes and the number of days since their release. Click on a model to see its card. Use `;` to split filter and search")
 
 
 
 
 
 
82
  with gr.Row():
83
  likes_slider = gr.Slider(minimum=1, maximum=100, step=1, value=5, label="Minimum Likes")
84
  days_slider = gr.Slider(minimum=1, maximum=30, step=1, value=3, label="Days Ago")
85
- with gr.Row():
86
- filter_text = gr.Text(label="Filter", max_lines=1)
87
- search_text = gr.Text(label="Search", max_lines=1)
88
 
 
 
 
 
89
  btn = gr.Button("Run")
90
 
91
  with gr.Column():
 
7
  api = HfApi()
8
 
9
  def get_recent_models(min_likes, days_ago, filter_string, search_string):
10
+ # Calculate the start date for filtering models
11
+ start_date = datetime.utcnow() - timedelta(days=days_ago)
12
+
13
+ # Prepare filter and search substrings
14
+ filter_substrings = {sub.strip().lower() for sub in filter_string.split(';') if sub.strip()}
15
+ search_substrings = {term.strip().lower() for term in search_string.split(';') if term.strip()}
16
 
17
  # Initialize an empty list to store the filtered models
18
  recent_models = []
19
 
20
+ # Fetch models sorted by likes in descending order
 
 
 
 
21
  for model in api.list_models(sort="likes", direction=-1):
22
+ if model.likes < min_likes:
23
+ # Since models are sorted by likes in descending order, break early
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  break
25
+
26
+ created_at_date = model.created_at.replace(tzinfo=None) if model.created_at else None
27
+
28
+ # Ensure the model meets the date, like, search, and filter criteria
29
+ if created_at_date and created_at_date >= start_date:
30
+ model_id_lower = model.modelId.lower()
31
+ if (not search_substrings or any(term in model_id_lower for term in search_substrings)) and \
32
+ (not filter_substrings or not any(sub in model_id_lower for sub in filter_substrings)):
33
+
34
+ task = model.pipeline_tag if hasattr(model, "pipeline_tag") else "N/A"
35
+ recent_models.append({
36
+ "Model ID": f'<a href="https://huggingface.co/{model.modelId}" target="_blank">{model.modelId}</a>',
37
+ "Likes": model.likes,
38
+ "Creation Date": created_at_date.strftime("%Y-%m-%d %H:%M"),
39
+ "Task": task
40
+ })
41
 
42
  # Convert the list of dictionaries to a pandas DataFrame
43
  df = pd.DataFrame(recent_models)
 
47
  # Define the Gradio interface
48
  with gr.Blocks() as demo:
49
  gr.Markdown("# Model Drops Tracker ๐Ÿš€")
50
+ gr.Markdown(
51
+ "Overwhelmed by the rapid pace of model releases? ๐Ÿ˜… You're not alone! "
52
+ "That's exactly why I built this tool. Easily filter recent models from the Hub "
53
+ "by setting a minimum number of likes and the number of days since their release. "
54
+ "Click on a model to see its card. Use `;` to split filter and search terms."
55
+ )
56
+
57
  with gr.Row():
58
  likes_slider = gr.Slider(minimum=1, maximum=100, step=1, value=5, label="Minimum Likes")
59
  days_slider = gr.Slider(minimum=1, maximum=30, step=1, value=3, label="Days Ago")
 
 
 
60
 
61
+ with gr.Row():
62
+ filter_text = gr.Text(label="Filter", max_lines=1, placeholder="Exclude models containing these terms (separate by `;`)")
63
+ search_text = gr.Text(label="Search", max_lines=1, placeholder="Include only models containing these terms (separate by `;`)")
64
+
65
  btn = gr.Button("Run")
66
 
67
  with gr.Column():