joshhayles's picture
Upload 3 files
0c221a9 verified
# 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": []
}
"""