kolaslab commited on
Commit
f4bfa4e
·
verified ·
1 Parent(s): d8bbec8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -51
app.py CHANGED
@@ -7,6 +7,8 @@ from datetime import datetime
7
  from concurrent.futures import ThreadPoolExecutor, as_completed
8
  from functools import lru_cache
9
  import time
 
 
10
 
11
  st.set_page_config(page_title="HF Contributions", layout="wide")
12
  api = HfApi()
@@ -34,28 +36,57 @@ def cached_list_items(username, kind):
34
  return []
35
 
36
 
37
- # Function to fetch trending model repositories
38
  @lru_cache(maxsize=1)
39
  def get_trending_accounts(limit=100):
40
  try:
41
- # Fetch trending models, datasets, and spaces
42
- trending_models = list(api.list_models(sort="trending", limit=limit))
43
- trending_datasets = list(api.list_datasets(sort="trending", limit=limit))
44
- trending_spaces = list(api.list_spaces(sort="trending", limit=limit))
45
 
46
- # Extract unique authors
47
- authors = set()
48
- for item in trending_models + trending_datasets + trending_spaces:
49
- if hasattr(item, "author"):
50
- authors.add(item.author)
51
- elif hasattr(item, "id") and "/" in item.id:
52
- authors.add(item.id.split("/")[0])
53
-
54
- # Return sorted list of unique authors
55
- return sorted(list(authors))[:limit]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  except Exception as e:
57
  st.error(f"Error fetching trending accounts: {str(e)}")
58
- return ["ritvik77", "facebook", "google", "stabilityai", "Salesforce", "tiiuae", "bigscience"]
 
59
 
60
 
61
  # Rate limiting
@@ -207,51 +238,61 @@ with st.sidebar:
207
 
208
  # Fetch trending accounts with a loading spinner
209
  with st.spinner("Loading top trending accounts..."):
210
- trending_accounts = get_trending_accounts(limit=100)
211
 
212
- # Create a tab interface for selection method
213
- tab1, tab2 = st.tabs(["Top 100 Trending", "Custom User"])
214
 
215
- with tab1:
216
- # Show trending accounts list with search filter
217
- st.subheader("🔥 Top Trending Accounts")
218
- search_filter = st.text_input("Filter accounts", key="trending_filter")
219
-
220
- # Filter accounts based on search
221
- filtered_accounts = [acc for acc in trending_accounts if search_filter.lower() in acc.lower()] if search_filter else trending_accounts
222
-
223
- # Show account count
224
- st.caption(f"Showing {len(filtered_accounts)} accounts")
225
-
226
- # Create a scrollable container for the accounts list
227
- account_container = st.container()
228
- with account_container:
229
- selected_trending = st.selectbox(
230
- "Select trending account",
231
- options=filtered_accounts,
232
- index=0 if filtered_accounts else None,
233
- key="trending_selectbox"
234
- )
235
 
236
- if selected_trending:
237
- username = selected_trending
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
- with tab2:
240
- st.subheader("🔎 Custom Account")
241
- default_accounts = ["ritvik77", "facebook", "google", "stabilityai", "Salesforce", "tiiuae", "bigscience"]
242
- custom_account = st.selectbox(
243
- "Select or type a username",
244
- options=default_accounts,
245
- index=0
 
246
  )
 
 
247
  st.markdown("<div style='text-align: center; margin: 10px 0;'>OR</div>", unsafe_allow_html=True)
248
  custom = st.text_input("", placeholder="Enter custom username/org")
 
 
249
  if custom.strip():
250
  username = custom.strip()
251
- elif tab2._active and not tab1._active: # Only set if tab2 is active
252
- username = custom_account
 
 
253
 
254
- # Year selection (outside tabs to always be visible)
255
  st.subheader("🗓️ Time Period")
256
  year_options = list(range(datetime.now().year, 2017, -1))
257
  selected_year = st.selectbox("Select Year", options=year_options)
@@ -266,6 +307,11 @@ with st.sidebar:
266
  st.title("🤗 Hugging Face Contributions")
267
  if username:
268
  with st.spinner(f"Fetching commit data for {username}..."):
 
 
 
 
 
269
  # Create a dictionary to store commits by type
270
  commits_by_type = {}
271
  commit_counts_by_type = {}
@@ -342,6 +388,13 @@ if username:
342
 
343
  with profile_col2:
344
  st.metric("Total Commits", total_commits)
 
 
 
 
 
 
 
345
  st.markdown(f"[View Profile on Hugging Face](https://huggingface.co/{username})")
346
 
347
  # Create DataFrame for all commits
 
7
  from concurrent.futures import ThreadPoolExecutor, as_completed
8
  from functools import lru_cache
9
  import time
10
+ import requests
11
+ from collections import Counter
12
 
13
  st.set_page_config(page_title="HF Contributions", layout="wide")
14
  api = HfApi()
 
36
  return []
37
 
38
 
39
+ # Function to fetch trending accounts and create stats
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)
56
+ else:
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
 
238
 
239
  # Fetch trending accounts with a loading spinner
240
  with st.spinner("Loading top trending accounts..."):
241
+ trending_accounts, top_owners = get_trending_accounts(limit=100)
242
 
243
+ # Show trending accounts list with search filter
244
+ st.subheader("🔥 Top Trending Accounts")
245
 
246
+ # Add stats expander
247
+ with st.expander("View Top 30 Contributor Stats"):
248
+ # Create a bar chart for top 30 contributors
249
+ if top_owners:
250
+ chart_data = pd.DataFrame(top_owners[:30], columns=["Owner", "Spaces Count"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
 
252
+ fig, ax = plt.subplots(figsize=(10, 8))
253
+ bars = ax.barh(chart_data["Owner"], chart_data["Spaces Count"])
254
+
255
+ # Add color gradient to bars
256
+ for i, bar in enumerate(bars):
257
+ bar.set_color(plt.cm.viridis(i/len(bars)))
258
+
259
+ ax.set_title("Top 30 Contributors by Number of Spaces")
260
+ ax.set_xlabel("Number of Spaces")
261
+ plt.tight_layout()
262
+ st.pyplot(fig)
263
+
264
+ # Search filter
265
+ search_filter = st.text_input("Filter accounts", key="trending_filter")
266
+
267
+ # Filter accounts based on search
268
+ filtered_accounts = [acc for acc in trending_accounts if search_filter.lower() in acc.lower()] if search_filter else trending_accounts
269
+
270
+ # Show account count
271
+ st.caption(f"Showing {len(filtered_accounts)} of {len(trending_accounts)} accounts")
272
 
273
+ # Create a scrollable container for the accounts list
274
+ account_container = st.container()
275
+ with account_container:
276
+ selected_trending = st.selectbox(
277
+ "Select trending account",
278
+ options=filtered_accounts,
279
+ index=0 if filtered_accounts else None,
280
+ key="trending_selectbox"
281
  )
282
+
283
+ # Custom account input option
284
  st.markdown("<div style='text-align: center; margin: 10px 0;'>OR</div>", unsafe_allow_html=True)
285
  custom = st.text_input("", placeholder="Enter custom username/org")
286
+
287
+ # Set username based on selection or custom input
288
  if custom.strip():
289
  username = custom.strip()
290
+ elif selected_trending:
291
+ username = selected_trending
292
+ else:
293
+ username = "facebook" # Default fallback
294
 
295
+ # Year selection
296
  st.subheader("🗓️ Time Period")
297
  year_options = list(range(datetime.now().year, 2017, -1))
298
  selected_year = st.selectbox("Select Year", options=year_options)
 
307
  st.title("🤗 Hugging Face Contributions")
308
  if username:
309
  with st.spinner(f"Fetching commit data for {username}..."):
310
+ # Display contributor rank if in top 100
311
+ if username in trending_accounts:
312
+ rank = trending_accounts.index(username) + 1
313
+ st.success(f"🏆 {username} is ranked #{rank} in the top trending contributors!")
314
+
315
  # Create a dictionary to store commits by type
316
  commits_by_type = {}
317
  commit_counts_by_type = {}
 
388
 
389
  with profile_col2:
390
  st.metric("Total Commits", total_commits)
391
+
392
+ # Show contributor rank if in top owners
393
+ for owner, count in top_owners:
394
+ if owner.lower() == username.lower():
395
+ st.metric("Spaces Count", count)
396
+ break
397
+
398
  st.markdown(f"[View Profile on Hugging Face](https://huggingface.co/{username})")
399
 
400
  # Create DataFrame for all commits