Spaces:
Sleeping
Sleeping
| import os | |
| import tempfile | |
| import shutil | |
| import subprocess | |
| import gradio as gr | |
| from huggingface_hub import snapshot_download | |
| from git import Repo | |
| def transfer_repo( | |
| profile: gr.OAuthProfile, | |
| token: gr.OAuthToken, | |
| repo_type, | |
| repo_id, | |
| github_repo_url, | |
| github_token, | |
| ): | |
| temp_dir = tempfile.mkdtemp() | |
| try: | |
| # ----------------------------- | |
| # 1️⃣ Hugging Face から取得 | |
| # ----------------------------- | |
| snapshot_download( | |
| repo_id=repo_id, # 例: username/repo | |
| repo_type=repo_type, # model / dataset / space | |
| local_dir=temp_dir, | |
| local_dir_use_symlinks=False, | |
| token=token.token # 🔐 OAuth token | |
| ) | |
| print("GIT:", github_repo_url) | |
| # ----------------------------- | |
| # 2️⃣ GitHub URL に token 埋め込み | |
| # ----------------------------- | |
| if github_repo_url.startswith("https://"): | |
| github_repo_url = github_repo_url.replace( | |
| "https://", | |
| f"https://{github_token}@" | |
| ) | |
| # ----------------------------- | |
| # 3️⃣ Git 初期化 | |
| # ----------------------------- | |
| repo = Repo.init(temp_dir) | |
| # main ブランチ作成 | |
| repo.git.checkout(b="main") | |
| repo.git.add(A=True) | |
| repo.index.commit("Initial commit from Hugging Face") | |
| origin = repo.create_remote("origin", github_repo_url) | |
| # push(main) | |
| origin.push(refspec="main:main", force=True) | |
| print("GH:", github_token) | |
| hf_token_value = token.token if hasattr(token, "token") else token | |
| print("HF:", hf_token_value) | |
| print("REP:", repo_id) | |
| return "✅ 転送完了しました!" | |
| except Exception as e: | |
| return f"❌ エラーが発生しました: {str(e)}" | |
| finally: | |
| shutil.rmtree(temp_dir, ignore_errors=True) | |
| # ----------------------------- | |
| # Gradio UI | |
| # ----------------------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🚀 Hugging Face → GitHub 転送ツール") | |
| # 🔐 Hugging Face ログイン | |
| gr.LoginButton() | |
| repo_type = gr.Radio( | |
| ["model", "dataset", "space"], | |
| label="Hugging Face リポジトリ種別", | |
| value="dataset" | |
| ) | |
| repo_id = gr.Textbox( | |
| label="Hugging Face Repo ID (例: username/repo)" | |
| ) | |
| github_repo_url = gr.Textbox( | |
| label="GitHub リポジトリURL (https://github.com/username/repo.git)" | |
| ) | |
| github_token = gr.Textbox( | |
| label="GitHub Personal Access Token", | |
| type="password" | |
| ) | |
| output = gr.Textbox(label="結果") | |
| transfer_button = gr.Button("🚀 転送開始") | |
| transfer_button.click( | |
| transfer_repo, | |
| inputs=[ | |
| repo_type, | |
| repo_id, | |
| github_repo_url, | |
| github_token, | |
| ], | |
| outputs=output | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |