admin commited on
Commit
a98d805
Β·
1 Parent(s): bda5f56

decouple modules

Browse files
app.py CHANGED
@@ -2,10 +2,12 @@ import time
2
  import schedule
3
  import threading
4
  import gradio as gr
5
- from activate import trigger
6
- from restart import restart
7
- from config import *
8
- from times import *
 
 
9
 
10
 
11
  def run_schedule():
@@ -19,12 +21,12 @@ def run_schedule():
19
 
20
  def monitor(period=PERIOD):
21
  print(f"Monitor is on and triggered every {period}h...")
22
- schedule.every(47).hours.do(restart)
23
  schedule.every(int(period)).hours.do(trigger)
24
  threading.Thread(target=run_schedule, daemon=True).start()
25
 
26
 
27
- def tasklist():
28
  try:
29
  outputs = f"Has been running for {time_diff(START_TIME, datetime.now())}"
30
  jobs = schedule.get_jobs()
@@ -49,7 +51,7 @@ if __name__ == "__main__":
49
  with gr.Column():
50
  gr.Interface(
51
  title="See current task status",
52
- fn=tasklist,
53
  inputs=None,
54
  outputs=gr.TextArea(label="Current task details"),
55
  flagging_mode="never",
@@ -64,4 +66,25 @@ if __name__ == "__main__":
64
  flagging_mode="never",
65
  )
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  demo.launch()
 
2
  import schedule
3
  import threading
4
  import gradio as gr
5
+ from datetime import datetime
6
+ from modules.smtp import send_email
7
+ from modules.activate import trigger
8
+ from modules.times import fix_datetime, time_diff
9
+ from modules.restart import self_restart, test_restart
10
+ from utils import START_TIME, DELAY, PERIOD
11
 
12
 
13
  def run_schedule():
 
21
 
22
  def monitor(period=PERIOD):
23
  print(f"Monitor is on and triggered every {period}h...")
24
+ schedule.every(47).hours.do(self_restart)
25
  schedule.every(int(period)).hours.do(trigger)
26
  threading.Thread(target=run_schedule, daemon=True).start()
27
 
28
 
29
+ def tasklst():
30
  try:
31
  outputs = f"Has been running for {time_diff(START_TIME, datetime.now())}"
32
  jobs = schedule.get_jobs()
 
51
  with gr.Column():
52
  gr.Interface(
53
  title="See current task status",
54
+ fn=tasklst,
55
  inputs=None,
56
  outputs=gr.TextArea(label="Current task details"),
57
  flagging_mode="never",
 
66
  flagging_mode="never",
67
  )
68
 
69
+ with gr.Column():
70
+ gr.Interface(
71
+ title="Cookie test",
72
+ fn=test_restart,
73
+ inputs=gr.Textbox(label="Restart a space with permissions"),
74
+ outputs=[
75
+ gr.Textbox(label="Status"),
76
+ gr.Markdown(container=True, show_label=True, label="Shortcut"),
77
+ ],
78
+ flagging_mode="never",
79
+ )
80
+
81
+ with gr.Column():
82
+ gr.Interface(
83
+ title="SMTP test",
84
+ fn=send_email,
85
+ inputs=None,
86
+ outputs=gr.Textbox(label="Status"),
87
+ flagging_mode="never",
88
+ )
89
+
90
  demo.launch()
activate.py β†’ modules/activate.py RENAMED
@@ -3,10 +3,11 @@ import time
3
  import requests
4
  import threading
5
  from tqdm import tqdm
 
6
  from huggingface_hub import HfApi
7
- from smtp import send_email
8
- from config import *
9
- from times import *
10
 
11
 
12
  def get_space_status(repo_id: str):
 
3
  import requests
4
  import threading
5
  from tqdm import tqdm
6
+ from datetime import datetime
7
  from huggingface_hub import HfApi
8
+ from modules.smtp import send_email
9
+ from modules.times import fix_datetime
10
+ from utils import HF_DOMAIN, HEADER, TIMEOUT, MS_DOMAIN, DELAY, USERS
11
 
12
 
13
  def get_space_status(repo_id: str):
modules/restart.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from modules.smtp import send_email
3
+ from utils import HF_DOMAIN, HEADER, TIMEOUT, COOKIE
4
+
5
+
6
+ def restart_private_space(repo: str):
7
+ response = requests.post(
8
+ f"{HF_DOMAIN}/api/spaces/{repo}/restart",
9
+ headers={
10
+ "User-Agent": HEADER["User-Agent"],
11
+ "cookie": COOKIE,
12
+ },
13
+ timeout=TIMEOUT,
14
+ )
15
+ response.raise_for_status()
16
+
17
+
18
+ def self_restart():
19
+ try:
20
+ restart_private_space("kakamond/keep_spaces_alive")
21
+
22
+ except Exception as e:
23
+ send_email(f"Failed to self-restart: {e}")
24
+
25
+
26
+ def test_restart(repo):
27
+ status = "Success"
28
+ url = None
29
+ try:
30
+ restart_private_space(repo)
31
+ url = f"{HF_DOMAIN}/spaces/{repo}"
32
+
33
+ except Exception as e:
34
+ status = f"{e}"
35
+
36
+ return status, url
smtp.py β†’ modules/smtp.py RENAMED
@@ -1,9 +1,9 @@
1
  import requests
2
- from config import *
3
 
4
 
5
- def send_email(
6
- content,
7
  title="Runtime error detected",
8
  header="Please fix following repo(s):",
9
  email=EMAIL,
@@ -29,16 +29,29 @@ def send_email(
29
  "email": email, # from
30
  "mail": target, # to
31
  "title": title, # subject
32
- "name": "ksa", # nickname
33
  "text": body, # content
34
  },
35
  )
36
  if response.status_code == 200:
37
  result: dict = response.json()
38
  if result.get("status") == "success":
39
- print("Email sent successfully!")
40
  else:
41
- print(f"Failed to send email: {result.get('message')}")
42
 
43
  else:
44
- print(f"Request failed with status code: {response.status_code}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import requests
2
+ from utils import EMAIL, SMTP, TAG, HOST, PORT, API
3
 
4
 
5
+ def email_api(
6
+ content: str,
7
  title="Runtime error detected",
8
  header="Please fix following repo(s):",
9
  email=EMAIL,
 
29
  "email": email, # from
30
  "mail": target, # to
31
  "title": title, # subject
32
+ "name": "Keep Spaces Alive", # nickname
33
  "text": body, # content
34
  },
35
  )
36
  if response.status_code == 200:
37
  result: dict = response.json()
38
  if result.get("status") == "success":
39
+ return "Email sent successfully!"
40
  else:
41
+ raise ConnectionError(f"Failed to send email: {result.get('message')}")
42
 
43
  else:
44
+ raise ConnectionError(
45
+ f"Request failed with status code: {response.status_code}"
46
+ )
47
+
48
+
49
+ def send_email(content="Test SMTP"):
50
+ status = "Success"
51
+ try:
52
+ status = email_api(content)
53
+
54
+ except Exception as e:
55
+ status = f"{e}"
56
+
57
+ return status
times.py β†’ modules/times.py RENAMED
File without changes
restart.py DELETED
@@ -1,23 +0,0 @@
1
- import requests
2
- from smtp import send_email
3
- from config import *
4
-
5
-
6
- def restart_private_space(repo: str, cookie: str):
7
- try:
8
- response = requests.post(
9
- f"{HF_DOMAIN}/api/spaces/{repo}/restart",
10
- headers={
11
- "User-Agent": HEADER["User-Agent"],
12
- "cookie": cookie,
13
- },
14
- timeout=TIMEOUT,
15
- )
16
- response.raise_for_status()
17
-
18
- except Exception as e:
19
- send_email(f"{e}", "Restart failure", f"Failed to restart {repo}")
20
-
21
-
22
- def restart():
23
- restart_private_space("kakamond/keep_spaces_alive", COOKIE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
config.py β†’ utils.py RENAMED
File without changes