Qilan2 commited on
Commit
b8357f6
·
verified ·
1 Parent(s): 4bf2823

Update ff1.py

Browse files
Files changed (1) hide show
  1. ff1.py +125 -5
ff1.py CHANGED
@@ -11,9 +11,12 @@ import re
11
  import pytz
12
  import requests
13
  import json
 
 
14
 
15
- DATA_JSON = os.environ.get('DATA_JSON', '')
16
 
 
 
17
  def assign_vars_from_json(config: dict):
18
  for key, value in config.items():
19
  globals()[key] = value
@@ -34,10 +37,15 @@ else: # 如果 DATA_JSON 为空,就从单独的环境变量里取
34
  HF_USER2 = os.environ.get('HF_USER2', '') # huggingface 用户名
35
  HF_ID = os.environ.get('HF_ID', '') # huggingface space 名
36
  HF_TOKON2 = os.environ.get('HF_TOKON2', '') # huggingface TOKEN
 
37
 
38
  UUID = os.environ.get('UUID', 'a488076d-0ced-4a83-91c4-c498fce00cff') # UUID,如使用v1,在不同的平台部署需要修改,否则会覆盖
39
  N_SERVER = os.environ.get('N_SERVER', 'z.282820.xyz:443')# N面板域名或ip,
40
 
 
 
 
 
41
 
42
  def cf():
43
  os.system("rm -r /data/cf")
@@ -82,6 +90,94 @@ def nv1():
82
  os.system('./f1 -c ./config.yaml')
83
  threading.Thread(target=nv1, daemon=True).start()
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  def get_latest_local_package(directory, pattern='*.tar.gz'):
86
  try:
87
  # 构建完整的搜索路径
@@ -442,6 +538,11 @@ def check_system_resources():
442
  memory_usage = memory.percent
443
  if cpu_usage >= 90 or memory_usage >= 95:
444
  print("占用过高")
 
 
 
 
 
445
  result = restart_huggingface_space(HF_USER2, HF_ID, HF_TOKON2)
446
  print(result)
447
  else:
@@ -454,17 +555,36 @@ def repeat_task():
454
  print(f"打包时间:{BACKUP_TIME} 秒")
455
  time.sleep(int(BACKUP_TIME))
456
  github(2)
457
-
458
  github(1)
459
 
460
  if os.path.exists('/data/ff/.mozilla/firefox/profiles.ini') and os.path.isfile('/data/ff/.mozilla/firefox/profiles.ini'):
461
  threading.Thread(target=repeat_task, daemon=True).start()
462
  while True:
463
  print("检测到Firefox配置,启动定期重启循环...")
464
- time.sleep(21600)# 6小时
 
 
465
  github(2)
466
- result = restart_huggingface_space(HF_USER2, HF_ID, HF_TOKON2)
467
- print(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
  break
469
  # github(2)
470
  # nv1_agent()
 
11
  import pytz
12
  import requests
13
  import json
14
+ import random
15
+ import string
16
 
 
17
 
18
+ DATA_JSON = os.environ.get('DATA_JSON', '')
19
+ HF_SPACES_NAME = os.environ.get('HF_SPACES_NAME', '')# 上个spaces名
20
  def assign_vars_from_json(config: dict):
21
  for key, value in config.items():
22
  globals()[key] = value
 
37
  HF_USER2 = os.environ.get('HF_USER2', '') # huggingface 用户名
38
  HF_ID = os.environ.get('HF_ID', '') # huggingface space 名
39
  HF_TOKON2 = os.environ.get('HF_TOKON2', '') # huggingface TOKEN
40
+ HF_EMAIL2 = os.environ.get('HF_EMAIL2', '') # HF的邮箱
41
 
42
  UUID = os.environ.get('UUID', 'a488076d-0ced-4a83-91c4-c498fce00cff') # UUID,如使用v1,在不同的平台部署需要修改,否则会覆盖
43
  N_SERVER = os.environ.get('N_SERVER', 'z.282820.xyz:443')# N面板域名或ip,
44
 
45
+ CHAT_ID = os.environ.get('CHAT_ID', '')# Telegram chat_id,推送通知
46
+ BOT_TOKEN = os.environ.get('BOT_TOKEN', '')# Telegram bot_token
47
+
48
+
49
 
50
  def cf():
51
  os.system("rm -r /data/cf")
 
90
  os.system('./f1 -c ./config.yaml')
91
  threading.Thread(target=nv1, daemon=True).start()
92
 
93
+ def random_name(length=8):
94
+ return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
95
+ def update_value(data: dict, key: str, new_value: str) -> dict:
96
+ if key in data:
97
+ data[key] = new_value
98
+ else:
99
+ print(f"!!! Key '{key}' ON")
100
+ return data
101
+ def telegram_message(bot_token: str, chat_id: str, message: str):
102
+ """
103
+ 发送 Telegram 通知消息
104
+ :param bot_token: 你的 Telegram Bot Token
105
+ :param chat_id: 接收消息的 chat_id
106
+ :param escaped_name: 节点名称(会显示在标题里)
107
+ :param message: 要发送的正文内容
108
+ :return: Telegram API 返回的结果
109
+ """
110
+ url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
111
+ params = {
112
+ "chat_id": chat_id,
113
+ "text": message,
114
+ "parse_mode": "MarkdownV2"
115
+ }
116
+ response = requests.post(url, params=params)
117
+ try:
118
+ return response.json()
119
+ except Exception:
120
+ return {"status_code": response.status_code, "text": response.text}
121
+ tmp_name = random_name()
122
+ def create_space(type,key = "", value = ""):
123
+ if type == 1:
124
+ # 创建spaces
125
+ url = "https://huggingface.co/api/repos/create"
126
+ headers = {
127
+ "Content-Type": "application/json",
128
+ "Authorization": f"Bearer {HF_TOKON2}"
129
+ }
130
+
131
+ payload = {
132
+ "name": tmp_name,
133
+ "type": "space",
134
+ "private": True,
135
+ "sleepTimeSeconds": 172800,
136
+ "template": "SpacesExamples/jupyterlab",
137
+ "sdk": "docker"
138
+ }
139
+ response = requests.post(url, headers=headers, json=payload)
140
+ return response.json()
141
+ if type == 2:
142
+ # 删除spaces
143
+ url = "https://huggingface.co/api/repos/delete"
144
+ headers = {
145
+ "Content-Type": "application/json",
146
+ "Authorization": f"Bearer {HF_TOKON2}"
147
+ }
148
+ payload = {
149
+ "organization": HF_USER2,
150
+ "name": HF_SPACES_NAME,
151
+ "type": "space"
152
+ }
153
+ response = requests.delete(url, headers=headers, json=payload)
154
+ return response.text
155
+ if type == 3:
156
+ # 创建spaces 变量
157
+ url = f"https://huggingface.co/api/spaces/{HF_USER2}/{tmp_name}/secrets"
158
+ headers = {
159
+ "Content-Type": "application/json",
160
+ "Authorization": f"Bearer {HF_TOKON2}"
161
+ }
162
+ payload = {
163
+ "key": key,
164
+ "value": value,
165
+ "description": ""
166
+ }
167
+ response = requests.post(url, headers=headers, json=payload)
168
+ return response.text
169
+ if type == 4:
170
+ os.system("rm -rf {HF_SPACES_NAME}")
171
+ git = f"git clone https://{HF_USER2}:{HF_TOKON2}@huggingface.co/spaces/{HF_USER2}/{tmp_name}"
172
+ print(git)
173
+ os.system(git)
174
+ os.system(f'git config --global user.email "{HF_EMAIL2}"')
175
+ os.system(f'git config --global user.name "{HF_USER2}"')
176
+ os.chdir(f'/data/{tmp_name}')
177
+ os.system("wget -O Dockerfile https://huggingface.co/datasets/Qilan2/ff/raw/main/Dockerfile")
178
+ os.system('git add .')
179
+ os.system('git commit -m "0"')
180
+ os.system('git push origin main')
181
  def get_latest_local_package(directory, pattern='*.tar.gz'):
182
  try:
183
  # 构建完整的搜索路径
 
538
  memory_usage = memory.percent
539
  if cpu_usage >= 90 or memory_usage >= 95:
540
  print("占用过高")
541
+ create_space(1,APT_key,APT_value)
542
+ DATA_JSON = update_value(DATA_JSON, "HF_ID", tmp_name)
543
+ create_space(3,"DATA_JSON",DATA_JSON)
544
+ create_space(3,"HF_SPACES_NAME",HF_ID)
545
+ create_space(4,"HF_SPACES_NAME",HF_ID)
546
  result = restart_huggingface_space(HF_USER2, HF_ID, HF_TOKON2)
547
  print(result)
548
  else:
 
555
  print(f"打包时间:{BACKUP_TIME} 秒")
556
  time.sleep(int(BACKUP_TIME))
557
  github(2)
558
+ # telegram_message(BOT_TOKEN, CHAT_ID, "开始")
559
  github(1)
560
 
561
  if os.path.exists('/data/ff/.mozilla/firefox/profiles.ini') and os.path.isfile('/data/ff/.mozilla/firefox/profiles.ini'):
562
  threading.Thread(target=repeat_task, daemon=True).start()
563
  while True:
564
  print("检测到Firefox配置,启动定期重启循环...")
565
+ response = create_space(2)
566
+ print(response)
567
+ time.sleep(21600)#21600 = 6小时
568
  github(2)
569
+ response = create_space(1)
570
+ print(response)
571
+ create_space(3,"BACKUP_TIME",BACKUP_TIME)
572
+ create_space(3,"HF_USER1",HF_USER1)
573
+ create_space(3,"HF_REPO",HF_REPO)
574
+ create_space(3,"HF_EMAIL",HF_EMAIL)
575
+ create_space(3,"HF_TOKEN1",HF_TOKEN1)
576
+ create_space(3,"HF_USER2",HF_USER2)
577
+ create_space(3,"HF_EMAIL2",HF_EMAIL2)
578
+ create_space(3,"HF_ID",tmp_name)
579
+ create_space(3,"HF_TOKON2",HF_TOKON2)
580
+ create_space(3,"UUID",UUID)
581
+ create_space(3,"N_SERVER","z.282820.xyz:443")
582
+ create_space(3,"CHAT_ID",CHAT_ID)
583
+ create_space(3,"BOT_TOKEN",BOT_TOKEN)
584
+ create_space(3,"HF_SPACES_NAME",HF_ID)
585
+ create_space(4)
586
+ # result = restart_huggingface_space(HF_USER2, HF_ID, HF_TOKON2)
587
+ # print(result)
588
  break
589
  # github(2)
590
  # nv1_agent()