-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_stats.py
More file actions
63 lines (51 loc) · 2.76 KB
/
update_stats.py
File metadata and controls
63 lines (51 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import requests
from huggingface_hub import HfApi
USERNAME = "AI4Protein"
README_PATH = "profile/README.md"
MODEL_LOG_FILE = "log_model_download.txt"
DATASET_LOG_FILE = "log_dataset_download.txt"
def get_github_stats(username):
repos_url = f"https://api.github.com/users/{username}/repos?per_page=100"
response = requests.get(repos_url)
repos = response.json()
if isinstance(repos, dict) and "message" in repos:
raise Exception(f"GitHub API error: {repos['message']}")
total_stars = sum(repo["stargazers_count"] for repo in repos)
total_forks = sum(repo["forks_count"] for repo in repos)
return total_stars, total_forks
def get_downloads():
total_model_downloads, total_dataset_downloads = 0, 0
with open(MODEL_LOG_FILE, 'r') as f:
for line in f:
line = line.strip()
total_model_downloads += int(line.split(',')[1])
with open(DATASET_LOG_FILE, 'r') as f:
for line in f:
line = line.strip()
total_dataset_downloads += int(line.split(',')[1])
# 加上千分位
total_model_downloads = "{:,}".format(total_model_downloads)
total_dataset_downloads = "{:,}".format(total_dataset_downloads)
return total_model_downloads, total_dataset_downloads
def update_readme(stars, forks, model_downloads, dataset_downloads):
with open(README_PATH, "r", encoding="utf-8") as f:
lines = f.readlines()
for i, line in enumerate(lines):
if "<!-- 🔄 stars -->" in line:
lines[i] = f" <!-- 🔄 stars -->\n"
if "<!-- 🔄 forks -->" in line:
lines[i] = f" <!-- 🔄 forks -->\n"
if "<!-- 🔄 total_hf_models -->" in line:
lines[i] = f" <!-- 🔄 total_hf_models -->\n"
if "<!-- 🔄 total_hf_datasets -->" in line:
lines[i] = f" <!-- 🔄 total_hf_datasets -->\n"
with open(README_PATH, "w", encoding="utf-8") as f:
f.writelines(lines)
if __name__ == "__main__":
try:
stars, forks = get_github_stats(USERNAME)
model_downloads, dataset_downloads = get_downloads()
update_readme(stars, forks, model_downloads, dataset_downloads)
print(f"✅ Updated profile/README.md — Stars: {stars}, Forks: {forks}")
except Exception as e:
print(f"❌ Error: {e}")