File size: 7,406 Bytes
fe42caa |
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 |
import gradio as gr
from huggingface_hub import HfApi, HfFolder
from huggingface_hub.utils import HfHubHTTPError
# Helper function to initialize the HfApi client
def get_hf_api(token):
if not token:
raise gr.Error("Hugging Face API token is required.")
return HfApi(token=token)
# --- Spaces Functions ---
def list_user_spaces(token, author):
try:
api = get_hf_api(token)
spaces = api.list_spaces(author=author)
return "\n".join([space.id for space in spaces]) if spaces else "No spaces found for this user."
except HfHubHTTPError as e:
return f"Error: {e}"
def create_space(token, repo_id, repo_type, space_sdk):
try:
api = get_hf_api(token)
repo_url = api.create_repo(repo_id=repo_id, repo_type=repo_type, space_sdk=space_sdk)
return f"Space created successfully! URL: {repo_url}"
except HfHubHTTPError as e:
return f"Error: {e}"
def delete_space(token, repo_id):
try:
api = get_hf_api(token)
api.delete_repo(repo_id=repo_id, repo_type='space')
return f"Space '{repo_id}' deleted successfully."
except HfHubHTTPError as e:
return f"Error: {e}"
# --- Models Functions ---
def list_user_models(token, author):
try:
api = get_hf_api(token)
models = api.list_models(author=author)
return "\n".join([model.id for model in models]) if models else "No models found for this user."
except HfHubHTTPError as e:
return f"Error: {e}"
def delete_model(token, repo_id):
try:
api = get_hf_api(token)
api.delete_repo(repo_id=repo_id, repo_type='model')
return f"Model '{repo_id}' deleted successfully."
except HfHubHTTPError as e:
return f"Error: {e}"
# --- Datasets Functions ---
def list_user_datasets(token, author):
try:
api = get_hf_api(token)
datasets = api.list_datasets(author=author)
return "\n".join([dataset.id for dataset in datasets]) if datasets else "No datasets found for this user."
except HfHubHTTPError as e:
return f"Error: {e}"
def delete_dataset(token, repo_id):
try:
api = get_hf_api(token)
api.delete_repo(repo_id=repo_id, repo_type='dataset')
return f"Dataset '{repo_id}' deleted successfully."
except HfHubHTTPError as e:
return f"Error: {e}"
# --- User Info Functions ---
def whoami(token):
try:
api = get_hf_api(token)
user_info = api.whoami()
return user_info
except HfHubHTTPError as e:
return f"Error: {e}"
def get_user_info(token, user):
try:
api = get_hf_api(token)
user_info = api.get_user_info(user)
return user_info
except HfHubHTTPError as e:
return f"Error: {e}"
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# Hugging Face Hub Toolkit")
gr.Markdown("A Gradio interface for interacting with the Hugging Face Hub. Enter your Hugging Face API token with 'write' permissions to use the tools.")
with gr.Row():
hf_token = gr.Textbox(label="Hugging Face API Token", type="password", placeholder="hf_...")
with gr.Tabs():
with gr.TabItem("Spaces"):
with gr.Row():
with gr.Column():
gr.Markdown("### List User Spaces")
list_spaces_author = gr.Textbox(label="Author (Username)")
list_spaces_btn = gr.Button("List Spaces")
list_spaces_output = gr.Textbox(label="User Spaces", lines=10)
with gr.Column():
gr.Markdown("### Create a New Space")
create_space_id = gr.Textbox(label="Space Repo ID (e.g., username/spacename)")
create_space_sdk = gr.Dropdown(label="Space SDK", choices=["gradio", "streamlit", "docker", "static"])
create_space_btn = gr.Button("Create Space")
create_space_output = gr.Textbox(label="Result")
with gr.Column():
gr.Markdown("### Delete a Space")
delete_space_id = gr.Textbox(label="Space Repo ID to Delete")
delete_space_btn = gr.Button("Delete Space")
delete_space_output = gr.Textbox(label="Result")
with gr.TabItem("Models"):
with gr.Row():
with gr.Column():
gr.Markdown("### List User Models")
list_models_author = gr.Textbox(label="Author (Username)")
list_models_btn = gr.Button("List Models")
list_models_output = gr.Textbox(label="User Models", lines=10)
with gr.Column():
gr.Markdown("### Delete a Model")
delete_model_id = gr.Textbox(label="Model Repo ID to Delete")
delete_model_btn = gr.Button("Delete Model")
delete_model_output = gr.Textbox(label="Result")
with gr.TabItem("Datasets"):
with gr.Row():
with gr.Column():
gr.Markdown("### List User Datasets")
list_datasets_author = gr.Textbox(label="Author (Username)")
list_datasets_btn = gr.Button("List Datasets")
list_datasets_output = gr.Textbox(label="User Datasets", lines=10)
with gr.Column():
gr.Markdown("### Delete a Dataset")
delete_dataset_id = gr.Textbox(label="Dataset Repo ID to Delete")
delete_dataset_btn = gr.Button("Delete Dataset")
delete_dataset_output = gr.Textbox(label="Result")
with gr.TabItem("User Info"):
with gr.Row():
with gr.Column():
gr.Markdown("### Who Am I?")
whoami_btn = gr.Button("Get My Info")
whoami_output = gr.JSON(label="My User Info")
with gr.Column():
gr.Markdown("### Get User Info")
get_user_info_user = gr.Textbox(label="Username")
get_user_info_btn = gr.Button("Get User Info")
get_user_info_output = gr.JSON(label="User Info")
# --- Event Handlers ---
# Spaces
list_spaces_btn.click(list_user_spaces, inputs=[hf_token, list_spaces_author], outputs=list_spaces_output)
create_space_btn.click(create_space, inputs=[hf_token, create_space_id, gr.Textbox(value="space", visible=False), create_space_sdk], outputs=create_space_output)
delete_space_btn.click(delete_space, inputs=[hf_token, delete_space_id], outputs=delete_space_output)
# Models
list_models_btn.click(list_user_models, inputs=[hf_token, list_models_author], outputs=list_models_output)
delete_model_btn.click(delete_model, inputs=[hf_token, delete_model_id], outputs=delete_model_output)
# Datasets
list_datasets_btn.click(list_user_datasets, inputs=[hf_token, list_datasets_author], outputs=list_datasets_output)
delete_dataset_btn.click(delete_dataset, inputs=[hf_token, delete_dataset_id], outputs=delete_dataset_output)
# User Info
whoami_btn.click(whoami, inputs=hf_token, outputs=whoami_output)
get_user_info_btn.click(get_user_info, inputs=[hf_token, get_user_info_user], outputs=get_user_info_output)
if __name__ == "__main__":
demo.launch() |