File size: 5,970 Bytes
059e0d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""

Deploy Byte Dream to Hugging Face Spaces

Automated deployment script for creating and updating Spaces

"""

import argparse
from pathlib import Path
import shutil
import tempfile
from huggingface_hub import HfApi, create_repo
import os


def deploy_to_spaces(

    repo_id: str,

    space_title: str = "Byte Dream - AI Image Generator",

    token: str = None,

    private: bool = False,

    hardware: str = "cpu-basic",

):
    """

    Deploy Byte Dream to Hugging Face Spaces

    

    Args:

        repo_id: Space repository ID (username/SpaceName)

        space_title: Title for the Space

        token: Hugging Face API token

        private: Whether to make Space private

        hardware: Hardware tier (cpu-basic, cpu-upgrade, etc.)

    """
    api = HfApi()
    
    print("=" * 60)
    print("Byte Dream - Hugging Face Spaces Deployment")
    print("=" * 60)
    
    # Create space if it doesn't exist
    try:
        create_repo(
            repo_id=repo_id,
            token=token,
            repo_type="space",
            space_sdk="gradio",
            space_hardware=hardware,
            private=private,
            exist_ok=True,
        )
        print(f"โœ“ Space created/verified: {repo_id}")
    except Exception as e:
        print(f"Error creating space: {e}")
        return
    
    # Prepare files to upload
    with tempfile.TemporaryDirectory() as tmp_dir:
        tmp_path = Path(tmp_dir)
        
        # Copy essential files
        files_to_copy = [
            "app.py",
            "config.yaml",
            "requirements.txt",
        ]
        
        for file in files_to_copy:
            src = Path(file)
            if src.exists():
                shutil.copy2(src, tmp_path / file)
                print(f"โœ“ Copied {file}")
        
        # Copy bytedream package
        bytedream_src = Path("bytedream")
        bytedream_dst = tmp_path / "bytedream"
        if bytedream_src.exists():
            shutil.copytree(bytedream_src, bytedream_dst)
            print("โœ“ Copied bytedream package")
        
        # Copy spaces app template
        spaces_app = Path("spaces_app.py")
        if spaces_app.exists():
            # Rename to README.md for the space
            shutil.copy2(spaces_app, tmp_path / "README.md")
            print("โœ“ Copied README.md")
        
        # Create .gitignore for space
        gitignore_content = """

__pycache__/

*.pyc

.env

*.log

outputs/

models/

        """
        (tmp_path / ".gitignore").write_text(gitignore_content)
        print("โœ“ Created .gitignore")
        
        # Upload to space
        print("\nUploading files to Hugging Face Spaces...")
        try:
            api.upload_folder(
                folder_path=str(tmp_path),
                repo_id=repo_id,
                repo_type="space",
                token=token,
                commit_message="Deploy Byte Dream application",
            )
            print("โœ“ Files uploaded successfully!")
            
            # Get space URL
            space_url = f"https://huggingface.co/spaces/{repo_id}"
            print(f"\n๐Ÿš€ Your Space is deployed at:")
            print(f"{space_url}")
            print(f"\nNote: It may take a few minutes for the Space to build and become available.")
            print(f"Hardware: {hardware}")
            
        except Exception as e:
            print(f"Error uploading to space: {e}")
            raise


def update_space(repo_id: str, files: list, token: str = None):
    """

    Update specific files in an existing Space

    

    Args:

        repo_id: Space repository ID

        files: List of files to update

        token: Hugging Face API token

    """
    api = HfApi()
    
    print(f"Updating files in space: {repo_id}")
    
    for file_path in files:
        if Path(file_path).exists():
            api.upload_file(
                path_or_fileobj=file_path,
                path_in_repo=Path(file_path).name,
                repo_id=repo_id,
                repo_type="space",
                token=token,
            )
            print(f"โœ“ Updated {file_path}")
        else:
            print(f"โš  File not found: {file_path}")


def main():
    parser = argparse.ArgumentParser(description="Deploy Byte Dream to Hugging Face Spaces")
    
    parser.add_argument(
        "--repo_id",
        type=str,
        required=True,
        help="Space repository ID (e.g., username/ByteDream-Space)"
    )
    
    parser.add_argument(
        "--token",
        type=str,
        default=None,
        help="Hugging Face API token (optional, will use cached token if not provided)"
    )
    
    parser.add_argument(
        "--title",
        type=str,
        default="Byte Dream - AI Image Generator",
        help="Title for the Space"
    )
    
    parser.add_argument(
        "--private",
        action="store_true",
        help="Make the Space private"
    )
    
    parser.add_argument(
        "--hardware",
        type=str,
        default="cpu-basic",
        choices=["cpu-basic", "cpu-upgrade", "cpu-xl", "zero-a10g", "t4-small", "t4-medium", "a10g-small", "a10g-large"],
        help="Hardware tier for the Space"
    )
    
    parser.add_argument(
        "--update",
        nargs="+",
        help="Update specific files instead of full deployment"
    )
    
    args = parser.parse_args()
    
    if args.update:
        # Update specific files
        update_space(args.repo_id, args.update, args.token)
    else:
        # Full deployment
        deploy_to_spaces(
            repo_id=args.repo_id,
            space_title=args.title,
            token=args.token,
            private=args.private,
            hardware=args.hardware,
        )


if __name__ == "__main__":
    main()