Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update main.py
Browse files
main.py
CHANGED
|
@@ -8,9 +8,15 @@ import logging
|
|
| 8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
-
|
| 12 |
SLACK_WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def send_slack_message(message: str):
|
| 15 |
"""Send a message to Slack using webhook URL"""
|
| 16 |
if not SLACK_WEBHOOK_URL:
|
|
@@ -25,41 +31,92 @@ def send_slack_message(message: str):
|
|
| 25 |
except requests.exceptions.RequestException as e:
|
| 26 |
logger.error(f"Failed to send Slack message: {e}")
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
app = FastAPI()
|
| 29 |
|
| 30 |
@app.post("/webhook")
|
| 31 |
async def webhook(request: Request):
|
| 32 |
logger.info(f"Received webhook request from {request.client.host}")
|
| 33 |
|
| 34 |
-
if request.headers.get("X-Webhook-Secret")
|
| 35 |
logger.warning("Invalid webhook secret received")
|
| 36 |
return Response("Invalid secret", status_code=401)
|
| 37 |
|
| 38 |
data = await request.json()
|
| 39 |
logger.info(f"Webhook payload: {json.dumps(data, indent=2)}")
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return Response("Not a model repo event", status_code=200)
|
| 48 |
-
|
| 49 |
-
action = event.get("action")
|
| 50 |
-
logger.info(f"Processing model repo action: {action}")
|
| 51 |
-
|
| 52 |
-
if action == "move":
|
| 53 |
-
old_name = repo.get("name", "unknown")
|
| 54 |
-
new_name = data.get("movedTo", {}).get("name", "unknown")
|
| 55 |
-
message = f"π Model renamed: {old_name} β https://hf.co/{new_name}"
|
| 56 |
-
send_slack_message(message)
|
| 57 |
-
elif action == "delete":
|
| 58 |
-
name = repo.get("name", "unknown")
|
| 59 |
-
message = f"ποΈ Model deleted: https://hf.co/{name}"
|
| 60 |
-
send_slack_message(message)
|
| 61 |
else:
|
| 62 |
-
logger.
|
|
|
|
| 63 |
|
| 64 |
return Response("Webhook notification received and processed!", status_code=200)
|
| 65 |
-
|
|
|
|
| 8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
+
# Configurations
|
| 12 |
SLACK_WEBHOOK_URL = os.environ.get("SLACK_WEBHOOK_URL")
|
| 13 |
|
| 14 |
+
MODEL_CATALOG_WEBHOOK = os.environ.get("MODEL_CATALOG_WEBHOOK")
|
| 15 |
+
MODEL_CATALOG_WEBHOOK_SECRET = os.environ.get("MODEL_CATALOG_WEBHOOK_SECRET")
|
| 16 |
+
|
| 17 |
+
SIMSHIP_WEBHOOK = os.environ.get("SIMSHIP_WEBHOOK")
|
| 18 |
+
SIMSHIP_WEBHOOK_SECRET = os.environ.get("SIMSHIP_WEBHOOK_SECRET")
|
| 19 |
+
|
| 20 |
def send_slack_message(message: str):
|
| 21 |
"""Send a message to Slack using webhook URL"""
|
| 22 |
if not SLACK_WEBHOOK_URL:
|
|
|
|
| 31 |
except requests.exceptions.RequestException as e:
|
| 32 |
logger.error(f"Failed to send Slack message: {e}")
|
| 33 |
|
| 34 |
+
def process_model_catalog_webhook(data: dict):
|
| 35 |
+
"""
|
| 36 |
+
Process model catalog webhook
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
event = data.get("event", {})
|
| 40 |
+
repo = data.get("repo", {})
|
| 41 |
+
movedTo = data.get("movedTo", {})
|
| 42 |
+
|
| 43 |
+
# repo name changed
|
| 44 |
+
if (
|
| 45 |
+
repo.get("type") == "model" and
|
| 46 |
+
event.get("scope") == "repo" and
|
| 47 |
+
event.get("action") == "move"
|
| 48 |
+
):
|
| 49 |
+
message = f"π Model in the catalog renamed: {repo.get('name', '')} β https://hf.co/{movedTo.get('name', '')}"
|
| 50 |
+
send_slack_message(message)
|
| 51 |
+
|
| 52 |
+
# repo deleted
|
| 53 |
+
elif (
|
| 54 |
+
repo.get("type") == "model" and
|
| 55 |
+
event.get("scope") == "repo" and
|
| 56 |
+
event.get("action") == "delete"
|
| 57 |
+
):
|
| 58 |
+
message = f"ποΈ Model in the catalog deleted: https://hf.co/{repo.get('name', '')}"
|
| 59 |
+
send_slack_message(message)
|
| 60 |
+
|
| 61 |
+
# other events
|
| 62 |
+
else:
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
def process_simship_webhook(data: dict):
|
| 66 |
+
"""
|
| 67 |
+
Process simship webhook
|
| 68 |
+
"""
|
| 69 |
+
event = data.get("event", {})
|
| 70 |
+
repo = data.get("repo", {})
|
| 71 |
+
updatedConfig = data.get("updatedConfig", {})
|
| 72 |
+
|
| 73 |
+
# repo creation
|
| 74 |
+
if (
|
| 75 |
+
repo.get("type") == "model" and
|
| 76 |
+
event.get("scope") == "repo" and
|
| 77 |
+
event.get("action") == "create"
|
| 78 |
+
):
|
| 79 |
+
message = f"π SimShip Model created: https://hf.co/{repo.get('name', '')}"
|
| 80 |
+
send_slack_message(message)
|
| 81 |
+
|
| 82 |
+
# repo visibility update
|
| 83 |
+
elif (
|
| 84 |
+
repo.get("type") == "model" and
|
| 85 |
+
event.get("scope") == "repo.config" and
|
| 86 |
+
event.get("action") == "update" and
|
| 87 |
+
updatedConfig.get("private") is False
|
| 88 |
+
):
|
| 89 |
+
message = f"π SimShip Model made public: https://hf.co/{repo.get('name', '')}"
|
| 90 |
+
send_slack_message(message)
|
| 91 |
+
|
| 92 |
+
# other events
|
| 93 |
+
else:
|
| 94 |
+
pass
|
| 95 |
+
|
| 96 |
+
|
| 97 |
app = FastAPI()
|
| 98 |
|
| 99 |
@app.post("/webhook")
|
| 100 |
async def webhook(request: Request):
|
| 101 |
logger.info(f"Received webhook request from {request.client.host}")
|
| 102 |
|
| 103 |
+
if request.headers.get("X-Webhook-Secret") not in {MODEL_CATALOG_WEBHOOK_SECRET, SIMSHIP_WEBHOOK_SECRET}:
|
| 104 |
logger.warning("Invalid webhook secret received")
|
| 105 |
return Response("Invalid secret", status_code=401)
|
| 106 |
|
| 107 |
data = await request.json()
|
| 108 |
logger.info(f"Webhook payload: {json.dumps(data, indent=2)}")
|
| 109 |
+
|
| 110 |
+
webhook_id = data.get("webhook", {}).get("id", None)
|
| 111 |
|
| 112 |
+
if webhook_id == MODEL_CATALOG_WEBHOOK:
|
| 113 |
+
logger.info("Processing model catalog webhook")
|
| 114 |
+
process_model_catalog_webhook(data)
|
| 115 |
+
elif webhook_id == SIMSHIP_WEBHOOK:
|
| 116 |
+
logger.info("Processing simship webhook")
|
| 117 |
+
process_simship_webhook(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
else:
|
| 119 |
+
logger.warning("Invalid webhook ID received")
|
| 120 |
+
return Response("Invalid webhook ID", status_code=401)
|
| 121 |
|
| 122 |
return Response("Webhook notification received and processed!", status_code=200)
|
|
|