Spaces:
Running
Running
Screwllum76
commited on
Commit
•
b3cc6b7
1
Parent(s):
77e08a4
Upload 5 files
Browse files- .gitattributes +37 -35
- .gitignore +2 -1
- README.md +8 -6
- app.py +284 -76
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -1,35 +1,37 @@
|
|
1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
-
*.
|
5 |
-
*.
|
6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
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 |
-
*
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.db* filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.ark* filter=lfs diff=lfs merge=lfs -text
|
30 |
+
**/*ckpt*data* filter=lfs diff=lfs merge=lfs -text
|
31 |
+
**/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text
|
32 |
+
**/*ckpt*.index filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*.gguf* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.ggml filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.llamafile* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
|
|
|
|
1 |
+
test.py
|
2 |
+
rename.sh
|
README.md
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
-
---
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Monitor
|
3 |
+
emoji: 📊
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: gray
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.9.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -1,85 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
from
|
|
|
|
|
4 |
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
try:
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
except Exception as e:
|
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 |
-
"key2": "val12",
|
59 |
-
...
|
60 |
-
},
|
61 |
-
{
|
62 |
-
"key1": "val21",
|
63 |
-
"key2": "val22",
|
64 |
-
...
|
65 |
-
},
|
66 |
-
...
|
67 |
-
]
|
68 |
-
```
|
69 |
-
## 支持的 JSON Lines 格式 (Supported jsonl format)
|
70 |
-
```
|
71 |
-
{"key1": "val11", "key2": "val12", ...}
|
72 |
-
{"key1": "val21", "key2": "val22", ...}
|
73 |
-
...
|
74 |
-
```
|
75 |
-
## 支持的 CSV 格式 (Supported CSV format)
|
76 |
-
```
|
77 |
-
key1, key2, ...
|
78 |
-
val11, val12, ...
|
79 |
-
val21, val22, ...
|
80 |
-
...
|
81 |
-
```
|
82 |
-
"""
|
83 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import json
|
4 |
+
import time
|
5 |
+
import random
|
6 |
+
import string
|
7 |
+
import requests
|
8 |
+
import schedule
|
9 |
import gradio as gr
|
10 |
import pandas as pd
|
11 |
+
from tqdm import tqdm
|
12 |
+
from functools import partial
|
13 |
+
from datetime import datetime, timedelta
|
14 |
|
15 |
+
TIMEOUT = 15
|
16 |
+
DELAY = 1
|
17 |
|
18 |
+
|
19 |
+
def start_monitor(url: str):
|
20 |
+
payload = {
|
21 |
+
"data": ["", ""],
|
22 |
+
"event_data": None, # 使用None来表示null
|
23 |
+
"fn_index": 0,
|
24 |
+
"trigger_id": 11,
|
25 |
+
"session_hash": "".join(
|
26 |
+
random.choice(string.ascii_lowercase) for _ in range(11)
|
27 |
+
),
|
28 |
+
}
|
29 |
+
response = requests.post(f"{url}/queue/join?", json=payload)
|
30 |
+
# 检查请求是否成功
|
31 |
+
if response.status_code == 200:
|
32 |
+
return "monitoring"
|
33 |
+
|
34 |
+
return "running"
|
35 |
+
|
36 |
+
|
37 |
+
def add_six_hours(match):
|
38 |
+
datetime_str = match.group(0)
|
39 |
+
dt = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
|
40 |
+
dt_plus_six = dt + timedelta(hours=6)
|
41 |
+
return dt_plus_six.strftime("%Y-%m-%d %H:%M:%S")
|
42 |
+
|
43 |
+
|
44 |
+
def fix_datetime(text: str):
|
45 |
+
datetime_pattern = r"\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\b"
|
46 |
+
return re.sub(datetime_pattern, add_six_hours, text)
|
47 |
+
|
48 |
+
|
49 |
+
def get_studios(username: str):
|
50 |
+
# 请求负载
|
51 |
+
payload = {
|
52 |
+
"PageNumber": 1,
|
53 |
+
"PageSize": 1000,
|
54 |
+
"Name": "",
|
55 |
+
"SortBy": "gmt_modified",
|
56 |
+
"Order": "desc",
|
57 |
+
}
|
58 |
+
try:
|
59 |
+
# 发送PUT请求
|
60 |
+
response = requests.put(
|
61 |
+
f"https://www.modelscope.cn/api/v1/studios/{username}/list",
|
62 |
+
data=json.dumps(payload),
|
63 |
+
headers={
|
64 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
|
65 |
+
},
|
66 |
+
timeout=TIMEOUT,
|
67 |
+
)
|
68 |
+
# 检查请求是否成功
|
69 |
+
response.raise_for_status()
|
70 |
+
# 解析JSON响应
|
71 |
+
spaces: list = response.json()["Data"]["Studios"]
|
72 |
+
if spaces:
|
73 |
+
studios = []
|
74 |
+
for space in spaces:
|
75 |
+
studios.append(
|
76 |
+
f"https://www.modelscope.cn/api/v1/studio/{username}/{space['Name']}/start_expired"
|
77 |
+
)
|
78 |
+
|
79 |
+
return studios
|
80 |
+
|
81 |
+
except requests.exceptions.Timeout as errt:
|
82 |
+
print(f"请求超时: {errt}, retrying...")
|
83 |
+
time.sleep(DELAY)
|
84 |
+
return get_studios(username)
|
85 |
+
|
86 |
+
except Exception as err:
|
87 |
+
print(f"请求发生错误: {err}")
|
88 |
+
|
89 |
+
return []
|
90 |
+
|
91 |
+
|
92 |
+
def get_spaces(username: str):
|
93 |
+
try:
|
94 |
+
# 发送GET请求
|
95 |
+
response = requests.get(
|
96 |
+
"https://huggingface.co/spaces-json",
|
97 |
+
params={"sort": "trending", "search": username},
|
98 |
+
headers={
|
99 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537"
|
100 |
+
},
|
101 |
+
timeout=TIMEOUT,
|
102 |
+
)
|
103 |
+
# 检查请求是否成功
|
104 |
+
response.raise_for_status()
|
105 |
+
# 解析JSON响应
|
106 |
+
spaces: list = response.json()["spaces"]
|
107 |
+
studios = []
|
108 |
+
for space in spaces:
|
109 |
+
if space["author"] == username:
|
110 |
+
studios.append(
|
111 |
+
f"https://{space['id'].replace('/', '-').replace('_', '-')}.hf.space"
|
112 |
+
)
|
113 |
+
|
114 |
+
return studios
|
115 |
+
|
116 |
+
except requests.exceptions.Timeout as errt:
|
117 |
+
print(f"请求超时: {errt}, retrying...")
|
118 |
+
time.sleep(DELAY)
|
119 |
+
return get_spaces(username)
|
120 |
+
|
121 |
+
except Exception as err:
|
122 |
+
print(f"请求发生错误: {err}")
|
123 |
+
|
124 |
+
return []
|
125 |
+
|
126 |
+
|
127 |
+
def activate_space(url: str):
|
128 |
+
status = "running"
|
129 |
try:
|
130 |
+
if ".hf.space" in url:
|
131 |
+
response = requests.get(url, timeout=TIMEOUT)
|
132 |
+
response.raise_for_status()
|
133 |
+
if "-keep-spaces-active.hf.space" in url:
|
134 |
+
status = start_monitor(url)
|
135 |
+
|
136 |
+
else:
|
137 |
+
response = requests.put(url, timeout=TIMEOUT)
|
138 |
+
response.raise_for_status()
|
139 |
+
print("Expired studio found, restarting...")
|
140 |
+
while (
|
141 |
+
requests.get(
|
142 |
+
url.replace("/start_expired", "/status"),
|
143 |
+
timeout=TIMEOUT,
|
144 |
+
).json()["Data"]["Status"]
|
145 |
+
== "ExpiredCreating"
|
146 |
+
):
|
147 |
+
requests.get(
|
148 |
+
url.replace("/api/v1/studio/", "/studios/").replace(
|
149 |
+
"/start_expired", ""
|
150 |
+
),
|
151 |
+
timeout=TIMEOUT,
|
152 |
+
)
|
153 |
+
time.sleep(5)
|
154 |
+
|
155 |
+
except requests.exceptions.Timeout as e:
|
156 |
+
if ".hf.space" in url:
|
157 |
+
status = "restarting"
|
158 |
+
else:
|
159 |
+
print(f"Failed to activate {url} : {e}, retrying...")
|
160 |
+
return activate_space(url)
|
161 |
+
|
162 |
+
except requests.RequestException as e:
|
163 |
+
if (
|
164 |
+
"500 Server Error:" in f"{e}"
|
165 |
+
and response.json()["Message"] == "studio is not expired"
|
166 |
+
):
|
167 |
+
status = "running"
|
168 |
+
else:
|
169 |
+
status = f"{e}"
|
170 |
|
171 |
except Exception as e:
|
172 |
+
status = f"{e}"
|
173 |
+
|
174 |
+
return status
|
175 |
+
|
176 |
+
|
177 |
+
def activate(hf_users: str, ms_users: str):
|
178 |
+
if not hf_users:
|
179 |
+
hf_users = os.getenv("hf_users")
|
180 |
+
|
181 |
+
if not ms_users:
|
182 |
+
ms_users = hf_users
|
183 |
+
|
184 |
+
hf_usernames = hf_users.split(";")
|
185 |
+
ms_usernames = ms_users.split(";")
|
186 |
+
spaces = []
|
187 |
+
for user in tqdm(hf_usernames, desc="Collecting spaces..."):
|
188 |
+
username = user.strip()
|
189 |
+
if username:
|
190 |
+
spaces += get_spaces(username)
|
191 |
+
time.sleep(DELAY)
|
192 |
+
|
193 |
+
monitors, studios = [], []
|
194 |
+
for space in spaces:
|
195 |
+
if "keep-spaces-active" in space:
|
196 |
+
monitors.append(space)
|
197 |
+
else:
|
198 |
+
studios.append(space)
|
199 |
+
spaces = monitors + studios
|
200 |
+
|
201 |
+
for user in tqdm(ms_usernames, desc="Collecting studios..."):
|
202 |
+
username = user.strip()
|
203 |
+
if username:
|
204 |
+
spaces += get_studios(username)
|
205 |
+
time.sleep(DELAY)
|
206 |
+
|
207 |
+
output = []
|
208 |
+
for space in tqdm(spaces, desc="Activating spaces..."):
|
209 |
+
output.append(
|
210 |
+
{
|
211 |
+
"space": space.split("//")[-1].replace(
|
212 |
+
"www.modelscope.cn/api/v1/studio/", ""
|
213 |
+
),
|
214 |
+
"status": activate_space(space),
|
215 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
)
|
217 |
+
time.sleep(DELAY)
|
218 |
+
|
219 |
+
print("Activation complete!")
|
220 |
+
return pd.DataFrame(output)
|
221 |
+
|
222 |
+
|
223 |
+
def monitor(hf_users: str, ms_users: str, period=4):
|
224 |
+
if schedule.get_jobs():
|
225 |
+
return
|
226 |
+
|
227 |
+
if not hf_users:
|
228 |
+
hf_users = os.getenv("hf_users")
|
229 |
+
|
230 |
+
if not ms_users:
|
231 |
+
ms_users = hf_users
|
232 |
+
|
233 |
+
print(f"监控开启中...每 {period} 小时触发")
|
234 |
+
fixed_activate = partial(activate, hf_users=hf_users, ms_users=ms_users)
|
235 |
+
schedule.every(period).hours.do(fixed_activate)
|
236 |
+
while True:
|
237 |
+
schedule.run_pending()
|
238 |
+
time.sleep(DELAY)
|
239 |
+
|
240 |
+
|
241 |
+
def listasks():
|
242 |
+
jobs = schedule.get_jobs()
|
243 |
+
if jobs:
|
244 |
+
details = f"{jobs}".replace("[", "").replace("]", "")
|
245 |
+
return fix_datetime(
|
246 |
+
details.split("functools.")[0] + "(" + details.split(") (")[-1]
|
247 |
+
)
|
248 |
+
|
249 |
+
return "None"
|
250 |
+
|
251 |
+
|
252 |
+
with gr.Blocks() as demo:
|
253 |
+
gr.Interface(
|
254 |
+
title="Start keeping all spaces active periodically",
|
255 |
+
fn=monitor,
|
256 |
+
inputs=[
|
257 |
+
gr.Textbox(
|
258 |
+
label="HuggingFace",
|
259 |
+
placeholder="Usernames joint by ;",
|
260 |
+
),
|
261 |
+
gr.Textbox(
|
262 |
+
label="ModelScope",
|
263 |
+
placeholder="Usernames joint by ;",
|
264 |
+
),
|
265 |
+
],
|
266 |
+
outputs=None,
|
267 |
+
allow_flagging="never",
|
268 |
+
)
|
269 |
+
gr.Interface(
|
270 |
+
title="See current task status",
|
271 |
+
fn=listasks,
|
272 |
+
inputs=None,
|
273 |
+
outputs=gr.Textbox(label="Current task details"),
|
274 |
+
allow_flagging="never",
|
275 |
+
)
|
276 |
+
gr.Interface(
|
277 |
+
title="Test activation for all spaces once",
|
278 |
+
fn=activate,
|
279 |
+
inputs=[
|
280 |
+
gr.Textbox(
|
281 |
+
label="HuggingFace",
|
282 |
+
placeholder="Usernames joint by ;",
|
283 |
+
),
|
284 |
+
gr.Textbox(
|
285 |
+
label="ModelScope",
|
286 |
+
placeholder="Usernames joint by ;",
|
287 |
+
),
|
288 |
+
],
|
289 |
+
outputs=gr.Dataframe(label="Activated spaces"),
|
290 |
+
allow_flagging="never",
|
291 |
+
)
|
292 |
|
293 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
beautifulsoup4
|
3 |
+
tqdm
|
4 |
+
schedule
|