|
import gradio as gr |
|
from huggingface_hub import HfApi, HfFolder |
|
from huggingface_hub.utils import HfHubHTTPError |
|
|
|
|
|
def get_hf_api(token): |
|
if not token: |
|
raise gr.Error("Hugging Face API token is required.") |
|
return HfApi(token=token) |
|
|
|
|
|
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}" |
|
|
|
|
|
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}" |
|
|
|
|
|
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}" |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
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() |