Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,29 @@ def get_my_spaces(hf_token):
|
|
| 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 |
def make_spaces_paused(hf_token, selected_spaces, progress=gr.Progress()):
|
| 30 |
"""Sets the visibility of selected Hugging Face Spaces to private."""
|
| 31 |
if not hf_token:
|
|
@@ -109,7 +131,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 109 |
placeholder="hf_...",
|
| 110 |
scale=3,
|
| 111 |
)
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
|
| 114 |
with gr.Group():
|
| 115 |
# This (invisible) state component is the key to the select_all fix
|
|
@@ -123,7 +147,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 123 |
)
|
| 124 |
|
| 125 |
make_private_button = gr.Button("Set Selected Spaces Private", variant="primary")
|
| 126 |
-
make_paused_button = gr.Button("Set Selected Spaces Paused", variant="
|
| 127 |
status_output = gr.Textbox(label="Status", interactive=False, lines=5)
|
| 128 |
|
| 129 |
# --- Event Listeners ---
|
|
@@ -133,13 +157,23 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 133 |
spaces, message, state_list = get_my_spaces(token)
|
| 134 |
# Also reset the select_all checkbox to unchecked
|
| 135 |
return gr.update(choices=spaces, value=[]), message, state_list, gr.update(value=False)
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
list_spaces_button.click(
|
| 138 |
fn=list_spaces_action,
|
| 139 |
inputs=hf_token_input,
|
| 140 |
outputs=[spaces_checkboxes, status_output, spaces_list_state, select_all_checkbox]
|
| 141 |
)
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
def select_all_action(select_all_checked, full_list_of_spaces):
|
| 144 |
"""
|
| 145 |
Called when the "Select All" checkbox is changed.
|
|
|
|
| 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 |
+
def get_my_spaces_all(hf_token):
|
| 30 |
+
"""Fetches a list of the user's public Hugging Face Spaces."""
|
| 31 |
+
if not hf_token:
|
| 32 |
+
# Return empty values for all outputs
|
| 33 |
+
return [], "Please enter your Hugging Face token.", []
|
| 34 |
+
try:
|
| 35 |
+
api = HfApi(token=hf_token)
|
| 36 |
+
# Get the username to list spaces accurately
|
| 37 |
+
username = api.whoami()["name"]
|
| 38 |
+
spaces = api.list_spaces(author=username)
|
| 39 |
+
# Filter for only public spaces
|
| 40 |
+
public_spaces = [space.id for space in spaces]
|
| 41 |
+
if not public_spaces:
|
| 42 |
+
return [], "Successfully connected, but you have no public spaces.", []
|
| 43 |
+
# Return the list of spaces for the checkboxes and the state
|
| 44 |
+
return public_spaces, f"Successfully listed {len(public_spaces)} public spaces.", public_spaces
|
| 45 |
+
except HfHubHTTPError as e:
|
| 46 |
+
# Handle common authentication errors
|
| 47 |
+
return [], f"Authentication error: Please check your token. Details: {e}", []
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return [], f"An error occurred: {e}", []
|
| 50 |
+
|
| 51 |
def make_spaces_paused(hf_token, selected_spaces, progress=gr.Progress()):
|
| 52 |
"""Sets the visibility of selected Hugging Face Spaces to private."""
|
| 53 |
if not hf_token:
|
|
|
|
| 131 |
placeholder="hf_...",
|
| 132 |
scale=3,
|
| 133 |
)
|
| 134 |
+
with gr.Column():
|
| 135 |
+
list_spaces_button = gr.Button("List My Public Spaces", variant="secondary", scale=1)
|
| 136 |
+
list_spaces_button_all = gr.Button("List All My Spaces", variant="secondary", scale=1)
|
| 137 |
|
| 138 |
with gr.Group():
|
| 139 |
# This (invisible) state component is the key to the select_all fix
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
make_private_button = gr.Button("Set Selected Spaces Private", variant="primary")
|
| 150 |
+
make_paused_button = gr.Button("Set Selected Spaces Paused", variant="primary")
|
| 151 |
status_output = gr.Textbox(label="Status", interactive=False, lines=5)
|
| 152 |
|
| 153 |
# --- Event Listeners ---
|
|
|
|
| 157 |
spaces, message, state_list = get_my_spaces(token)
|
| 158 |
# Also reset the select_all checkbox to unchecked
|
| 159 |
return gr.update(choices=spaces, value=[]), message, state_list, gr.update(value=False)
|
| 160 |
+
|
| 161 |
+
def list_spaces_action_all(token):
|
| 162 |
+
"""Called when the list button is clicked. Updates the checkboxes and the state."""
|
| 163 |
+
spaces, message, state_list = get_my_spaces_all(token)
|
| 164 |
+
# Also reset the select_all checkbox to unchecked
|
| 165 |
+
return gr.update(choices=spaces, value=[]), message, state_list, gr.update(value=False)
|
| 166 |
+
|
| 167 |
list_spaces_button.click(
|
| 168 |
fn=list_spaces_action,
|
| 169 |
inputs=hf_token_input,
|
| 170 |
outputs=[spaces_checkboxes, status_output, spaces_list_state, select_all_checkbox]
|
| 171 |
)
|
| 172 |
+
list_spaces_button_all.click(
|
| 173 |
+
fn=list_spaces_action_all,
|
| 174 |
+
inputs=hf_token_input,
|
| 175 |
+
outputs=[spaces_checkboxes, status_output, spaces_list_state, select_all_checkbox]
|
| 176 |
+
)
|
| 177 |
def select_all_action(select_all_checked, full_list_of_spaces):
|
| 178 |
"""
|
| 179 |
Called when the "Select All" checkbox is changed.
|