Spaces:
Running on Zero
Running on Zero
| import gradio as gr | |
| import spaces | |
| import os | |
| from huggingface_hub import HfApi | |
| def _init_gpu(): | |
| pass | |
| # Read token from Space Secret (recommended) or environment | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| def get_status(): | |
| if HF_TOKEN: | |
| return "✅ Token loaded from Space Secrets" | |
| return "❌ No HF_TOKEN found. Please add it in Space Settings → Secrets" | |
| def create_or_update_file(repo_id: str, path_in_repo: str, content: str): | |
| if not HF_TOKEN: | |
| return {"status": "error", "message": "HF_TOKEN not found. Add it in Space Secrets."} | |
| if not repo_id or not path_in_repo or not content: | |
| return {"status": "error", "message": "Please fill all fields"} | |
| try: | |
| api = HfApi(token=HF_TOKEN) | |
| api.upload_file( | |
| path_or_fileobj=content.encode("utf-8"), | |
| path_in_repo=path_in_repo, | |
| repo_id=repo_id, | |
| commit_message="Updated via NEXUS HF Bridge" | |
| ) | |
| return {"status": "success", "message": f"File '{path_in_repo}' updated successfully"} | |
| except Exception as e: | |
| return {"status": "error", "message": str(e)} | |
| def list_repo_files(repo_id: str): | |
| if not HF_TOKEN: | |
| return {"status": "error", "message": "HF_TOKEN not found"} | |
| if not repo_id: | |
| return {"status": "error", "message": "Please provide repository ID"} | |
| try: | |
| api = HfApi(token=HF_TOKEN) | |
| files = api.list_repo_files(repo_id=repo_id) | |
| return {"status": "success", "files": files} | |
| except Exception as e: | |
| return {"status": "error", "message": str(e)} | |
| # ===================== UI ===================== | |
| with gr.Blocks(title="NEXUS Visual Weaver") as demo: | |
| gr.Markdown("# NEXUS Visual Weaver - HF MCP Bridge") | |
| gr.Markdown("**Using Space Secrets (secure)**") | |
| with gr.Tab("Repository Tools"): | |
| repo_id = gr.Textbox(label="Repository ID", placeholder="username/model-name") | |
| path = gr.Textbox(label="Path in Repo", placeholder="folder/file.txt") | |
| content = gr.Textbox(label="Content", lines=6) | |
| with gr.Row(): | |
| write_btn = gr.Button("Create / Update File", variant="primary") | |
| list_btn = gr.Button("List Files") | |
| result = gr.JSON(label="Result") | |
| write_btn.click(create_or_update_file, inputs=[repo_id, path, content], outputs=result) | |
| list_btn.click(list_repo_files, inputs=[repo_id], outputs=result) | |
| with gr.Tab("Status"): | |
| gr.Button("Check Token Status").click(get_status, outputs=gr.Textbox()) | |
| demo.launch(mcp_server=True) |