Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
add variables
Browse files
main.py
CHANGED
|
@@ -18,7 +18,7 @@ MODEL_CATALOG_WEBHOOK_SECRET = os.environ.get("MODEL_CATALOG_WEBHOOK_SECRET")
|
|
| 18 |
SIMSHIP_WEBHOOK = os.environ.get("SIMSHIP_WEBHOOK")
|
| 19 |
SIMSHIP_WEBHOOK_SECRET = os.environ.get("SIMSHIP_WEBHOOK_SECRET")
|
| 20 |
|
| 21 |
-
def send_slack_message(
|
| 22 |
"""
|
| 23 |
Send a message to Slack using webhook URL with timeout and retry mechanism
|
| 24 |
|
|
@@ -29,11 +29,9 @@ def send_slack_message(message: str, max_retries: int = 3, timeout: int = 10, re
|
|
| 29 |
retry_delay: Delay between retries in seconds (default: 2)
|
| 30 |
"""
|
| 31 |
if not SLACK_WEBHOOK_URL:
|
| 32 |
-
logger.warning(f"No Slack webhook URL configured.
|
| 33 |
return
|
| 34 |
|
| 35 |
-
payload = {"text": message}
|
| 36 |
-
|
| 37 |
for attempt in range(max_retries + 1): # +1 for initial attempt
|
| 38 |
try:
|
| 39 |
logger.info(f"Sending Slack message (attempt {attempt + 1}/{max_retries + 1})")
|
|
@@ -43,7 +41,7 @@ def send_slack_message(message: str, max_retries: int = 3, timeout: int = 10, re
|
|
| 43 |
timeout=timeout
|
| 44 |
)
|
| 45 |
response.raise_for_status()
|
| 46 |
-
logger.info(f"Slack message sent successfully: {
|
| 47 |
return # Success, exit the function
|
| 48 |
|
| 49 |
except requests.exceptions.Timeout as e:
|
|
@@ -109,7 +107,7 @@ def process_model_catalog_webhook(data: dict):
|
|
| 109 |
f"Model in the catalog renamed π \n"
|
| 110 |
f"{repo.get('name', '')} β https://hf.co/{movedTo.get('name', '')}"
|
| 111 |
)
|
| 112 |
-
send_slack_message(message)
|
| 113 |
|
| 114 |
# repo deleted
|
| 115 |
elif (
|
|
@@ -122,7 +120,7 @@ def process_model_catalog_webhook(data: dict):
|
|
| 122 |
f"Model in the catalog deleted ποΈ\n"
|
| 123 |
f"https://hf.co/{repo.get('name', '')}"
|
| 124 |
)
|
| 125 |
-
send_slack_message(message)
|
| 126 |
|
| 127 |
# other events
|
| 128 |
else:
|
|
@@ -142,12 +140,13 @@ def process_simship_webhook(data: dict):
|
|
| 142 |
event.get("scope") == "repo" and
|
| 143 |
event.get("action") == "create"
|
| 144 |
):
|
|
|
|
| 145 |
message = (
|
| 146 |
"π₯οΈ SimShip Provider Alert π₯οΈ\n\n"
|
| 147 |
f"SimShip Model created π\n"
|
| 148 |
-
f"
|
| 149 |
)
|
| 150 |
-
send_slack_message(message)
|
| 151 |
|
| 152 |
# repo visibility update
|
| 153 |
elif (
|
|
@@ -156,12 +155,13 @@ def process_simship_webhook(data: dict):
|
|
| 156 |
event.get("action") == "update" and
|
| 157 |
updatedConfig.get("private") is False
|
| 158 |
):
|
|
|
|
| 159 |
message = (
|
| 160 |
"π₯οΈ SimShip Provider Alert π₯οΈ\n\n"
|
| 161 |
f"SimShip Model made public π\n"
|
| 162 |
-
f"
|
| 163 |
)
|
| 164 |
-
send_slack_message(message)
|
| 165 |
|
| 166 |
# other events
|
| 167 |
else:
|
|
|
|
| 18 |
SIMSHIP_WEBHOOK = os.environ.get("SIMSHIP_WEBHOOK")
|
| 19 |
SIMSHIP_WEBHOOK_SECRET = os.environ.get("SIMSHIP_WEBHOOK_SECRET")
|
| 20 |
|
| 21 |
+
def send_slack_message(payload: dict, max_retries: int = 3, timeout: int = 10, retry_delay: int = 2):
|
| 22 |
"""
|
| 23 |
Send a message to Slack using webhook URL with timeout and retry mechanism
|
| 24 |
|
|
|
|
| 29 |
retry_delay: Delay between retries in seconds (default: 2)
|
| 30 |
"""
|
| 31 |
if not SLACK_WEBHOOK_URL:
|
| 32 |
+
logger.warning(f"No Slack webhook URL configured. Payload: {payload}")
|
| 33 |
return
|
| 34 |
|
|
|
|
|
|
|
| 35 |
for attempt in range(max_retries + 1): # +1 for initial attempt
|
| 36 |
try:
|
| 37 |
logger.info(f"Sending Slack message (attempt {attempt + 1}/{max_retries + 1})")
|
|
|
|
| 41 |
timeout=timeout
|
| 42 |
)
|
| 43 |
response.raise_for_status()
|
| 44 |
+
logger.info(f"Slack message sent successfully: {payload}")
|
| 45 |
return # Success, exit the function
|
| 46 |
|
| 47 |
except requests.exceptions.Timeout as e:
|
|
|
|
| 107 |
f"Model in the catalog renamed π \n"
|
| 108 |
f"{repo.get('name', '')} β https://hf.co/{movedTo.get('name', '')}"
|
| 109 |
)
|
| 110 |
+
send_slack_message({"text": message, "type": "", "link": ""})
|
| 111 |
|
| 112 |
# repo deleted
|
| 113 |
elif (
|
|
|
|
| 120 |
f"Model in the catalog deleted ποΈ\n"
|
| 121 |
f"https://hf.co/{repo.get('name', '')}"
|
| 122 |
)
|
| 123 |
+
send_slack_message({"text": message, "type": "", "link": ""})
|
| 124 |
|
| 125 |
# other events
|
| 126 |
else:
|
|
|
|
| 140 |
event.get("scope") == "repo" and
|
| 141 |
event.get("action") == "create"
|
| 142 |
):
|
| 143 |
+
link = f"https://hf.co/{repo.get('name', '')}"
|
| 144 |
message = (
|
| 145 |
"π₯οΈ SimShip Provider Alert π₯οΈ\n\n"
|
| 146 |
f"SimShip Model created π\n"
|
| 147 |
+
f"{link}"
|
| 148 |
)
|
| 149 |
+
send_slack_message({"text": message, "link": link, "type": "simship"})
|
| 150 |
|
| 151 |
# repo visibility update
|
| 152 |
elif (
|
|
|
|
| 155 |
event.get("action") == "update" and
|
| 156 |
updatedConfig.get("private") is False
|
| 157 |
):
|
| 158 |
+
link = f"https://hf.co/{repo.get('name', '')}"
|
| 159 |
message = (
|
| 160 |
"π₯οΈ SimShip Provider Alert π₯οΈ\n\n"
|
| 161 |
f"SimShip Model made public π\n"
|
| 162 |
+
f"{link}"
|
| 163 |
)
|
| 164 |
+
send_slack_message({"text": message, "link": link, "type": "simship"})
|
| 165 |
|
| 166 |
# other events
|
| 167 |
else:
|