Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Add `archive_model_in_azure` fn for deleted repositories (#1)
Browse files- Add `archive_model_in_azure` fn for deleted repositories (a0e77b2a3664491d50fb850d5c4d255e567735af)
- Add Azure-related requirements (fd0ba9751f257887fef0613fdc5cc571fa23825b)
Co-authored-by: Alvaro Bartolome <[email protected]>
- main.py +37 -0
- requirements.txt +4 -1
main.py
CHANGED
|
@@ -6,6 +6,9 @@ import json
|
|
| 6 |
import logging
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Set up logging
|
| 10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
logger = logging.getLogger(__name__)
|
|
@@ -18,6 +21,17 @@ CATALOG_WEBHOOKS_FLEET_DATASET = os.environ.get("CATALOG_WEBHOOKS_FLEET_DATASET"
|
|
| 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
|
|
@@ -122,6 +136,11 @@ def process_model_catalog_webhook(data: dict):
|
|
| 122 |
)
|
| 123 |
send_slack_message({"text": message, "type": "", "link": ""})
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# other events
|
| 126 |
else:
|
| 127 |
pass
|
|
@@ -167,6 +186,24 @@ def process_simship_webhook(data: dict):
|
|
| 167 |
else:
|
| 168 |
pass
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
app = FastAPI()
|
| 172 |
|
|
|
|
| 6 |
import logging
|
| 7 |
import time
|
| 8 |
|
| 9 |
+
from azure.ai.ml import MLClient
|
| 10 |
+
from azure.identity import DefaultAzureCredential
|
| 11 |
+
|
| 12 |
# Set up logging
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
logger = logging.getLogger(__name__)
|
|
|
|
| 21 |
SIMSHIP_WEBHOOK = os.environ.get("SIMSHIP_WEBHOOK")
|
| 22 |
SIMSHIP_WEBHOOK_SECRET = os.environ.get("SIMSHIP_WEBHOOK_SECRET")
|
| 23 |
|
| 24 |
+
# Initialize Azure ML Client
|
| 25 |
+
AZURE_SUBSCRIPTION_ID = os.getenv('AZURE_SUBSCRIPTION_ID')
|
| 26 |
+
AZURE_RESOURCE_GROUP = os.getenv('AZURE_RESOURCE_GROUP')
|
| 27 |
+
|
| 28 |
+
client = MLClient(
|
| 29 |
+
credential=DefaultAzureCredential(),
|
| 30 |
+
subscription_id=AZURE_SUBSCRIPTION_ID,
|
| 31 |
+
resource_group_name=AZURE_RESOURCE_GROUP,
|
| 32 |
+
registry_name="HuggingFace",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
def send_slack_message(payload: dict, max_retries: int = 3, timeout: int = 10, retry_delay: int = 2):
|
| 36 |
"""
|
| 37 |
Send a message to Slack using webhook URL with timeout and retry mechanism
|
|
|
|
| 136 |
)
|
| 137 |
send_slack_message({"text": message, "type": "", "link": ""})
|
| 138 |
|
| 139 |
+
try:
|
| 140 |
+
archive_model_in_azure(model_id=repo.get("name", ""))
|
| 141 |
+
except Exception as e:
|
| 142 |
+
logger.error(f"Model {repo.get('name','')} couldn't be archived with {e}")
|
| 143 |
+
|
| 144 |
# other events
|
| 145 |
else:
|
| 146 |
pass
|
|
|
|
| 186 |
else:
|
| 187 |
pass
|
| 188 |
|
| 189 |
+
def archive_model_in_azure(model_id: str) -> None:
|
| 190 |
+
name = model_id.replace("/", "-").replace("_", "-").lower()
|
| 191 |
+
models = list(client.models.list(name=name))
|
| 192 |
+
if len(models) < 1:
|
| 193 |
+
msg = f"Model {name} not available in Azure ML, then no need to delete anything."
|
| 194 |
+
logger.error(msg)
|
| 195 |
+
raise ValueError(msg)
|
| 196 |
+
|
| 197 |
+
for model in models:
|
| 198 |
+
try:
|
| 199 |
+
client.models.archive(name=model.name, version=model.version) # type: ignore
|
| 200 |
+
logger.info(f"Archived {model.name=} {model.version=}")
|
| 201 |
+
except Exception as e:
|
| 202 |
+
msg = f"Failed when archiving model with {name=} with exception {e}"
|
| 203 |
+
logger.error(msg)
|
| 204 |
+
raise RuntimeError(msg)
|
| 205 |
+
|
| 206 |
+
logger.info(f"Model {name} already archived on Azure ML!")
|
| 207 |
|
| 208 |
app = FastAPI()
|
| 209 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
requests
|
| 4 |
-
datasets
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
requests
|
| 4 |
+
datasets
|
| 5 |
+
azure-ai-ml
|
| 6 |
+
azureml
|
| 7 |
+
azureml-core
|