import gradio as gr import os, shutil, tempfile from huggingface_hub import HfApi, whoami from git import Repo api = HfApi() def create_from_repo(oauth_token: gr.OAuthToken | None, git_url, repo_type, private, space_id, port, title, description): if oauth_token is None: return "⚠️ Hugging Face にログインしてください。" token = oauth_token.token if not git_url.strip(): return "Git リポジトリの URL を入力してください。" temp_dir = tempfile.mkdtemp() try: Repo.clone_from(git_url, temp_dir) if not space_id.strip(): space_id = git_url.rstrip("/").split("/")[-1].replace(".git","") user = whoami(token=token) repo_id = f"{user['name']}/{space_id}" api.create_repo(repo_id=repo_id, repo_type=repo_type, private=private, token=token, exist_ok=False) if repo_type == "space": readme = [] if title.strip(): readme.append(f"# {title.strip()}") if description.strip(): readme.append(f"\n{description.strip()}") if port.strip(): readme.append(f"\napp_port: {port.strip()}") if readme: with open(os.path.join(temp_dir,"README.md"),"w",encoding="utf‑8") as f: f.write("\n".join(readme)) api.upload_folder(folder_path=temp_dir, repo_id=repo_id, repo_type=repo_type, token=token) return f"✅ {repo_type} を作成しました: https://huggingface.co/{repo_id}" except Exception as e: return f"❌ エラー: {e}" finally: shutil.rmtree(temp_dir) with gr.Blocks() as demo: gr.Markdown("## Hugging Face に Space / Dataset / Model を作成") login = gr.LoginButton() with gr.Column(): git_url = gr.Textbox(label="Git リポジトリ URL", placeholder="https://...") repo_type = gr.Radio(["space","dataset","model"], label="タイプ", value="space") private = gr.Checkbox(label="非公開にする", value=False) space_id = gr.Textbox(label="Space ID(空欄で自動生成)", visible=True) port = gr.Textbox(label="起動ポート(README)", visible=True) title = gr.Textbox(label="スペース名", visible=True) description = gr.Textbox(label="説明文", visible=True) output = gr.Textbox(label="出力", lines=4) def toggle_spaceopts(rtype): vis = (rtype=="space") return {space_id: gr.update(visible=vis), port: gr.update(visible=vis), title: gr.update(visible=vis), description: gr.update(visible=vis)} repo_type.change(toggle_spaceopts, inputs=repo_type, outputs=[space_id, port, title, description]) login.click(create_from_repo, inputs=[gr.OAuthToken(), git_url, repo_type, private, space_id, port, title, description], outputs=output) demo.launch()