Spaces:
Running
Running
File size: 5,717 Bytes
751f3b1 a98d805 751f3b1 a98d805 dac0f46 42452f8 751f3b1 ef9fd49 751f3b1 bda5f56 34c3eb6 751f3b1 f5fc8a7 751f3b1 738c081 751f3b1 42452f8 751f3b1 42452f8 751f3b1 42452f8 751f3b1 42452f8 751f3b1 b50dddd 751f3b1 b50dddd de43d1a 751f3b1 738c081 de43d1a 751f3b1 b50dddd de43d1a b50dddd de43d1a b50dddd de43d1a 751f3b1 dac0f46 de43d1a b50dddd |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
import json
import time
import requests
import threading
from tqdm import tqdm
from datetime import datetime
from huggingface_hub import HfApi
from modules.smtp import send_email
from modules.times import fix_datetime
from modules.restart import restart_repos
from utils import HF_DOMAIN, HEADER, MS_HEADER, TIMEOUT, MS_DOMAIN, DELAY, USERS
def get_space_status(repo_id: str):
response: list = requests.get(
url=f"{HF_DOMAIN}/api/spaces/semantic-search",
params={"q": repo_id, "includeNonRunning": "true"},
headers=HEADER,
timeout=TIMEOUT,
).json()
if response:
for repo in response:
if repo["id"] == repo_id:
return repo["runtime"]["stage"]
return "ERROR"
def get_spaces(username: str):
sleepings, errors = [], []
try:
spaces = HfApi().list_spaces(author=username)
for space in spaces:
status = get_space_status(space.id)
if status == "SLEEPING":
space_id = space.id.replace("/", "-").replace("_", "-").lower()
if space.sdk == "gradio":
sleepings.append(f"https://{space_id}.hf.space")
else:
sleepings.append(f"https://{space_id}.static.hf.space")
elif "ERROR" in status:
errors.append(f"{HF_DOMAIN}/spaces/{space.id}")
return sleepings, errors
except Exception as e:
print(f"An error occurred in the request: {e}")
return [], []
def activate_space(url: str):
try:
response = requests.get(url, headers=HEADER, timeout=TIMEOUT)
response.raise_for_status()
except Exception as e:
print(e)
def check_ms_login():
try:
response = requests.get(
"https://www.modelscope.cn/api/v1/users/login/info",
headers=MS_HEADER,
timeout=TIMEOUT,
)
response.raise_for_status()
except Exception as e:
send_email(f"ModelScope cookie 失效: {e}")
def get_studios(username: str):
try:
response = requests.put(
f"{MS_DOMAIN}/api/v1/studios/{username}/list",
data=json.dumps(
{
"PageNumber": 1,
"PageSize": 1000,
"Name": "",
"SortBy": "gmt_modified",
"Order": "desc",
}
),
headers=MS_HEADER,
timeout=TIMEOUT,
)
response.raise_for_status()
spaces: list = response.json()["Data"]["Studios"]
if spaces:
studios, errors = [], []
for space in spaces:
status = space["Status"]
if status == "Expired":
studios.append(f"{username}/{space['Name']}")
elif status == "Failed":
errors.append(f"{MS_DOMAIN}/studios/{username}/{space['Name']}")
return studios, errors
except requests.exceptions.Timeout as e:
print(f"Timeout: {e}, retrying...")
time.sleep(DELAY)
return get_studios(username)
except Exception as e:
print(f"Requesting error: {e}")
return [], []
def activate_studio(repo: str, holding_delay=5):
repo_page = f"{MS_DOMAIN}/studios/{repo}"
status_api = f"{MS_DOMAIN}/api/v1/studio/{repo}/status"
start_expired_api = f"{MS_DOMAIN}/api/v1/studio/{repo}/start_expired"
try:
response = requests.put(start_expired_api, headers=MS_HEADER, timeout=TIMEOUT)
response.raise_for_status()
while (
requests.get(status_api, headers=MS_HEADER, timeout=TIMEOUT).json()["Data"][
"Status"
]
!= "Running"
):
requests.get(repo_page, headers=MS_HEADER, timeout=TIMEOUT)
time.sleep(holding_delay)
except requests.exceptions.Timeout as e:
print(f"Failed to activate {repo}: {e}, retrying...")
activate_studio(repo)
except Exception as e:
print(e)
# UI func
def trigger(users=USERS):
status = "Success"
logs = None
try:
spaces, studios, failures = [], [], []
usernames = users.split(";")
for user in tqdm(usernames, desc="Collecting spaces"):
username = user.strip()
if username:
sleeps, errors = get_spaces(username)
spaces += sleeps
failures += errors
time.sleep(DELAY)
for space in tqdm(spaces, desc="Activating spaces"):
activate_space(space)
time.sleep(DELAY)
check_ms_login()
for user in tqdm(usernames, desc="Collecting studios"):
username = user.strip()
if username:
sleeps, errors = get_studios(username)
studios += sleeps
failures += errors
time.sleep(DELAY)
for studio in tqdm(studios, desc="Activating studios"):
threading.Thread(
target=activate_studio, args=(studio,), daemon=True
).start()
time.sleep(DELAY)
logs = (
"\n".join(spaces + studios)
+ f"\n[{fix_datetime(datetime.now())}] Activation complete!\n"
)
print(logs)
content = ""
for failure in failures:
errepo: str = failure
errepo = errepo.replace(HF_DOMAIN, "").replace(MS_DOMAIN, "")
content += f"<br><a href='{failure}'>{errepo[1:]}</a><br>"
if content:
send_email(content)
restart_repos()
except Exception as e:
status = f"{e}"
return status, logs
|