Upload gated_model_controller.py with huggingface_hub
Browse files- gated_model_controller.py +63 -0
gated_model_controller.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Manage Gated Access Requests for bbkdevops/Fiber-MoE-Symplectic-Gating-Research
|
| 3 |
+
Uses official huggingface_hub Gated Model API and HTTP Endpoints.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from huggingface_hub import HfApi, get_token
|
| 7 |
+
import requests
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
REPO_ID = "bbkdevops/Fiber-MoE-Symplectic-Gating-Research"
|
| 11 |
+
BASE_URL = f"https://huggingface.co/api/models/{REPO_ID}/user-access-request"
|
| 12 |
+
|
| 13 |
+
class GatedModelController:
|
| 14 |
+
def __init__(self, repo_id: str = REPO_ID):
|
| 15 |
+
self.repo_id = repo_id
|
| 16 |
+
self.token = get_token()
|
| 17 |
+
self.api = HfApi(token=self.token)
|
| 18 |
+
|
| 19 |
+
def _headers(self):
|
| 20 |
+
return {"Authorization": f"Bearer {self.token}"}
|
| 21 |
+
|
| 22 |
+
def list_pending(self):
|
| 23 |
+
"""Retrieve list of pending access requests."""
|
| 24 |
+
url = f"{BASE_URL}/pending"
|
| 25 |
+
res = requests.get(url, headers=self._headers())
|
| 26 |
+
if res.status_code == 200:
|
| 27 |
+
return res.json()
|
| 28 |
+
return {"status_code": res.status_code, "msg": res.text}
|
| 29 |
+
|
| 30 |
+
def list_accepted(self):
|
| 31 |
+
"""Retrieve list of accepted access requests."""
|
| 32 |
+
url = f"{BASE_URL}/accepted"
|
| 33 |
+
res = requests.get(url, headers=self._headers())
|
| 34 |
+
if res.status_code == 200:
|
| 35 |
+
return res.json()
|
| 36 |
+
return {"status_code": res.status_code, "msg": res.text}
|
| 37 |
+
|
| 38 |
+
def grant_user(self, username: str):
|
| 39 |
+
"""Directly grant a user access to the model."""
|
| 40 |
+
url = f"{BASE_URL}/grant"
|
| 41 |
+
payload = {"user": username}
|
| 42 |
+
res = requests.post(url, headers=self._headers(), json=payload)
|
| 43 |
+
return {"status_code": res.status_code, "response": res.text}
|
| 44 |
+
|
| 45 |
+
def handle_request(self, username: str, status: str = "accepted", reason: str = ""):
|
| 46 |
+
"""Handle request: status in ['accepted', 'rejected', 'pending', 'reset']."""
|
| 47 |
+
url = f"{BASE_URL}/handle"
|
| 48 |
+
payload = {"status": status, "user": username}
|
| 49 |
+
if reason:
|
| 50 |
+
if status == "rejected":
|
| 51 |
+
payload["rejectionReason"] = reason[:200]
|
| 52 |
+
elif status == "reset":
|
| 53 |
+
payload["resetReason"] = reason[:200]
|
| 54 |
+
res = requests.post(url, headers=self._headers(), json=payload)
|
| 55 |
+
return {"status_code": res.status_code, "response": res.text}
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
controller = GatedModelController()
|
| 59 |
+
print("Checking Gated Model Requests for:", REPO_ID)
|
| 60 |
+
pending = controller.list_pending()
|
| 61 |
+
print("Pending Requests:", json.dumps(pending, indent=2))
|
| 62 |
+
accepted = controller.list_accepted()
|
| 63 |
+
print("Accepted Requests:", json.dumps(accepted, indent=2))
|