-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathgenerate_model_list.py
153 lines (122 loc) · 4.06 KB
/
generate_model_list.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import re
from jinja2 import Template
MODEL_ROOT = "/xx/bos/community/"
URL_BASE = "https://paddlenlp.bj.bcebos.com/models/community/"
OUTPUT_DIR = "./website"
MAIN_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Model Downloads</title>
</head>
<body>
<h1>Available Models</h1>
<ul>
{% for model in models %}
<li><a href="{{ model }}/index.html">{{ model }}</a></li>
{% endfor %}
</ul>
</body>
</html>
"""
MODEL_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>{{ model_name }}</title>
</head>
<body>
<h1>{{ model_name }} Files</h1>
<ul>
{% for file in files %}
<li>
<a href="{{ model_path }}/{{ file.name }}" download>{{ file.name }}</a>
<span>({{ file.size }})</span>
</li>
{% endfor %}
</ul>
<p><a href="../../">Back to Main</a></p>
</body>
</html>
"""
def convert_size(size_bytes):
units = ["B", "KB", "MB", "GB", "TB"]
unit_index = 0
while size_bytes >= 1024 and unit_index < len(units) - 1:
size_bytes /= 1024.0
unit_index += 1
return f"{size_bytes:.1f} {units[unit_index]}"
def generate_model_page(model_path, model_name):
full_path = os.path.join(MODEL_ROOT, model_path)
files = []
for root, _, filenames in os.walk(full_path):
for f in filenames:
if f.endswith("index.html"):
continue
file_path = os.path.join(root, f)
rel_path = os.path.relpath(file_path, full_path)
size = os.path.getsize(file_path)
files.append({"name": rel_path, "size": convert_size(size)})
output_path = os.path.join(OUTPUT_DIR, model_path)
os.makedirs(output_path, exist_ok=True)
template = Template(MODEL_TEMPLATE)
html = template.render(
model_name=model_name, model_path=URL_BASE + model_path, files=sorted(files, key=lambda x: x["name"])
)
with open(os.path.join(output_path, "index.html"), "w") as f:
f.write(html)
def generate_main_page(models):
template = Template(MAIN_TEMPLATE)
html = template.render(models=sorted(models))
with open(os.path.join(OUTPUT_DIR, "index.html"), "w") as f:
f.write(html)
def is_model_directory(path):
if os.path.isfile(os.path.join(path, "model_index.json")):
return True
if not os.path.isfile(os.path.join(path, "config.json")):
return False
model_files = [
f
for f in os.listdir(path)
if f.startswith(("model", "pytorch_model"))
and (f.endswith(".safetensors") or f.endswith(".bin") or f.endswith(".pdparams"))
]
sharded_files = [f for f in os.listdir(path) if re.match(r"model-\d+-of-\d+\.safetensors", f)]
return len(model_files) > 0 or len(sharded_files) > 0
ommit_paths = ["_internal_", "hf-internal", "zhuweiguo"]
def find_models():
models = []
for root, dirs, _ in os.walk(MODEL_ROOT):
rel_path = os.path.relpath(root, MODEL_ROOT)
if any(p in rel_path for p in ommit_paths):
continue
print(rel_path)
if rel_path == ".":
continue
if is_model_directory(root):
models.append(rel_path)
dirs[:] = []
return models
def main():
os.makedirs(OUTPUT_DIR, exist_ok=True)
models = find_models()
generate_main_page(models)
for model_path in models:
model_name = os.path.basename(model_path)
generate_model_page(model_path, model_name)
if __name__ == "__main__":
main()