hf-cli-jobs-uv-run-scripts / permissions.py
freddyaboulton's picture
Upload permissions.py with huggingface_hub
6c9bfdf verified
# /// script
# dependencies = [
# "huggingface_hub",
# "tqdm",
# "httpx",
# ]
# ///
import httpx
from huggingface_hub import HfApi, upload_file, hf_hub_download
import os
import tqdm
import time
from pathlib import Path
HEADERS = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.getenv('HF_TOKEN')}",
}
#changed_usernames = hf_hub_download(
# repo_id="gradio/job-outputs",
# repo_type="dataset",
# filename="changed_users_contributors.txt",
# token=os.getenv("HF_TOKEN"),
#)
changed_users = set([]) #set(Path(changed_usernames).read_text().splitlines())
def change_permission(member: str, role: str):
url = f"https://huggingface.co/api/organizations/MCP-1st-Birthday/members/{member}/role"
r = httpx.put(url, headers=HEADERS, json={"role": role})
print(f"Changed {member} to {role}: {r.status_code} - {r.text}")
return r.status_code, r.json()["success"]
api = HfApi(token=os.getenv('HF_TOKEN'))
members = api.list_organization_members("MCP-1st-Birthday")
try:
for member in tqdm.tqdm(members):
if member.username not in changed_users and member.username not in ["freddyaboulton", "hmb", "pngwn", "hysts", "akhaliq", "ysharma", "abidlabs"]:
status_code, success = change_permission(member.username, "contributor")
if status_code == 200 and success:
changed_users.add(member.username)
time.sleep(0.2)
except Exception as e:
print(f"Error occurred: {e}")
finally:
Path("changed_users.txt").write_text("\n".join(changed_users))
upload_file(path_or_fileobj="changed_users.txt",
path_in_repo="changed_users_contributors.txt",
repo_id="gradio/job-outputs",
repo_type="dataset",
token=os.getenv("HF_TOKEN"))