Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,124 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import HfApi
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 8 |
try:
|
| 9 |
api = HfApi(token=hf_token)
|
| 10 |
-
spaces
|
|
|
|
|
|
|
|
|
|
| 11 |
public_spaces = [space.id for space in spaces if not space.private]
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
api.update_repo_settings(repo_id=space_id, private=True)
|
| 29 |
-
|
| 30 |
except Exception as e:
|
| 31 |
-
|
| 32 |
|
| 33 |
-
return "\n".join(
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"An error occurred: {e}"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 50 |
|
|
|
|
|
|
|
| 51 |
def list_spaces_action(token):
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 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(
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
select_all_checkbox.change(
|
| 65 |
fn=select_all_action,
|
| 66 |
-
inputs=[select_all_checkbox,
|
| 67 |
outputs=spaces_checkboxes
|
| 68 |
)
|
| 69 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import HfApi
|
| 3 |
+
from huggingface_hub.utils import HfHubHTTPError
|
| 4 |
+
|
| 5 |
+
# --- Core Functions ---
|
| 6 |
|
| 7 |
def get_my_spaces(hf_token):
|
| 8 |
"""Fetches a list of the user's public Hugging Face Spaces."""
|
| 9 |
if not hf_token:
|
| 10 |
+
# Return empty values for all outputs
|
| 11 |
+
return [], "Please enter your Hugging Face token.", []
|
| 12 |
try:
|
| 13 |
api = HfApi(token=hf_token)
|
| 14 |
+
# Get the username to list spaces accurately
|
| 15 |
+
username = api.whoami()["name"]
|
| 16 |
+
spaces = api.list_spaces(author=username)
|
| 17 |
+
# Filter for only public spaces
|
| 18 |
public_spaces = [space.id for space in spaces if not space.private]
|
| 19 |
+
if not public_spaces:
|
| 20 |
+
return [], "Successfully connected, but you have no public spaces.", []
|
| 21 |
+
# Return the list of spaces for the checkboxes and the state
|
| 22 |
+
return public_spaces, f"Successfully listed {len(public_spaces)} public spaces.", public_spaces
|
| 23 |
+
except HfHubHTTPError as e:
|
| 24 |
+
# Handle common authentication errors
|
| 25 |
+
return [], f"Authentication error: Please check your token. Details: {e}", []
|
| 26 |
except Exception as e:
|
| 27 |
+
return [], f"An error occurred: {e}", []
|
| 28 |
+
|
| 29 |
|
| 30 |
+
def make_spaces_private(hf_token, selected_spaces, progress=gr.Progress()):
|
| 31 |
"""Sets the visibility of selected Hugging Face Spaces to private."""
|
| 32 |
if not hf_token:
|
| 33 |
return "Please enter your Hugging Face token."
|
| 34 |
if not selected_spaces:
|
| 35 |
return "Please select at least one space to make private."
|
| 36 |
+
|
| 37 |
+
# Disable progress tracking temporarily for Gradio 4+ compatibility with loops
|
| 38 |
+
progress(0, desc="Starting...")
|
| 39 |
+
|
| 40 |
try:
|
| 41 |
api = HfApi(token=hf_token)
|
| 42 |
+
log_messages = []
|
| 43 |
+
total_spaces = len(selected_spaces)
|
| 44 |
+
|
| 45 |
+
for i, space_id in enumerate(selected_spaces):
|
| 46 |
+
progress((i + 1) / total_spaces, desc=f"Processing '{space_id}'...")
|
| 47 |
try:
|
| 48 |
api.update_repo_settings(repo_id=space_id, private=True)
|
| 49 |
+
log_messages.append(f"✅ Successfully made '{space_id}' private.")
|
| 50 |
except Exception as e:
|
| 51 |
+
log_messages.append(f"❌ Failed to make '{space_id}' private: {e}")
|
| 52 |
|
| 53 |
+
return "\n".join(log_messages)
|
| 54 |
+
except HfHubHTTPError as e:
|
| 55 |
+
return f"Authentication error: Please check your token. Details: {e}"
|
| 56 |
except Exception as e:
|
| 57 |
return f"An error occurred: {e}"
|
| 58 |
|
| 59 |
+
# --- Gradio UI ---
|
| 60 |
+
|
| 61 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 62 |
+
gr.Markdown(
|
| 63 |
+
"""
|
| 64 |
+
# 🛡️ Hugging Face Space Privacy Control
|
| 65 |
+
Quickly make your public Hugging Face Spaces private.
|
| 66 |
+
1. Enter your Hugging Face access token with **write** permission.
|
| 67 |
+
2. Click "List My Public Spaces".
|
| 68 |
+
3. Select the spaces you want to make private (or use "Select All").
|
| 69 |
+
4. Click the final button to make them private.
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
hf_token_input = gr.Textbox(
|
| 75 |
+
label="Hugging Face Write Token",
|
| 76 |
+
type="password",
|
| 77 |
+
placeholder="hf_...",
|
| 78 |
+
scale=3,
|
| 79 |
+
)
|
| 80 |
+
list_spaces_button = gr.Button("List My Public Spaces", variant="secondary", scale=1)
|
| 81 |
|
| 82 |
+
with gr.Group():
|
| 83 |
+
# This (invisible) state component is the key to the fix
|
| 84 |
+
spaces_list_state = gr.State([])
|
| 85 |
+
|
| 86 |
+
select_all_checkbox = gr.Checkbox(label="Select All / Deselect All")
|
| 87 |
+
|
| 88 |
+
spaces_checkboxes = gr.CheckboxGroup(
|
| 89 |
+
label="Your Public Spaces",
|
| 90 |
+
info="Only your public spaces are shown here."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
make_private_button = gr.Button("Make Selected Spaces Private", variant="primary")
|
| 94 |
status_output = gr.Textbox(label="Status", interactive=False)
|
| 95 |
|
| 96 |
+
# --- Event Listeners ---
|
| 97 |
+
|
| 98 |
def list_spaces_action(token):
|
| 99 |
+
"""Called when the list button is clicked. Updates the checkboxes and the state."""
|
| 100 |
+
spaces, message, state_list = get_my_spaces(token)
|
| 101 |
+
return gr.update(choices=spaces, value=[]), message, state_list
|
| 102 |
|
| 103 |
list_spaces_button.click(
|
| 104 |
fn=list_spaces_action,
|
| 105 |
inputs=hf_token_input,
|
| 106 |
+
outputs=[spaces_checkboxes, status_output, spaces_list_state]
|
| 107 |
)
|
| 108 |
|
| 109 |
+
def select_all_action(select_all_checked, full_list_of_spaces):
|
| 110 |
+
"""
|
| 111 |
+
Called when the "Select All" checkbox is changed.
|
| 112 |
+
Uses the list from the state to update the checkbox group.
|
| 113 |
+
"""
|
| 114 |
+
if select_all_checked:
|
| 115 |
+
return gr.update(value=full_list_of_spaces) # Select all
|
| 116 |
+
else:
|
| 117 |
+
return gr.update(value=[]) # Deselect all
|
| 118 |
|
| 119 |
select_all_checkbox.change(
|
| 120 |
fn=select_all_action,
|
| 121 |
+
inputs=[select_all_checkbox, spaces_list_state], # Input is from the state now
|
| 122 |
outputs=spaces_checkboxes
|
| 123 |
)
|
| 124 |
|