import gradio as gr from huggingface_hub import HfApi from git import Repo from slugify import slugify import uuid import os import re def extract_from_readme(readme_path): title = None description = None port = None if os.path.exists(readme_path): with open(readme_path, encoding="utf-8") as f: content = f.read() match_title = re.search(r'title:\s*(.*)', content) match_desc = re.search(r'short_description:\s*(.*)', content) match_port = re.search(r'app_port:\s*(\d+)', content) if match_title: title = match_title.group(1).strip() if match_desc: description = match_desc.group(1).strip() if match_port: port = match_port.group(1).strip() return title, description, port def clone_and_create(profile: gr.OAuthProfile, token: gr.OAuthToken, repo_url, repo_type, private, space_sdk, sdk_version, space_id, space_title, space_description): folder = str(uuid.uuid4()) Repo.clone_from(repo_url, folder) hf_api = HfApi(token=token.token) # 自動補完 title_from_readme, desc_from_readme, port_from_readme = extract_from_readme(os.path.join(folder, "README.md")) final_id = slugify(space_id) if space_id else slugify(repo_url.split("/")[-1]) final_title = space_title or title_from_readme or final_id final_description = space_description or desc_from_readme repo_visibility = "private" if private else "public" # スペースの場合 if repo_type == "space": hf_api.create_repo( repo_id=f"{profile.username}/{final_id}", repo_type="space", space_sdk=space_sdk, space_sdk_version=sdk_version if sdk_version else None, private=private ) else: hf_api.create_repo( repo_id=f"{profile.username}/{final_id}", repo_type=repo_type, private=private ) # README.md に補完情報追加(必要なら) readme_path = os.path.join(folder, "README.md") with open(readme_path, "a", encoding="utf-8") as f: if space_title and not title_from_readme: f.write(f"\ntitle: {space_title}") if space_description and not desc_from_readme: f.write(f"\nshort_description: {space_description}") if port_from_readme: f.write(f"\napp_port: {port_from_readme}") # アップロード hf_api.upload_folder( folder_path=folder, repo_id=f"{profile.username}/{final_id}", repo_type=repo_type ) # URL return f"https://huggingface.co/{repo_type + 's' if repo_type != 'model' else 'models'}/{profile.username}/{final_id}" with gr.Blocks() as demo: gr.Markdown("# 🚀 Gitリポジトリ → Hugging Face クローンツール") gr.LoginButton() with gr.Row(): with gr.Column(): repo_url = gr.Textbox(label="GitリポジトリURL(GitHub等)") repo_type = gr.Dropdown(choices=["space", "dataset", "model"], value="space", label="Hugging Faceの種類") private = gr.Checkbox(label="非公開にする", value=False) space_sdk = gr.Dropdown(choices=["gradio", "streamlit", "docker", "static"], label="スペースSDK", visible=True) sdk_version = gr.Textbox(label="SDKバージョン(任意)", visible=True) space_id = gr.Textbox(label="Hugging Face ID(空欄なら自動)") space_title = gr.Textbox(label="スペースのタイトル(空欄ならREADMEやIDから)") space_description = gr.Textbox(label="スペースの説明(空欄ならREADMEの内容を使用)") with gr.Column(): output = gr.Textbox(label="作成されたHugging Face URL") def toggle_space_fields(repo_type_val): visible = repo_type_val == "space" return { space_sdk: gr.update(visible=visible), sdk_version: gr.update(visible=visible), space_id: gr.update(visible=True), space_title: gr.update(visible=True), space_description: gr.update(visible=True), } repo_type.change(fn=toggle_space_fields, inputs=repo_type, outputs=[space_sdk, sdk_version, space_id, space_title, space_description]) btn = gr.Button("クローンして作成!") btn.click( fn=clone_and_create, inputs=[repo_url, repo_type, private, space_sdk, sdk_version, space_id, space_title, space_description], outputs=output ) if __name__ == "__main__": demo.launch()