Update app.py
Browse files11 unmodified lines121314151617183 unmodified lines2223242526272829303111 unmodified lines434445464748491 unmodified line51525354555657583 unmodified lines6263646566676819 unmodified lines888990919293942 unmodified lines9798991001011021031 unmodified line1051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821836 unmodified lines19019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130211 unmodified lines# --- UI Functions ---def handle_token_change(token, current_author):"""Called when the token is entered. Fetches user info and updates UI interactivity."""3 unmodified linesmanage_files_btn: gr.update(interactive=False),delete_repo_btn: gr.update(interactive=False),commit_btn: gr.update(interactive=False),whoami_output: gr.update(value=None, visible=False)}# Do not clear the author field if the user typed it manuallyreturn (None, current_author, *update_dict.values())try:api = get_hf_api(token)11 unmodified linesreturn (token, username, *update_dict.values())except HfHubHTTPError as e:# Token is invalidgr.Warning(f"Invalid Token: {e}. You can only perform read-only actions.")update_dict = {manage_files_btn: gr.update(interactive=False),1 unmodified linecommit_btn: gr.update(interactive=False),whoami_output: gr.update(value=None, visible=False)}# Clear username but keep author field as isreturn (token, current_author, *update_dict.values())def list_repos(token, author, repo_type):3 unmodified linesreturn gr.update(choices=[], value=None), gr.update(visible=False), gr.update(visible=False)try:api = get_hf_api(token)# Use the dedicated list functions for claritylist_fn = getattr(api, f"list_{repo_type}s")repos = list_fn(author=author)repo_ids = [repo.id for repo in repos]19 unmodified linestry:api = get_hf_api(token)api.delete_repo(repo_id=repo_id, repo_type=repo_type)gr.Info(f"Successfully deleted '{repo_id}'. Listing updated repositories.")return None, gr.update(visible=False), gr.update(visible=False)except HfHubHTTPError as e:gr.Error(f"Failed to delete repository: {e}")2 unmodified lines# --- File Editor Functions ---def show_file_manager(token, repo_id, repo_type):"""Lists files in the selected repo and shows the editor panel."""if not repo_id:gr.Warning("No repository selected.")return gr.update(visible=False), gr.update(), gr.update(), gr.update()1 unmodified lineapi = get_hf_api(token)repo_files = api.list_repo_files(repo_id=repo_id, repo_type=repo_type)filtered_files = [f for f in repo_files if not f.startswith('.')]return (gr.update(visible=True),gr.update(choices=filtered_files, value=None),gr.update(value="## Select a file to view or edit.", language='markdown'),"")except RepositoryNotFoundError:gr.Error(f"Repository '{repo_id}' not found. It might be private and require a token.")return gr.update(visible=False), gr.update(), gr.update(), gr.update()except Exception as e:gr.Error(f"Could not list files: {e}")return gr.update(visible=False), gr.update(), gr.update(), gr.update()def load_file_content(token, repo_id, repo_type, filepath):"""Downloads and displays the content of a selected file."""if not filepath:return gr.update(value="## Select a file to view its content.", language='markdown')try:api = get_hf_api(token)local_path = api.hf_hub_download(repo_id=repo_id, repo_type=repo_type, filename=filepath, token=token)with open(local_path, 'r', encoding='utf-8') as f:content = f.read()language = os.path.splitext(filepath)[1].lstrip('.').lower()supported_langs = ['python', 'typescript', 'css', 'json', 'markdown', 'html', 'javascript']if language == 'py': language = 'python'if language == 'js': language = 'javascript'if language == 'md': language = 'markdown'return gr.update(value=content, language=language if language in supported_langs else 'plaintext')except Exception as e:gr.Error(f"Could not load file '{filepath}': {e}")return gr.update(value=f"Error loading file: {e}", language='plaintext')def commit_file(token, repo_id, repo_type, filepath, content, commit_message):"""Commits the edited file content back to the repository."""if not token:gr.Error("A write-enabled token is required to commit changes.")returnif not filepath:gr.Warning("No file is selected to commit.")returnif not commit_message:gr.Warning("Commit message cannot be empty.")returntry:temp_dir = "hf_temp_files"os.makedirs(temp_dir, exist_ok=True)temp_file_path = os.path.join(temp_dir, f"{uuid.uuid4()}_{os.path.basename(filepath)}")with open(temp_file_path, "w", encoding="utf-8") as f:f.write(content)api = get_hf_api(token)api.upload_file(path_or_fileobj=temp_file_path,path_in_repo=filepath,repo_id=repo_id,repo_type=repo_type,commit_message=commit_message,)os.remove(temp_file_path)gr.Info(f"Successfully committed '{filepath}' to '{repo_id}'!")except Exception as e:gr.Error(f"Failed to commit file: {e}")# --- Gradio UI Layout ---with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="Hugging Face Hub Toolkit") as demo:# State management6 unmodified linesgr.Markdown("An intuitive interface to manage your Hugging Face repositories. **Enter a write-token for full access.**")with gr.Row():hf_token = gr.Textbox(label="Hugging Face API Token (write permission recommended)",type="password", placeholder="hf_...", scale=3,)whoami_output = gr.JSON(label="Authenticated User", visible=False, scale=1)with gr.Row(equal_height=False):# PANEL 1: List and Select Reposwith gr.Column(scale=1):gr.Markdown("### 1. Select a Repository")author_input = gr.Textbox(label="Author (Username or Org)", interactive=True)repo_selector = gr.Radio(label="Select a Repository", interactive=True, value=None)# This is the corrected function definition. It now takes UI elements as arguments.def create_repo_lister(repo_type, label, repo_selector_el, action_panel_el, editor_panel_el):with gr.Tab(label, id=repo_type):btn = gr.Button(f"List {label}")btn.click(fn=list_repos,inputs=[hf_token_state, author_input, gr.State(repo_type)],outputs=[repo_selector_el, action_panel_el, editor_panel_el]).then(# After listing, update the author statefn=lambda author: author,inputs=author_input,outputs=author_state)with gr.Tabs() as repo_type_tabs:# The action and editor panels are defined later but referenced here.# We will define placeholder variables for them now.action_panel_ref = gr.Column()editor_panel_ref = gr.Column()# PANEL 2 & 3: Actions and Editorwith gr.Column(scale=3):with gr.Row():with gr.Column(scale=1, visible=False) as action_panel:gr.Markdown("### 2. Choose an Action")manage_files_btn = gr.Button("Manage Files", interactive=False)delete_repo_btn = gr.Button("Delete this Repo", variant="stop", interactive=False)with gr.Column(scale=3, visible=False) as editor_panel:gr.Markdown("### 3. Edit Files")file_selector = gr.Dropdown(label="Select File", interactive=True)code_editor = gr.Code(label="File Content", language="markdown", interactive=True)commit_message_input = gr.Textbox(label="Commit Message", placeholder="e.g., Update README.md", interactive=True)commit_btn = gr.Button("Commit Changes", variant="primary", interactive=False)# --- Post-Layout UI Wiring ---# Now that action_panel and editor_panel are fully defined, we can wire them up# in the create_repo_lister function calls within the Tabs context.with repo_type_tabs:create_repo_lister("space", "Spaces", repo_selector, action_panel, editor_panel)create_repo_lister("model", "Models", repo_selector, action_panel, editor_panel)create_repo_lister("dataset", "Datasets", repo_selector, action_panel, editor_panel)# --- Event Handlers ---hf_token.change(fn=handle_token_change,inputs=[hf_token, author_state],outputs=[hf_token_state, author_state, manage_files_btn, delete_repo_btn, commit_btn, author_input, whoami_output])repo_type_tabs.select(fn=lambda rt: (rt, None, gr.update(choices=[], value=None), gr.update(visible=False), gr.update(visible=False)),inputs=repo_type_tabs,outputs=[selected_repo_type, selected_repo_id, repo_selector, action_panel, editor_panel])repo_selector.select(fn=lambda repo_id: (repo_id, *handle_repo_selection(repo_id)),inputs=repo_selector,outputs=[selected_repo_id, action_panel, editor_panel])manage_files_btn.click(fn=show_file_manager,inputs=[hf_token_state, selected_repo_id, selected_repo_type],outputs=[editor_panel, file_selector, code_editor, commit_message_input])delete_repo_btn.click(fn=delete_repo,inputs=[hf_token_state, selected_repo_id, selected_repo_type],outputs=[selected_repo_id, action_panel, editor_panel],js="() => confirm('Are you sure you want to permanently delete this repository? This action cannot be undone.')").then(# After attempting deletion, refresh the listfn=list_repos,inputs=lambda: (hf_token_state.value, author_state.value, selected_repo_type.value),outputs=[repo_selector, action_panel, editor_panel])file_selector.change(fn=load_file_content,inputs=[hf_token_state, selected_repo_id, selected_repo_type, file_selector],outputs=code_editor)commit_btn.click(fn=commit_file,inputs=[hf_token_state, selected_repo_id, selected_repo_type, file_selector, code_editor, commit_message_input],outputs=[])if __name__ == "__main__":demo.launch()11 unmodified lines121314151617183 unmodified lines2223242526272829303111 unmodified lines4344454647481 unmodified line505152535455563 unmodified lines6061626364656619 unmodified lines868788899091922 unmodified lines95969798991001 unmodified line1021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431446 unmodified lines15115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422511 unmodified lines# --- UI Functions ---def handle_token_change(token):"""Called when the token is entered. Fetches user info and updates UI interactivity."""3 unmodified linesmanage_files_btn: gr.update(interactive=False),delete_repo_btn: gr.update(interactive=False),commit_btn: gr.update(interactive=False),author_input: gr.update(value=""),whoami_output: gr.update(value=None, visible=False)}return (None, "", *update_dict.values())try:api = get_hf_api(token)11 unmodified linesreturn (token, username, *update_dict.values())except HfHubHTTPError as e:gr.Warning(f"Invalid Token: {e}. You can only perform read-only actions.")update_dict = {manage_files_btn: gr.update(interactive=False),1 unmodified linecommit_btn: gr.update(interactive=False),whoami_output: gr.update(value=None, visible=False)}return (token, "", *update_dict.values())def list_repos(token, author, repo_type):3 unmodified linesreturn gr.update(choices=[], value=None), gr.update(visible=False), gr.update(visible=False)try:api = get_hf_api(token)# Use the dedicated list functions for clarity e.g. api.list_models, api.list_spaceslist_fn = getattr(api, f"list_{repo_type}s")repos = list_fn(author=author)repo_ids = [repo.id for repo in repos]19 unmodified linestry:api = get_hf_api(token)api.delete_repo(repo_id=repo_id, repo_type=repo_type)gr.Info(f"Successfully deleted '{repo_id}'.")return None, gr.update(visible=False), gr.update(visible=False)except HfHubHTTPError as e:gr.Error(f"Failed to delete repository: {e}")2 unmodified lines# --- File Editor Functions ---def show_file_manager(token, repo_id, repo_type):if not repo_id:gr.Warning("No repository selected.")return gr.update(visible=False), gr.update(), gr.update(), gr.update()1 unmodified lineapi = get_hf_api(token)repo_files = api.list_repo_files(repo_id=repo_id, repo_type=repo_type)filtered_files = [f for f in repo_files if not f.startswith('.')]return (gr.update(visible=True), gr.update(choices=filtered_files, value=None),gr.update(value="## Select a file to view or edit.", language='markdown'), "")except Exception as e:gr.Error(f"Could not list files: {e}")return gr.update(visible=False), gr.update(), gr.update(), gr.update()def load_file_content(token, repo_id, repo_type, filepath):if not filepath:return gr.update(value="## Select a file to view its content.", language='markdown')try:api = get_hf_api(token)local_path = api.hf_hub_download(repo_id=repo_id, repo_type=repo_type, filename=filepath, token=token)with open(local_path, 'r', encoding='utf-8') as f: content = f.read()language = os.path.splitext(filepath)[1].lstrip('.').lower()if language == 'py': language = 'python'if language == 'js': language = 'javascript'if language == 'md': language = 'markdown'return gr.update(value=content, language=language)except Exception as e:return gr.update(value=f"Error loading file: {e}", language='plaintext')def commit_file(token, repo_id, repo_type, filepath, content, commit_message):if not token: gr.Error("A write-enabled token is required."); returnif not filepath: gr.Warning("No file selected."); returnif not commit_message: gr.Warning("Commit message cannot be empty."); returntry:api = get_hf_api(token)api.upload_file(path_or_fileobj=bytes(content, 'utf-8'), path_in_repo=filepath,repo_id=repo_id, repo_type=repo_type, commit_message=commit_message,)gr.Info(f"Successfully committed '{filepath}' to '{repo_id}'!")except Exception as e:gr.Error(f"Failed to commit file: {e}")# --- Gradio UI Layout ---with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="Hugging Face Hub Toolkit") as demo:# State management6 unmodified linesgr.Markdown("An intuitive interface to manage your Hugging Face repositories. **Enter a write-token for full access.**")with gr.Row():hf_token = gr.Textbox(label="Hugging Face API Token", type="password", placeholder="hf_...", scale=3)whoami_output = gr.JSON(label="Authenticated User", visible=False, scale=1)with gr.Row(equal_height=False):with gr.Column(scale=1):gr.Markdown("### 1. Select a Repository")author_input = gr.Textbox(label="Author (Username or Org)", interactive=True)repo_selector = gr.Radio(label="Select a Repository", interactive=True, value=None)with gr.Tabs() as repo_type_tabs:for repo_type, label in [("space", "Spaces"), ("model", "Models"), ("dataset", "Datasets")]:with gr.Tab(label, id=repo_type):btn = gr.Button(f"List {label}")btn.click(fn=list_repos,inputs=[hf_token_state, author_input, gr.State(repo_type)],outputs=[repo_selector, gr.Column(), gr.Column()] # Dummy outputs for panels to hide them).then(fn=lambda author: author, inputs=author_input, outputs=author_state)with gr.Column(scale=3):with gr.Column(visible=False) as action_panel:gr.Markdown("### 2. Choose an Action")with gr.Row():manage_files_btn = gr.Button("Manage Files", interactive=False, scale=1)delete_repo_btn = gr.Button("Delete This Repo", variant="stop", interactive=False, scale=1)with gr.Column(visible=False) as editor_panel:gr.Markdown("### 3. Edit Files")file_selector = gr.Dropdown(label="Select File", interactive=True)code_editor = gr.Code(label="File Content", language="markdown", interactive=True)commit_message_input = gr.Textbox(label="Commit Message", placeholder="e.g., Update README.md", interactive=True)commit_btn = gr.Button("Commit Changes", variant="primary", interactive=False)# --- Event Handlers ---hf_token.change(fn=handle_token_change, inputs=hf_token,outputs=[hf_token_state, author_state, manage_files_btn, delete_repo_btn, commit_btn, author_input, whoami_output])repo_type_tabs.select(fn=lambda rt: (rt, None, gr.update(choices=[], value=None), gr.update(visible=False), gr.update(visible=False)),inputs=repo_type_tabs,outputs=[selected_repo_type, selected_repo_id, repo_selector, action_panel, editor_panel])repo_selector.select(fn=lambda repo_id: (repo_id, *handle_repo_selection(repo_id)),inputs=repo_selector, outputs=[selected_repo_id, action_panel, editor_panel])manage_files_btn.click(fn=show_file_manager, inputs=[hf_token_state, selected_repo_id, selected_repo_type],outputs=[editor_panel, file_selector, code_editor, commit_message_input])delete_repo_btn.click(fn=delete_repo, inputs=[hf_token_state, selected_repo_id, selected_repo_type],outputs=[selected_repo_id, action_panel, editor_panel],js="() => confirm('Are you sure you want to permanently delete this repository? This action cannot be undone.')").then(# On successful deletion, refresh the repo list.# THIS IS THE CORRECTED PART: Pass Gradio components to inputs, not a lambda.fn=list_repos,inputs=[hf_token_state, author_state, selected_repo_type],outputs=[repo_selector, action_panel, editor_panel])file_selector.change(fn=load_file_content, inputs=[hf_token_state, selected_repo_id, selected_repo_type, file_selector],outputs=code_editor)commit_btn.click(fn=commit_file,inputs=[hf_token_state, selected_repo_id, selected_repo_type, file_selector, code_editor, commit_message_input])if __name__ == "__main__":demo.launch(debug=True)
app.py+48-125