Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import HfApi, HfFolder
|
| 3 |
+
|
| 4 |
+
def get_my_spaces(hf_token):
|
| 5 |
+
"""Fetches a list of the user's public Hugging Face Spaces."""
|
| 6 |
+
if not hf_token:
|
| 7 |
+
return [], "Please enter your Hugging Face token."
|
| 8 |
+
try:
|
| 9 |
+
api = HfApi(token=hf_token)
|
| 10 |
+
spaces = api.list_spaces(author=api.whoami()["name"])
|
| 11 |
+
public_spaces = [space.id for space in spaces if not space.private]
|
| 12 |
+
return public_spaces, "Successfully listed your public spaces."
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return [], f"An error occurred: {e}"
|
| 15 |
+
|
| 16 |
+
def make_spaces_private(hf_token, selected_spaces):
|
| 17 |
+
"""Sets the visibility of selected Hugging Face Spaces to private."""
|
| 18 |
+
if not hf_token:
|
| 19 |
+
return "Please enter your Hugging Face token."
|
| 20 |
+
if not selected_spaces:
|
| 21 |
+
return "Please select at least one space to make private."
|
| 22 |
+
try:
|
| 23 |
+
api = HfApi(token=hf_token)
|
| 24 |
+
success_messages = []
|
| 25 |
+
error_messages = []
|
| 26 |
+
for space_id in selected_spaces:
|
| 27 |
+
try:
|
| 28 |
+
api.update_repo_settings(repo_id=space_id, private=True)
|
| 29 |
+
success_messages.append(f"Successfully made '{space_id}' private.")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
error_messages.append(f"Failed to make '{space_id}' private: {e}")
|
| 32 |
+
|
| 33 |
+
return "\n".join(success_messages + error_messages)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"An error occurred: {e}"
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("## Make Your Hugging Face Spaces Private")
|
| 39 |
+
gr.Markdown("Enter your Hugging Face access token with 'write' permission to get a list of your public spaces and make them private.")
|
| 40 |
+
|
| 41 |
+
hf_token_input = gr.Textbox(label="Hugging Face Token", type="password")
|
| 42 |
+
list_spaces_button = gr.Button("List My Spaces")
|
| 43 |
+
|
| 44 |
+
spaces_checkboxes = gr.CheckboxGroup(label="Your Public Spaces")
|
| 45 |
+
select_all_checkbox = gr.Checkbox(label="Select All")
|
| 46 |
+
|
| 47 |
+
make_private_button = gr.Button("Make Selected Spaces Private")
|
| 48 |
+
|
| 49 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 50 |
+
|
| 51 |
+
def list_spaces_action(token):
|
| 52 |
+
spaces, message = get_my_spaces(token)
|
| 53 |
+
return gr.update(choices=spaces, value=[]), message
|
| 54 |
+
|
| 55 |
+
list_spaces_button.click(
|
| 56 |
+
fn=list_spaces_action,
|
| 57 |
+
inputs=hf_token_input,
|
| 58 |
+
outputs=[spaces_checkboxes, status_output]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
def select_all_action(select_all, all_spaces):
|
| 62 |
+
return gr.update(value=all_spaces if select_all else [])
|
| 63 |
+
|
| 64 |
+
select_all_checkbox.change(
|
| 65 |
+
fn=select_all_action,
|
| 66 |
+
inputs=[select_all_checkbox, spaces_checkboxes],
|
| 67 |
+
outputs=spaces_checkboxes
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
make_private_button.click(
|
| 71 |
+
fn=make_spaces_private,
|
| 72 |
+
inputs=[hf_token_input, spaces_checkboxes],
|
| 73 |
+
outputs=status_output
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
demo.launch()
|