kakamond commited on
Commit
291db16
·
verified ·
1 Parent(s): ae4efa8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -84
app.py CHANGED
@@ -1,9 +1,6 @@
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
@@ -12,26 +9,30 @@ 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):
@@ -46,47 +47,47 @@ def fix_datetime(text: str):
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):
@@ -130,8 +131,8 @@ def activate_space(url: str):
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)
@@ -181,27 +182,28 @@ def activate(hf_users: str, ms_users: str):
181
  if not ms_users:
182
  ms_users = hf_users
183
 
184
- spaces, monitors, studios, output = [], [], [], []
 
185
  hf_usernames = hf_users.split(";")
186
- ms_usernames = ms_users.split(";")
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
- for space in spaces:
194
- if "keep-spaces-active" in space:
195
- monitors.append(space)
196
- else:
197
- studios.append(space)
198
 
199
- spaces = monitors + studios
200
- for user in tqdm(ms_usernames, desc="Collecting studios..."):
201
- username = user.strip()
202
- if username:
203
- spaces += get_studios(username)
204
- time.sleep(DELAY)
205
 
206
  for space in tqdm(spaces, desc="Activating spaces..."):
207
  output.append(
@@ -218,7 +220,11 @@ def activate(hf_users: str, ms_users: str):
218
  return pd.DataFrame(output)
219
 
220
 
221
- def monitor(hf_users: str, ms_users: str, period=4):
 
 
 
 
222
  if schedule.get_jobs():
223
  return
224
 
@@ -248,22 +254,24 @@ def listasks():
248
 
249
 
250
  if __name__ == "__main__":
 
251
  with gr.Blocks() as demo:
252
  gr.Interface(
253
  title="Start keeping all spaces active periodically",
254
  fn=monitor,
255
  inputs=[
256
- gr.Textbox(
257
- label="HuggingFace",
258
- placeholder="Usernames joint by ;",
259
- ),
260
- gr.Textbox(
261
- label="ModelScope",
262
- placeholder="Usernames joint by ;",
263
- ),
264
  ],
265
- outputs=None,
266
  flagging_mode="never",
 
267
  )
268
 
269
  gr.Interface(
 
1
  import os
2
  import re
 
3
  import time
 
 
4
  import requests
5
  import schedule
6
  import gradio as gr
 
9
  from functools import partial
10
  from datetime import datetime, timedelta
11
 
12
+ # import json
13
+ # import random
14
+ # import string
15
+
16
  TIMEOUT = 15
17
  DELAY = 1
18
 
19
 
20
+ # def start_monitor(url: str):
21
+ # payload = {
22
+ # "data": ["", ""],
23
+ # "event_data": None, # 使用None来表示null
24
+ # "fn_index": 0,
25
+ # "trigger_id": 11,
26
+ # "session_hash": "".join(
27
+ # random.choice(string.ascii_lowercase) for _ in range(11)
28
+ # ),
29
+ # }
30
+ # response = requests.post(f"{url}/queue/join?", json=payload)
31
+ # # 检查请求是否成功
32
+ # if response.status_code == 200:
33
+ # return "monitoring"
34
 
35
+ # return "running"
36
 
37
 
38
  def add_six_hours(match):
 
47
  return re.sub(datetime_pattern, add_six_hours, text)
48
 
49
 
50
+ # def get_studios(username: str):
51
+ # # 请求负载
52
+ # payload = {
53
+ # "PageNumber": 1,
54
+ # "PageSize": 1000,
55
+ # "Name": "",
56
+ # "SortBy": "gmt_modified",
57
+ # "Order": "desc",
58
+ # }
59
+ # try:
60
+ # # 发送PUT请求
61
+ # response = requests.put(
62
+ # f"https://www.modelscope.cn/api/v1/studios/{username}/list",
63
+ # data=json.dumps(payload),
64
+ # headers={
65
+ # "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"
66
+ # },
67
+ # timeout=TIMEOUT,
68
+ # )
69
+ # # 检查请求是否成功
70
+ # response.raise_for_status()
71
+ # # 解析JSON响应
72
+ # spaces: list = response.json()["Data"]["Studios"]
73
+ # if spaces:
74
+ # studios = []
75
+ # for space in spaces:
76
+ # studios.append(
77
+ # f"https://www.modelscope.cn/api/v1/studio/{username}/{space['Name']}/start_expired"
78
+ # )
79
+
80
+ # return studios
81
+
82
+ # except requests.exceptions.Timeout as errt:
83
+ # print(f"请求超时: {errt}, retrying...")
84
+ # time.sleep(DELAY)
85
+ # return get_studios(username)
86
+
87
+ # except Exception as err:
88
+ # print(f"请求发生错误: {err}")
89
+
90
+ # return []
91
 
92
 
93
  def get_spaces(username: str):
 
131
  if ".hf.space" in url:
132
  response = requests.get(url, timeout=TIMEOUT)
133
  response.raise_for_status()
134
+ # if "-keep-spaces-active.hf.space" in url:
135
+ # status = start_monitor(url)
136
 
137
  else:
138
  response = requests.put(url, timeout=TIMEOUT)
 
182
  if not ms_users:
183
  ms_users = hf_users
184
 
185
+ # spaces, monitors, studios, output = [], [], [], []
186
+ spaces, output = [], []
187
  hf_usernames = hf_users.split(";")
188
+ # ms_usernames = ms_users.split(";")
189
  for user in tqdm(hf_usernames, desc="Collecting spaces..."):
190
  username = user.strip()
191
  if username:
192
  spaces += get_spaces(username)
193
  time.sleep(DELAY)
194
 
195
+ # for space in spaces:
196
+ # if "keep-spaces-active" in space:
197
+ # monitors.append(space)
198
+ # else:
199
+ # studios.append(space)
200
 
201
+ # spaces = monitors + studios
202
+ # for user in tqdm(ms_usernames, desc="Collecting studios..."):
203
+ # username = user.strip()
204
+ # if username:
205
+ # spaces += get_studios(username)
206
+ # time.sleep(DELAY)
207
 
208
  for space in tqdm(spaces, desc="Activating spaces..."):
209
  output.append(
 
220
  return pd.DataFrame(output)
221
 
222
 
223
+ def monitor(
224
+ hf_users: str = os.getenv("hf_users"),
225
+ ms_users: str = os.getenv("hf_users"),
226
+ period=4,
227
+ ):
228
  if schedule.get_jobs():
229
  return
230
 
 
254
 
255
 
256
  if __name__ == "__main__":
257
+ # monitor(None, None)
258
  with gr.Blocks() as demo:
259
  gr.Interface(
260
  title="Start keeping all spaces active periodically",
261
  fn=monitor,
262
  inputs=[
263
+ # gr.Textbox(
264
+ # label="HuggingFace",
265
+ # placeholder="Usernames joint by ;",
266
+ # ),
267
+ # gr.Textbox(
268
+ # label="ModelScope",
269
+ # placeholder="Usernames joint by ;",
270
+ # ),
271
  ],
272
+ outputs="text",
273
  flagging_mode="never",
274
+ live=True,
275
  )
276
 
277
  gr.Interface(