File size: 5,451 Bytes
0c221a9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# I need to print some stuff out so I know what I'm dealing with here
# Go ahead, run the file. I DARE ya!
import requests
import json
from environs import Env
# Initialize environment
env = Env()
env.read_env()
HF_TOKEN = env("HF_TOKEN")
ORG_NAME = env("ORG_NAME")
BASE_URL = "https://huggingface.co/api"
headers = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json"
}
def print_section(title):
"""Print a simple section header"""
print("\n" + "=" * 50)
print(title)
print("=" * 50)
def format_response(label, response):
"""Format and print JSON response with clear structure"""
print(f"\n{label}: {response.status_code}")
if response.status_code == 200:
try:
# Parse and return the JSON data
data = response.json()
# Format and print it with indentation
formatted_json = json.dumps(data, indent=2)
print(formatted_json)
return data
except json.JSONDecodeError:
print("Error: Could not parse JSON response")
print(response.text)
return None
else:
print(f"Error: {response.text}")
return None
# First, check authentication and user info
print_section("AUTHENTICATION CHECK")
org_url = f"{BASE_URL}/whoami-v2"
response = requests.get(org_url, headers=headers)
user_data = format_response("Whoami response", response)
# List existing resource groups
print_section("RESOURCE GROUPS")
list_rg_url = f"{BASE_URL}/organizations/{ORG_NAME}/resource-groups"
response = requests.get(list_rg_url, headers=headers)
resource_groups = format_response("List resource groups response", response)
# Create a test resource group only if it doesn't already exist
print_section("RESOURCE GROUP CREATION TEST")
new_group_name = "test-resource-group"
new_group_description = "A test resource group"
# Check if a group with this name already exists
group_exists = False
if resource_groups:
for group in resource_groups:
if group.get("name") == new_group_name:
group_exists = True
print(f"\nResource group '{new_group_name}' already exists with ID: {group.get('id')}")
print("Skipping creation to avoid duplicates.")
break
# Only create the group if it doesn't exist
if not group_exists:
create_rg_url = f"{BASE_URL}/organizations/{ORG_NAME}/resource-groups"
data = {
"name": new_group_name,
"description": new_group_description
}
response = requests.post(create_rg_url, headers=headers, json=data)
format_response("Create resource group response", response)
"""
Expected response:
==================================================
AUTHENTICATION CHECK
==================================================
Whoami response: 200
{
"type": "user",
"id": "67c8834889772d508b6fa33c",
"name": "joshhayles",
"fullname": "Josh Hayles",
"isPro": false,
"avatarUrl": "https://cdn-avatars.huggingface.co/v1/production/uploads/67c8834889772d508b6fa33c/oZqi8zNQsTCSunm5NFnhE.png",
"orgs": [
{
"type": "org",
"id": "67a287f99b8fb9f109323d45",
"name": "eh-quizz",
"fullname": "eh-quizz",
"email": "[email protected]",
"canPay": false,
"periodEnd": 1743465599,
"avatarUrl": "https://cdn-avatars.huggingface.co/v1/production/uploads/67c8834889772d508b6fa33c/dU5WKVWLSq0jaYjG-zJBg.png",
"roleInOrg": "admin",
"isEnterprise": true
}
],
"auth": {
"type": "access_token",
"accessToken": {
"displayName": "wuzzup-token",
"role": "fineGrained",
"createdAt": "2025-03-05T23:19:18.771Z",
"fineGrained": {
"canReadGatedRepos": true,
"global": [
"inference.serverless.write",
"discussion.write",
"post.write"
],
"scoped": [
{
"entity": {
"_id": "67c9e5b9b98e0e0b6605992f",
"type": "model",
"name": "eh-quizz/testing-magic"
},
"permissions": [
"repo.content.read",
"repo.write"
]
},
{
"entity": {
"_id": "67a287f99b8fb9f109323d45",
"type": "org",
"name": "eh-quizz"
},
"permissions": [
"repo.content.read",
"discussion.write",
"repo.write",
"org.read",
"org.write",
"resourceGroup.write"
]
},
{
"entity": {
"_id": "67c8834889772d508b6fa33c",
"type": "user",
"name": "joshhayles"
},
"permissions": [
"repo.content.read",
"inference.endpoints.infer.write",
"user.webhooks.read",
"discussion.write"
]
}
]
}
}
}
}
==================================================
RESOURCE GROUPS
==================================================
List resource groups response: 200
[]
==================================================
RESOURCE GROUP CREATION TEST
==================================================
Create resource group response: 200
{
"id": "67ca1e89dd6c6628fcb2ca6d",
"name": "test-resource-group",
"description": "A test resource group",
"users": [],
"repos": []
}
""" |