Spaces:
Running
Running
admin
commited on
Commit
•
e9222b3
1
Parent(s):
916278b
Update app.py
Browse files
app.py
CHANGED
@@ -1,230 +1,230 @@
|
|
1 |
-
import json
|
2 |
-
import time
|
3 |
-
import requests
|
4 |
-
import schedule
|
5 |
-
import gradio as gr
|
6 |
-
import pandas as pd
|
7 |
-
from tqdm import tqdm
|
8 |
-
from bs4 import BeautifulSoup
|
9 |
-
from urllib.parse import urlparse
|
10 |
-
|
11 |
-
TIMEOUT = 15
|
12 |
-
DELAY = 1
|
13 |
-
|
14 |
-
|
15 |
-
def get_studios(username: str):
|
16 |
-
# 请求负载
|
17 |
-
payload = {
|
18 |
-
"PageNumber": 1,
|
19 |
-
"PageSize": 1000,
|
20 |
-
"Name": "",
|
21 |
-
"SortBy": "gmt_modified",
|
22 |
-
"Order": "desc",
|
23 |
-
}
|
24 |
-
try:
|
25 |
-
# 发送PUT请求
|
26 |
-
response = requests.put(
|
27 |
-
f"https://www.modelscope.cn/api/v1/studios/{username}/list",
|
28 |
-
data=json.dumps(payload),
|
29 |
-
headers={
|
30 |
-
"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"
|
31 |
-
},
|
32 |
-
timeout=TIMEOUT,
|
33 |
-
)
|
34 |
-
# 检查请求是否成功
|
35 |
-
response.raise_for_status()
|
36 |
-
# 解析JSON响应
|
37 |
-
spaces: list = response.json()["Data"]["Studios"]
|
38 |
-
if spaces:
|
39 |
-
studios = []
|
40 |
-
for space in spaces:
|
41 |
-
studios.append(
|
42 |
-
f"https://www.modelscope.cn/studios/{username}/{space['Name']}"
|
43 |
-
)
|
44 |
-
|
45 |
-
return studios
|
46 |
-
|
47 |
-
except requests.exceptions.HTTPError as errh:
|
48 |
-
print(f"HTTP错误发生: {errh}")
|
49 |
-
except requests.exceptions.ConnectionError as errc:
|
50 |
-
print(f"连接错误发生: {errc}")
|
51 |
-
except requests.exceptions.Timeout as errt:
|
52 |
-
print(f"请求超时: {errt}, retrying...")
|
53 |
-
time.sleep(DELAY)
|
54 |
-
return get_studios(username)
|
55 |
-
|
56 |
-
except requests.exceptions.RequestException as err:
|
57 |
-
print(f"请求发生错误: {err}")
|
58 |
-
|
59 |
-
return []
|
60 |
-
|
61 |
-
|
62 |
-
def get_spaces(username: str):
|
63 |
-
try:
|
64 |
-
# 发送GET请求
|
65 |
-
response = requests.get(
|
66 |
-
"https://huggingface.co/spaces-json",
|
67 |
-
params={"sort": "trending", "search": username},
|
68 |
-
headers={
|
69 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537"
|
70 |
-
},
|
71 |
-
timeout=TIMEOUT,
|
72 |
-
)
|
73 |
-
# 检查请求是否成功
|
74 |
-
response.raise_for_status()
|
75 |
-
# 解析JSON响应
|
76 |
-
spaces: list = response.json()["spaces"]
|
77 |
-
studios = []
|
78 |
-
for space in spaces:
|
79 |
-
if space["author"] == username:
|
80 |
-
studios.append(f"https://huggingface.co/spaces/{space['id']}")
|
81 |
-
|
82 |
-
return studios
|
83 |
-
|
84 |
-
except requests.exceptions.HTTPError as errh:
|
85 |
-
print(f"HTTP错误发生: {errh}")
|
86 |
-
|
87 |
-
except requests.exceptions.Timeout as errt:
|
88 |
-
print(f"请求超时: {errt}, retrying...")
|
89 |
-
time.sleep(DELAY)
|
90 |
-
return get_spaces(username)
|
91 |
-
|
92 |
-
except requests.exceptions.RequestException as err:
|
93 |
-
print(f"请求发生错误: {err}")
|
94 |
-
|
95 |
-
return []
|
96 |
-
|
97 |
-
|
98 |
-
def activate_space(url: str):
|
99 |
-
success = "success"
|
100 |
-
try:
|
101 |
-
# 发送GET请求获取页面内容
|
102 |
-
response = requests.get(url, timeout=TIMEOUT)
|
103 |
-
response.raise_for_status() # 确保请求成功
|
104 |
-
web_page_content = response.text
|
105 |
-
|
106 |
-
# 使用BeautifulSoup解析页面
|
107 |
-
soup = BeautifulSoup(web_page_content, "html.parser")
|
108 |
-
|
109 |
-
# 寻找class为btn-lg的按钮
|
110 |
-
button = soup.find("button", class_="btn-lg")
|
111 |
-
if not button:
|
112 |
-
# print("未找到class为'btn-lg'的按钮。")
|
113 |
-
return success
|
114 |
-
|
115 |
-
# 获取表单的action属性和method属性
|
116 |
-
form = button.find_parent("form")
|
117 |
-
if not form:
|
118 |
-
return "按钮未在表单内找到。"
|
119 |
-
|
120 |
-
form_action = form.get("action")
|
121 |
-
form_method = form.get("method", "GET").upper() # 默认为GET
|
122 |
-
host = "https://" + urlparse(url).hostname
|
123 |
-
if not host in form_action:
|
124 |
-
form_action = host + form_action
|
125 |
-
|
126 |
-
# 提取表单数据
|
127 |
-
form_data = {
|
128 |
-
input_tag.get("name"): input_tag.get("value")
|
129 |
-
for input_tag in form.find_all("input")
|
130 |
-
if input_tag.has_attr("name")
|
131 |
-
}
|
132 |
-
|
133 |
-
# 发送请求提交表单
|
134 |
-
if form_method == "POST":
|
135 |
-
response = requests.post(form_action, data=form_data)
|
136 |
-
else:
|
137 |
-
response = requests.get(form_action, params=form_data)
|
138 |
-
|
139 |
-
# 打印响应内容
|
140 |
-
print("提交表单后的页面内容:")
|
141 |
-
print(response.text)
|
142 |
-
|
143 |
-
except requests.exceptions.Timeout as errt:
|
144 |
-
print(f"请求超时: {errt}, retrying...")
|
145 |
-
time.sleep(DELAY)
|
146 |
-
return activate_space(url)
|
147 |
-
|
148 |
-
except requests.RequestException as e:
|
149 |
-
success = f"{e}"
|
150 |
-
|
151 |
-
except Exception as e:
|
152 |
-
success = f"{e}"
|
153 |
-
|
154 |
-
return success
|
155 |
-
|
156 |
-
|
157 |
-
def activate(hf_users: str, ms_users: str):
|
158 |
-
hf_usernames = hf_users.split(";")
|
159 |
-
ms_usernames = ms_users.split(";")
|
160 |
-
spaces = []
|
161 |
-
for user in tqdm(hf_usernames, desc="Collecting spaces..."):
|
162 |
-
username = user.strip()
|
163 |
-
if username:
|
164 |
-
spaces += get_spaces(username)
|
165 |
-
time.sleep(DELAY)
|
166 |
-
|
167 |
-
for user in tqdm(ms_usernames, desc="Collecting studios..."):
|
168 |
-
username = user.strip()
|
169 |
-
if username:
|
170 |
-
spaces += get_studios(username)
|
171 |
-
time.sleep(DELAY)
|
172 |
-
|
173 |
-
output = []
|
174 |
-
for space in tqdm(spaces, desc="Activating spaces..."):
|
175 |
-
output.append({"space": space, "status": activate_space(space)})
|
176 |
-
time.sleep(DELAY)
|
177 |
-
|
178 |
-
print("Activation complete!")
|
179 |
-
return pd.DataFrame(output)
|
180 |
-
|
181 |
-
|
182 |
-
def monitor(hf_users: str, ms_users: str,
|
183 |
-
if schedule.get_jobs():
|
184 |
-
return
|
185 |
-
|
186 |
-
print(f"
|
187 |
-
schedule.every().
|
188 |
-
while True:
|
189 |
-
schedule.run_pending()
|
190 |
-
time.sleep(DELAY)
|
191 |
-
|
192 |
-
|
193 |
-
def list_tasks():
|
194 |
-
jobs = schedule.get_jobs()
|
195 |
-
if jobs:
|
196 |
-
return f"{jobs}".replace("[", "").replace("]", "")
|
197 |
-
|
198 |
-
return "None"
|
199 |
-
|
200 |
-
|
201 |
-
with gr.Blocks() as iface:
|
202 |
-
gr.Interface(
|
203 |
-
title="Start keeping all spaces active daily",
|
204 |
-
fn=monitor,
|
205 |
-
inputs=[
|
206 |
-
gr.Textbox(label="HuggingFace", placeholder="Usernames joint by ;"),
|
207 |
-
gr.Textbox(label="ModelScope", placeholder="Usernames joint by ;"),
|
208 |
-
],
|
209 |
-
outputs=None,
|
210 |
-
allow_flagging=False,
|
211 |
-
)
|
212 |
-
gr.Interface(
|
213 |
-
title="See current task status",
|
214 |
-
fn=list_tasks,
|
215 |
-
inputs=None,
|
216 |
-
outputs=gr.Textbox(label="Current task details"),
|
217 |
-
allow_flagging=False,
|
218 |
-
)
|
219 |
-
gr.Interface(
|
220 |
-
title="Test activation for all spaces once",
|
221 |
-
fn=activate,
|
222 |
-
inputs=[
|
223 |
-
gr.Textbox(label="HuggingFace", placeholder="Usernames joint by ;"),
|
224 |
-
gr.Textbox(label="ModelScope", placeholder="Usernames joint by ;"),
|
225 |
-
],
|
226 |
-
outputs=gr.Dataframe(label="Activated spaces"),
|
227 |
-
allow_flagging=False,
|
228 |
-
)
|
229 |
-
|
230 |
-
iface.launch()
|
|
|
1 |
+
import json
|
2 |
+
import time
|
3 |
+
import requests
|
4 |
+
import schedule
|
5 |
+
import gradio as gr
|
6 |
+
import pandas as pd
|
7 |
+
from tqdm import tqdm
|
8 |
+
from bs4 import BeautifulSoup
|
9 |
+
from urllib.parse import urlparse
|
10 |
+
|
11 |
+
TIMEOUT = 15
|
12 |
+
DELAY = 1
|
13 |
+
|
14 |
+
|
15 |
+
def get_studios(username: str):
|
16 |
+
# 请求负载
|
17 |
+
payload = {
|
18 |
+
"PageNumber": 1,
|
19 |
+
"PageSize": 1000,
|
20 |
+
"Name": "",
|
21 |
+
"SortBy": "gmt_modified",
|
22 |
+
"Order": "desc",
|
23 |
+
}
|
24 |
+
try:
|
25 |
+
# 发送PUT请求
|
26 |
+
response = requests.put(
|
27 |
+
f"https://www.modelscope.cn/api/v1/studios/{username}/list",
|
28 |
+
data=json.dumps(payload),
|
29 |
+
headers={
|
30 |
+
"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"
|
31 |
+
},
|
32 |
+
timeout=TIMEOUT,
|
33 |
+
)
|
34 |
+
# 检查请求是否成功
|
35 |
+
response.raise_for_status()
|
36 |
+
# 解析JSON响应
|
37 |
+
spaces: list = response.json()["Data"]["Studios"]
|
38 |
+
if spaces:
|
39 |
+
studios = []
|
40 |
+
for space in spaces:
|
41 |
+
studios.append(
|
42 |
+
f"https://www.modelscope.cn/studios/{username}/{space['Name']}"
|
43 |
+
)
|
44 |
+
|
45 |
+
return studios
|
46 |
+
|
47 |
+
except requests.exceptions.HTTPError as errh:
|
48 |
+
print(f"HTTP错误发生: {errh}")
|
49 |
+
except requests.exceptions.ConnectionError as errc:
|
50 |
+
print(f"连接错误发生: {errc}")
|
51 |
+
except requests.exceptions.Timeout as errt:
|
52 |
+
print(f"请求超时: {errt}, retrying...")
|
53 |
+
time.sleep(DELAY)
|
54 |
+
return get_studios(username)
|
55 |
+
|
56 |
+
except requests.exceptions.RequestException as err:
|
57 |
+
print(f"请求发生错误: {err}")
|
58 |
+
|
59 |
+
return []
|
60 |
+
|
61 |
+
|
62 |
+
def get_spaces(username: str):
|
63 |
+
try:
|
64 |
+
# 发送GET请求
|
65 |
+
response = requests.get(
|
66 |
+
"https://huggingface.co/spaces-json",
|
67 |
+
params={"sort": "trending", "search": username},
|
68 |
+
headers={
|
69 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537"
|
70 |
+
},
|
71 |
+
timeout=TIMEOUT,
|
72 |
+
)
|
73 |
+
# 检查请求是否成功
|
74 |
+
response.raise_for_status()
|
75 |
+
# 解析JSON响应
|
76 |
+
spaces: list = response.json()["spaces"]
|
77 |
+
studios = []
|
78 |
+
for space in spaces:
|
79 |
+
if space["author"] == username:
|
80 |
+
studios.append(f"https://huggingface.co/spaces/{space['id']}")
|
81 |
+
|
82 |
+
return studios
|
83 |
+
|
84 |
+
except requests.exceptions.HTTPError as errh:
|
85 |
+
print(f"HTTP错误发生: {errh}")
|
86 |
+
|
87 |
+
except requests.exceptions.Timeout as errt:
|
88 |
+
print(f"请求超时: {errt}, retrying...")
|
89 |
+
time.sleep(DELAY)
|
90 |
+
return get_spaces(username)
|
91 |
+
|
92 |
+
except requests.exceptions.RequestException as err:
|
93 |
+
print(f"请求发生错误: {err}")
|
94 |
+
|
95 |
+
return []
|
96 |
+
|
97 |
+
|
98 |
+
def activate_space(url: str):
|
99 |
+
success = "success"
|
100 |
+
try:
|
101 |
+
# 发送GET请求获取页面内容
|
102 |
+
response = requests.get(url, timeout=TIMEOUT)
|
103 |
+
response.raise_for_status() # 确保请求成功
|
104 |
+
web_page_content = response.text
|
105 |
+
|
106 |
+
# 使用BeautifulSoup解析页面
|
107 |
+
soup = BeautifulSoup(web_page_content, "html.parser")
|
108 |
+
|
109 |
+
# 寻找class为btn-lg的按钮
|
110 |
+
button = soup.find("button", class_="btn-lg")
|
111 |
+
if not button:
|
112 |
+
# print("未找到class为'btn-lg'的按钮。")
|
113 |
+
return success
|
114 |
+
|
115 |
+
# 获取表单的action属性和method属性
|
116 |
+
form = button.find_parent("form")
|
117 |
+
if not form:
|
118 |
+
return "按钮未在表单内找到。"
|
119 |
+
|
120 |
+
form_action = form.get("action")
|
121 |
+
form_method = form.get("method", "GET").upper() # 默认为GET
|
122 |
+
host = "https://" + urlparse(url).hostname
|
123 |
+
if not host in form_action:
|
124 |
+
form_action = host + form_action
|
125 |
+
|
126 |
+
# 提取表单数据
|
127 |
+
form_data = {
|
128 |
+
input_tag.get("name"): input_tag.get("value")
|
129 |
+
for input_tag in form.find_all("input")
|
130 |
+
if input_tag.has_attr("name")
|
131 |
+
}
|
132 |
+
|
133 |
+
# 发送请求提交表单
|
134 |
+
if form_method == "POST":
|
135 |
+
response = requests.post(form_action, data=form_data)
|
136 |
+
else:
|
137 |
+
response = requests.get(form_action, params=form_data)
|
138 |
+
|
139 |
+
# 打印响应内容
|
140 |
+
print("提交表单后的页面内容:")
|
141 |
+
print(response.text)
|
142 |
+
|
143 |
+
except requests.exceptions.Timeout as errt:
|
144 |
+
print(f"请求超时: {errt}, retrying...")
|
145 |
+
time.sleep(DELAY)
|
146 |
+
return activate_space(url)
|
147 |
+
|
148 |
+
except requests.RequestException as e:
|
149 |
+
success = f"{e}"
|
150 |
+
|
151 |
+
except Exception as e:
|
152 |
+
success = f"{e}"
|
153 |
+
|
154 |
+
return success
|
155 |
+
|
156 |
+
|
157 |
+
def activate(hf_users: str, ms_users: str):
|
158 |
+
hf_usernames = hf_users.split(";")
|
159 |
+
ms_usernames = ms_users.split(";")
|
160 |
+
spaces = []
|
161 |
+
for user in tqdm(hf_usernames, desc="Collecting spaces..."):
|
162 |
+
username = user.strip()
|
163 |
+
if username:
|
164 |
+
spaces += get_spaces(username)
|
165 |
+
time.sleep(DELAY)
|
166 |
+
|
167 |
+
for user in tqdm(ms_usernames, desc="Collecting studios..."):
|
168 |
+
username = user.strip()
|
169 |
+
if username:
|
170 |
+
spaces += get_studios(username)
|
171 |
+
time.sleep(DELAY)
|
172 |
+
|
173 |
+
output = []
|
174 |
+
for space in tqdm(spaces, desc="Activating spaces..."):
|
175 |
+
output.append({"space": space, "status": activate_space(space)})
|
176 |
+
time.sleep(DELAY)
|
177 |
+
|
178 |
+
print("Activation complete!")
|
179 |
+
return pd.DataFrame(output)
|
180 |
+
|
181 |
+
|
182 |
+
def monitor(hf_users: str, ms_users: str, period=1):
|
183 |
+
if schedule.get_jobs():
|
184 |
+
return
|
185 |
+
|
186 |
+
print(f"监控开启中...每日触发")
|
187 |
+
schedule.every(period).days.do(lambda: activate(hf_users, ms_users))
|
188 |
+
while True:
|
189 |
+
schedule.run_pending()
|
190 |
+
time.sleep(DELAY)
|
191 |
+
|
192 |
+
|
193 |
+
def list_tasks():
|
194 |
+
jobs = schedule.get_jobs()
|
195 |
+
if jobs:
|
196 |
+
return f"{jobs}".replace("[", "").replace("]", "")
|
197 |
+
|
198 |
+
return "None"
|
199 |
+
|
200 |
+
|
201 |
+
with gr.Blocks() as iface:
|
202 |
+
gr.Interface(
|
203 |
+
title="Start keeping all spaces active daily",
|
204 |
+
fn=monitor,
|
205 |
+
inputs=[
|
206 |
+
gr.Textbox(label="HuggingFace", placeholder="Usernames joint by ;"),
|
207 |
+
gr.Textbox(label="ModelScope", placeholder="Usernames joint by ;"),
|
208 |
+
],
|
209 |
+
outputs=None,
|
210 |
+
allow_flagging=False,
|
211 |
+
)
|
212 |
+
gr.Interface(
|
213 |
+
title="See current task status",
|
214 |
+
fn=list_tasks,
|
215 |
+
inputs=None,
|
216 |
+
outputs=gr.Textbox(label="Current task details"),
|
217 |
+
allow_flagging=False,
|
218 |
+
)
|
219 |
+
gr.Interface(
|
220 |
+
title="Test activation for all spaces once",
|
221 |
+
fn=activate,
|
222 |
+
inputs=[
|
223 |
+
gr.Textbox(label="HuggingFace", placeholder="Usernames joint by ;"),
|
224 |
+
gr.Textbox(label="ModelScope", placeholder="Usernames joint by ;"),
|
225 |
+
],
|
226 |
+
outputs=gr.Dataframe(label="Activated spaces"),
|
227 |
+
allow_flagging=False,
|
228 |
+
)
|
229 |
+
|
230 |
+
iface.launch()
|