Spaces:
Herta83
/
Running

Herta83 commited on
Commit
6472efd
·
verified ·
1 Parent(s): d22f1af

Upload 5 files

Browse files
Files changed (5) hide show
  1. .gitattributes +37 -34
  2. .gitignore +2 -4
  3. README.md +10 -7
  4. app.py +276 -84
  5. requirements.txt +4 -5
.gitattributes CHANGED
@@ -1,34 +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
- *.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
 
 
 
 
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,4 +1,2 @@
1
- __pycache__/*
2
- *.pth
3
- flagged/*
4
- rename.sh
 
1
+ test.py
2
+ rename.sh
 
 
README.md CHANGED
@@ -1,11 +1,14 @@
1
  ---
2
- title: Door number recognition
3
- emoji: 🚪
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.6.0
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
- ---
 
 
 
 
1
  ---
2
+ title: Keep Spaces Active
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
+ short_description: Keep your spaces active without payment
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,101 +1,293 @@
1
  import os
2
- import torch
 
 
3
  import random
4
- import warnings
 
 
5
  import gradio as gr
6
- from PIL import Image
7
- from model import Model
8
- from torchvision import transforms
9
- from modelscope import snapshot_download
10
 
 
 
11
 
12
- MODEL_DIR = snapshot_download("MuGemSt/svhn", cache_dir="./__pycache__")
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- def infer(input_img: str, checkpoint_file: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  try:
17
- model = Model()
18
- model.restore(f"{MODEL_DIR}/{checkpoint_file}")
19
- outstr = ""
20
- with torch.no_grad():
21
- transform = transforms.Compose(
22
- [
23
- transforms.Resize([64, 64]),
24
- transforms.CenterCrop([54, 54]),
25
- transforms.ToTensor(),
26
- transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
27
- ]
28
- )
29
- image = Image.open(input_img)
30
- image = image.convert("RGB")
31
- image = transform(image)
32
- images = image.unsqueeze(dim=0)
33
- (
34
- length_logits,
35
- digit1_logits,
36
- digit2_logits,
37
- digit3_logits,
38
- digit4_logits,
39
- digit5_logits,
40
- ) = model.eval()(images)
41
- length_prediction = length_logits.max(1)[1]
42
- digit1_prediction = digit1_logits.max(1)[1]
43
- digit2_prediction = digit2_logits.max(1)[1]
44
- digit3_prediction = digit3_logits.max(1)[1]
45
- digit4_prediction = digit4_logits.max(1)[1]
46
- digit5_prediction = digit5_logits.max(1)[1]
47
- output = [
48
- digit1_prediction.item(),
49
- digit2_prediction.item(),
50
- digit3_prediction.item(),
51
- digit4_prediction.item(),
52
- digit5_prediction.item(),
53
- ]
54
-
55
- for i in range(length_prediction.item()):
56
- outstr += str(output[i])
57
-
58
- return outstr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  except Exception as e:
61
- return f"{e}"
62
-
63
-
64
- def get_files(dir_path=MODEL_DIR, ext=".pth"):
65
- files_and_folders = os.listdir(dir_path)
66
- outputs = []
67
- for file in files_and_folders:
68
- if file.endswith(ext):
69
- outputs.append(file)
70
-
71
- return outputs
72
-
73
-
74
- if __name__ == "__main__":
75
- warnings.filterwarnings("ignore")
76
- models = get_files()
77
- images = get_files(f"{MODEL_DIR}/examples", ".png")
78
- samples = []
79
- for img in images:
80
- samples.append(
81
- [
82
- f"{MODEL_DIR}/examples/{img}",
83
- models[random.randint(0, len(models) - 1)],
84
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  )
 
 
 
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  gr.Interface(
88
- fn=infer,
 
89
  inputs=[
90
- gr.Image(label="上传图片 Upload an image", type="filepath"),
91
- gr.Dropdown(
92
- label="选择权重 Select a model",
93
- choices=models,
94
- value=models[0],
 
 
95
  ),
96
  ],
97
- outputs=gr.Textbox(label="识别结果 Recognition result", show_copy_button=True),
98
- examples=samples,
99
  allow_flagging="never",
100
- cache_examples=False,
101
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 CHANGED
@@ -1,5 +1,4 @@
1
- gradio
2
- pillow
3
- torch
4
- torchvision
5
- modelscope==1.15
 
1
+ requests
2
+ beautifulsoup4
3
+ tqdm
4
+ schedule