kolaslab commited on
Commit
925914f
·
verified ·
1 Parent(s): 50e6b97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -26
app.py CHANGED
@@ -40,16 +40,24 @@ def cached_list_items(username, kind):
40
  @lru_cache(maxsize=1)
41
  def get_trending_accounts(limit=100):
42
  try:
 
 
43
  # Get spaces for stats calculation
44
  spaces_response = requests.get("https://huggingface.co/api/spaces",
45
  params={"limit": 10000},
46
  timeout=30)
47
 
 
 
 
 
 
 
48
  if spaces_response.status_code == 200:
49
  spaces = spaces_response.json()
50
 
51
  # Count spaces by owner
52
- owner_counts = {}
53
  for space in spaces:
54
  if '/' in space.get('id', ''):
55
  owner, _ = space.get('id', '').split('/', 1)
@@ -57,36 +65,61 @@ def get_trending_accounts(limit=100):
57
  owner = space.get('owner', '')
58
 
59
  if owner != 'None':
60
- owner_counts[owner] = owner_counts.get(owner, 0) + 1
61
-
62
- # Get top owners by count
63
- top_owners = sorted(owner_counts.items(), key=lambda x: x[1], reverse=True)[:limit]
64
-
65
- # Extract just the owner names for dropdown
66
- trending_authors = [owner for owner, count in top_owners]
67
 
68
- return trending_authors, top_owners
69
- else:
70
- # Fallback to API method if HTTP request fails
71
- trending_models = list(api.list_models(sort="trending", limit=limit))
72
- trending_datasets = list(api.list_datasets(sort="trending", limit=limit))
73
- trending_spaces = list(api.list_spaces(sort="trending", limit=limit))
 
74
 
75
- # Extract unique authors
76
- authors = set()
77
- for item in trending_models + trending_datasets + trending_spaces:
78
- if hasattr(item, "author"):
79
- authors.add(item.author)
80
- elif hasattr(item, "id") and "/" in item.id:
81
- authors.add(item.id.split("/")[0])
 
 
 
82
 
83
- # Return sorted list of unique authors and empty stats
84
- author_list = sorted(list(authors))[:limit]
85
- return author_list, [(author, 0) for author in author_list[:30]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  except Exception as e:
87
  st.error(f"Error fetching trending accounts: {str(e)}")
88
  fallback_authors = ["ritvik77", "facebook", "google", "stabilityai", "Salesforce", "tiiuae", "bigscience"]
89
- return fallback_authors, [(author, 0) for author in fallback_authors]
90
 
91
 
92
  # Rate limiting
@@ -252,11 +285,20 @@ with st.sidebar:
252
  ranking_data.index = ranking_data.index + 1 # Start index from 1 for ranking
253
 
254
  # Style the table
 
 
 
255
  st.dataframe(
256
  ranking_data,
257
  column_config={
258
  "Contributor": st.column_config.TextColumn("Contributor"),
259
- "Spaces Count": st.column_config.NumberColumn("Spaces Count (based on top 500 spaces)", format="%d")
 
 
 
 
 
 
260
  },
261
  use_container_width=True,
262
  hide_index=False
@@ -319,6 +361,18 @@ if username:
319
  if username in trending_accounts[:30]:
320
  rank = trending_accounts.index(username) + 1
321
  st.success(f"🏆 {username} is ranked #{rank} in the top trending contributors!")
 
 
 
 
 
 
 
 
 
 
 
 
322
 
323
  # Create a dictionary to store commits by type
324
  commits_by_type = {}
 
40
  @lru_cache(maxsize=1)
41
  def get_trending_accounts(limit=100):
42
  try:
43
+ trending_data = {"spaces": [], "models": []}
44
+
45
  # Get spaces for stats calculation
46
  spaces_response = requests.get("https://huggingface.co/api/spaces",
47
  params={"limit": 10000},
48
  timeout=30)
49
 
50
+ # Get models for stats calculation
51
+ models_response = requests.get("https://huggingface.co/api/models",
52
+ params={"limit": 10000},
53
+ timeout=30)
54
+
55
+ # Process spaces data
56
  if spaces_response.status_code == 200:
57
  spaces = spaces_response.json()
58
 
59
  # Count spaces by owner
60
+ owner_counts_spaces = {}
61
  for space in spaces:
62
  if '/' in space.get('id', ''):
63
  owner, _ = space.get('id', '').split('/', 1)
 
65
  owner = space.get('owner', '')
66
 
67
  if owner != 'None':
68
+ owner_counts_spaces[owner] = owner_counts_spaces.get(owner, 0) + 1
 
 
 
 
 
 
69
 
70
+ # Get top owners by count for spaces
71
+ top_owners_spaces = sorted(owner_counts_spaces.items(), key=lambda x: x[1], reverse=True)[:limit]
72
+ trending_data["spaces"] = top_owners_spaces
73
+
74
+ # Process models data
75
+ if models_response.status_code == 200:
76
+ models = models_response.json()
77
 
78
+ # Count models by owner
79
+ owner_counts_models = {}
80
+ for model in models:
81
+ if '/' in model.get('id', ''):
82
+ owner, _ = model.get('id', '').split('/', 1)
83
+ else:
84
+ owner = model.get('owner', '')
85
+
86
+ if owner != 'None':
87
+ owner_counts_models[owner] = owner_counts_models.get(owner, 0) + 1
88
 
89
+ # Get top owners by count for models
90
+ top_owners_models = sorted(owner_counts_models.items(), key=lambda x: x[1], reverse=True)[:limit]
91
+ trending_data["models"] = top_owners_models
92
+
93
+ # Combine rankings for overall trending (weighted average)
94
+ combined_scores = {}
95
+
96
+ # Add scores from spaces
97
+ for owner, count in trending_data["spaces"]:
98
+ if owner not in combined_scores:
99
+ combined_scores[owner] = {"spaces": 0, "models": 0, "total": 0}
100
+ combined_scores[owner]["spaces"] = count
101
+
102
+ # Add scores from models
103
+ for owner, count in trending_data["models"]:
104
+ if owner not in combined_scores:
105
+ combined_scores[owner] = {"spaces": 0, "models": 0, "total": 0}
106
+ combined_scores[owner]["models"] = count
107
+
108
+ # Calculate total score (spaces + models)
109
+ for owner in combined_scores:
110
+ combined_scores[owner]["total"] = combined_scores[owner]["spaces"] + combined_scores[owner]["models"]
111
+
112
+ # Sort by total score
113
+ sorted_combined = sorted(combined_scores.items(), key=lambda x: x[1]["total"], reverse=True)[:limit]
114
+
115
+ # Extract just the owner names for dropdown
116
+ trending_authors = [owner for owner, _ in sorted_combined]
117
+
118
+ return trending_authors, trending_data["spaces"], trending_data["models"]
119
  except Exception as e:
120
  st.error(f"Error fetching trending accounts: {str(e)}")
121
  fallback_authors = ["ritvik77", "facebook", "google", "stabilityai", "Salesforce", "tiiuae", "bigscience"]
122
+ return fallback_authors, [(author, 0) for author in fallback_authors], [(author, 0) for author in fallback_authors]
123
 
124
 
125
  # Rate limiting
 
285
  ranking_data.index = ranking_data.index + 1 # Start index from 1 for ranking
286
 
287
  # Style the table
288
+ # Add a score column based on spaces count
289
+ ranking_data["Score"] = ranking_data["Spaces Count"].apply(lambda x: x * 10) # Multiply by 10 for a score metric
290
+
291
  st.dataframe(
292
  ranking_data,
293
  column_config={
294
  "Contributor": st.column_config.TextColumn("Contributor"),
295
+ "Spaces Count": st.column_config.NumberColumn("Spaces Count (based on top 500 spaces)", format="%d"),
296
+ "Score": st.column_config.ProgressColumn(
297
+ "Score (within TOP 500 SPACES)",
298
+ min_value=0,
299
+ max_value=ranking_data["Score"].max() * 1.1, # Add 10% to max for visual scale
300
+ format="%d pts"
301
+ )
302
  },
303
  use_container_width=True,
304
  hide_index=False
 
361
  if username in trending_accounts[:30]:
362
  rank = trending_accounts.index(username) + 1
363
  st.success(f"🏆 {username} is ranked #{rank} in the top trending contributors!")
364
+
365
+ # Find user in spaces ranking
366
+ for i, (owner, count) in enumerate(top_owners_spaces):
367
+ if owner == username:
368
+ st.info(f"🚀 Spaces Ranking: #{i+1} with {count} spaces")
369
+ break
370
+
371
+ # Find user in models ranking
372
+ for i, (owner, count) in enumerate(top_owners_models):
373
+ if owner == username:
374
+ st.info(f"🧠 Models Ranking: #{i+1} with {count} models")
375
+ break
376
 
377
  # Create a dictionary to store commits by type
378
  commits_by_type = {}