import os import io from huggingface_hub import HfApi from huggingface_hub import create_repo, upload_folder, whoami, upload_file def deploy_workflow( name: str, app_code: str, requirements_file: str, hf_access_token: str, ): print("Syncing with Hugging Face Spaces...") api = HfApi() repo_id = f"{whoami(token=hf_access_token)['name']}/{name}" print("repo_id", repo_id) url = create_repo( repo_id, token=hf_access_token, exist_ok=True, repo_type='space', space_sdk='gradio', private=False, space_hardware="zero-a10g", ) api.add_space_secret(repo_id=repo_id, key="HF_TOKEN", value=hf_access_token, token=hf_access_token) # # Sync folder commit_url = upload_folder( folder_path='.', repo_id=repo_id, repo_type='space', token=hf_access_token, commit_message="Synced repo using 'sync_with_huggingface' Github Action", ignore_patterns=["*.git*", "*README.md*", "*env*", '*.cache*', '*ComfyUI-Manager*', '*ComfyUI-to-Python-Extension*', 'models/*'], ) upload_file( path_or_fileobj=io.BytesIO(requirements_file.encode("utf-8")), path_in_repo='requirements.txt', repo_id=repo_id, repo_type='space', token=hf_access_token, commit_message="Uploaded requirements.txt file", ) upload_file( path_or_fileobj=io.BytesIO(app_code.encode("utf-8")), path_in_repo='app.py', repo_id=repo_id, repo_type='space', token=hf_access_token, commit_message="Uploaded app.py file", ) print(f"\t- Repo synced: {commit_url}") return { "url": url, "repo_id": repo_id, "commit_url": commit_url }