broadfield-dev commited on
Commit
fe42caa
·
verified ·
1 Parent(s): a36fcb0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -0
app.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import HfApi, HfFolder
3
+ from huggingface_hub.utils import HfHubHTTPError
4
+
5
+ # Helper function to initialize the HfApi client
6
+ def get_hf_api(token):
7
+ if not token:
8
+ raise gr.Error("Hugging Face API token is required.")
9
+ return HfApi(token=token)
10
+
11
+ # --- Spaces Functions ---
12
+ def list_user_spaces(token, author):
13
+ try:
14
+ api = get_hf_api(token)
15
+ spaces = api.list_spaces(author=author)
16
+ return "\n".join([space.id for space in spaces]) if spaces else "No spaces found for this user."
17
+ except HfHubHTTPError as e:
18
+ return f"Error: {e}"
19
+
20
+ def create_space(token, repo_id, repo_type, space_sdk):
21
+ try:
22
+ api = get_hf_api(token)
23
+ repo_url = api.create_repo(repo_id=repo_id, repo_type=repo_type, space_sdk=space_sdk)
24
+ return f"Space created successfully! URL: {repo_url}"
25
+ except HfHubHTTPError as e:
26
+ return f"Error: {e}"
27
+
28
+ def delete_space(token, repo_id):
29
+ try:
30
+ api = get_hf_api(token)
31
+ api.delete_repo(repo_id=repo_id, repo_type='space')
32
+ return f"Space '{repo_id}' deleted successfully."
33
+ except HfHubHTTPError as e:
34
+ return f"Error: {e}"
35
+
36
+ # --- Models Functions ---
37
+ def list_user_models(token, author):
38
+ try:
39
+ api = get_hf_api(token)
40
+ models = api.list_models(author=author)
41
+ return "\n".join([model.id for model in models]) if models else "No models found for this user."
42
+ except HfHubHTTPError as e:
43
+ return f"Error: {e}"
44
+
45
+ def delete_model(token, repo_id):
46
+ try:
47
+ api = get_hf_api(token)
48
+ api.delete_repo(repo_id=repo_id, repo_type='model')
49
+ return f"Model '{repo_id}' deleted successfully."
50
+ except HfHubHTTPError as e:
51
+ return f"Error: {e}"
52
+
53
+ # --- Datasets Functions ---
54
+ def list_user_datasets(token, author):
55
+ try:
56
+ api = get_hf_api(token)
57
+ datasets = api.list_datasets(author=author)
58
+ return "\n".join([dataset.id for dataset in datasets]) if datasets else "No datasets found for this user."
59
+ except HfHubHTTPError as e:
60
+ return f"Error: {e}"
61
+
62
+ def delete_dataset(token, repo_id):
63
+ try:
64
+ api = get_hf_api(token)
65
+ api.delete_repo(repo_id=repo_id, repo_type='dataset')
66
+ return f"Dataset '{repo_id}' deleted successfully."
67
+ except HfHubHTTPError as e:
68
+ return f"Error: {e}"
69
+
70
+ # --- User Info Functions ---
71
+ def whoami(token):
72
+ try:
73
+ api = get_hf_api(token)
74
+ user_info = api.whoami()
75
+ return user_info
76
+ except HfHubHTTPError as e:
77
+ return f"Error: {e}"
78
+
79
+ def get_user_info(token, user):
80
+ try:
81
+ api = get_hf_api(token)
82
+ user_info = api.get_user_info(user)
83
+ return user_info
84
+ except HfHubHTTPError as e:
85
+ return f"Error: {e}"
86
+
87
+
88
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
89
+ gr.Markdown("# Hugging Face Hub Toolkit")
90
+ 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.")
91
+
92
+ with gr.Row():
93
+ hf_token = gr.Textbox(label="Hugging Face API Token", type="password", placeholder="hf_...")
94
+
95
+ with gr.Tabs():
96
+ with gr.TabItem("Spaces"):
97
+ with gr.Row():
98
+ with gr.Column():
99
+ gr.Markdown("### List User Spaces")
100
+ list_spaces_author = gr.Textbox(label="Author (Username)")
101
+ list_spaces_btn = gr.Button("List Spaces")
102
+ list_spaces_output = gr.Textbox(label="User Spaces", lines=10)
103
+ with gr.Column():
104
+ gr.Markdown("### Create a New Space")
105
+ create_space_id = gr.Textbox(label="Space Repo ID (e.g., username/spacename)")
106
+ create_space_sdk = gr.Dropdown(label="Space SDK", choices=["gradio", "streamlit", "docker", "static"])
107
+ create_space_btn = gr.Button("Create Space")
108
+ create_space_output = gr.Textbox(label="Result")
109
+ with gr.Column():
110
+ gr.Markdown("### Delete a Space")
111
+ delete_space_id = gr.Textbox(label="Space Repo ID to Delete")
112
+ delete_space_btn = gr.Button("Delete Space")
113
+ delete_space_output = gr.Textbox(label="Result")
114
+
115
+ with gr.TabItem("Models"):
116
+ with gr.Row():
117
+ with gr.Column():
118
+ gr.Markdown("### List User Models")
119
+ list_models_author = gr.Textbox(label="Author (Username)")
120
+ list_models_btn = gr.Button("List Models")
121
+ list_models_output = gr.Textbox(label="User Models", lines=10)
122
+ with gr.Column():
123
+ gr.Markdown("### Delete a Model")
124
+ delete_model_id = gr.Textbox(label="Model Repo ID to Delete")
125
+ delete_model_btn = gr.Button("Delete Model")
126
+ delete_model_output = gr.Textbox(label="Result")
127
+
128
+ with gr.TabItem("Datasets"):
129
+ with gr.Row():
130
+ with gr.Column():
131
+ gr.Markdown("### List User Datasets")
132
+ list_datasets_author = gr.Textbox(label="Author (Username)")
133
+ list_datasets_btn = gr.Button("List Datasets")
134
+ list_datasets_output = gr.Textbox(label="User Datasets", lines=10)
135
+ with gr.Column():
136
+ gr.Markdown("### Delete a Dataset")
137
+ delete_dataset_id = gr.Textbox(label="Dataset Repo ID to Delete")
138
+ delete_dataset_btn = gr.Button("Delete Dataset")
139
+ delete_dataset_output = gr.Textbox(label="Result")
140
+
141
+ with gr.TabItem("User Info"):
142
+ with gr.Row():
143
+ with gr.Column():
144
+ gr.Markdown("### Who Am I?")
145
+ whoami_btn = gr.Button("Get My Info")
146
+ whoami_output = gr.JSON(label="My User Info")
147
+ with gr.Column():
148
+ gr.Markdown("### Get User Info")
149
+ get_user_info_user = gr.Textbox(label="Username")
150
+ get_user_info_btn = gr.Button("Get User Info")
151
+ get_user_info_output = gr.JSON(label="User Info")
152
+
153
+ # --- Event Handlers ---
154
+ # Spaces
155
+ list_spaces_btn.click(list_user_spaces, inputs=[hf_token, list_spaces_author], outputs=list_spaces_output)
156
+ 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)
157
+ delete_space_btn.click(delete_space, inputs=[hf_token, delete_space_id], outputs=delete_space_output)
158
+
159
+ # Models
160
+ list_models_btn.click(list_user_models, inputs=[hf_token, list_models_author], outputs=list_models_output)
161
+ delete_model_btn.click(delete_model, inputs=[hf_token, delete_model_id], outputs=delete_model_output)
162
+
163
+ # Datasets
164
+ list_datasets_btn.click(list_user_datasets, inputs=[hf_token, list_datasets_author], outputs=list_datasets_output)
165
+ delete_dataset_btn.click(delete_dataset, inputs=[hf_token, delete_dataset_id], outputs=delete_dataset_output)
166
+
167
+ # User Info
168
+ whoami_btn.click(whoami, inputs=hf_token, outputs=whoami_output)
169
+ get_user_info_btn.click(get_user_info, inputs=[hf_token, get_user_info_user], outputs=get_user_info_output)
170
+
171
+
172
+ if __name__ == "__main__":
173
+ demo.launch()