Enzo8930302 commited on
Commit
059e0d4
·
verified ·
1 Parent(s): 8f3291e

Upload deploy_to_spaces.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. deploy_to_spaces.py +208 -0
deploy_to_spaces.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Deploy Byte Dream to Hugging Face Spaces
3
+ Automated deployment script for creating and updating Spaces
4
+ """
5
+
6
+ import argparse
7
+ from pathlib import Path
8
+ import shutil
9
+ import tempfile
10
+ from huggingface_hub import HfApi, create_repo
11
+ import os
12
+
13
+
14
+ def deploy_to_spaces(
15
+ repo_id: str,
16
+ space_title: str = "Byte Dream - AI Image Generator",
17
+ token: str = None,
18
+ private: bool = False,
19
+ hardware: str = "cpu-basic",
20
+ ):
21
+ """
22
+ Deploy Byte Dream to Hugging Face Spaces
23
+
24
+ Args:
25
+ repo_id: Space repository ID (username/SpaceName)
26
+ space_title: Title for the Space
27
+ token: Hugging Face API token
28
+ private: Whether to make Space private
29
+ hardware: Hardware tier (cpu-basic, cpu-upgrade, etc.)
30
+ """
31
+ api = HfApi()
32
+
33
+ print("=" * 60)
34
+ print("Byte Dream - Hugging Face Spaces Deployment")
35
+ print("=" * 60)
36
+
37
+ # Create space if it doesn't exist
38
+ try:
39
+ create_repo(
40
+ repo_id=repo_id,
41
+ token=token,
42
+ repo_type="space",
43
+ space_sdk="gradio",
44
+ space_hardware=hardware,
45
+ private=private,
46
+ exist_ok=True,
47
+ )
48
+ print(f"✓ Space created/verified: {repo_id}")
49
+ except Exception as e:
50
+ print(f"Error creating space: {e}")
51
+ return
52
+
53
+ # Prepare files to upload
54
+ with tempfile.TemporaryDirectory() as tmp_dir:
55
+ tmp_path = Path(tmp_dir)
56
+
57
+ # Copy essential files
58
+ files_to_copy = [
59
+ "app.py",
60
+ "config.yaml",
61
+ "requirements.txt",
62
+ ]
63
+
64
+ for file in files_to_copy:
65
+ src = Path(file)
66
+ if src.exists():
67
+ shutil.copy2(src, tmp_path / file)
68
+ print(f"✓ Copied {file}")
69
+
70
+ # Copy bytedream package
71
+ bytedream_src = Path("bytedream")
72
+ bytedream_dst = tmp_path / "bytedream"
73
+ if bytedream_src.exists():
74
+ shutil.copytree(bytedream_src, bytedream_dst)
75
+ print("✓ Copied bytedream package")
76
+
77
+ # Copy spaces app template
78
+ spaces_app = Path("spaces_app.py")
79
+ if spaces_app.exists():
80
+ # Rename to README.md for the space
81
+ shutil.copy2(spaces_app, tmp_path / "README.md")
82
+ print("✓ Copied README.md")
83
+
84
+ # Create .gitignore for space
85
+ gitignore_content = """
86
+ __pycache__/
87
+ *.pyc
88
+ .env
89
+ *.log
90
+ outputs/
91
+ models/
92
+ """
93
+ (tmp_path / ".gitignore").write_text(gitignore_content)
94
+ print("✓ Created .gitignore")
95
+
96
+ # Upload to space
97
+ print("\nUploading files to Hugging Face Spaces...")
98
+ try:
99
+ api.upload_folder(
100
+ folder_path=str(tmp_path),
101
+ repo_id=repo_id,
102
+ repo_type="space",
103
+ token=token,
104
+ commit_message="Deploy Byte Dream application",
105
+ )
106
+ print("✓ Files uploaded successfully!")
107
+
108
+ # Get space URL
109
+ space_url = f"https://huggingface.co/spaces/{repo_id}"
110
+ print(f"\n🚀 Your Space is deployed at:")
111
+ print(f"{space_url}")
112
+ print(f"\nNote: It may take a few minutes for the Space to build and become available.")
113
+ print(f"Hardware: {hardware}")
114
+
115
+ except Exception as e:
116
+ print(f"Error uploading to space: {e}")
117
+ raise
118
+
119
+
120
+ def update_space(repo_id: str, files: list, token: str = None):
121
+ """
122
+ Update specific files in an existing Space
123
+
124
+ Args:
125
+ repo_id: Space repository ID
126
+ files: List of files to update
127
+ token: Hugging Face API token
128
+ """
129
+ api = HfApi()
130
+
131
+ print(f"Updating files in space: {repo_id}")
132
+
133
+ for file_path in files:
134
+ if Path(file_path).exists():
135
+ api.upload_file(
136
+ path_or_fileobj=file_path,
137
+ path_in_repo=Path(file_path).name,
138
+ repo_id=repo_id,
139
+ repo_type="space",
140
+ token=token,
141
+ )
142
+ print(f"✓ Updated {file_path}")
143
+ else:
144
+ print(f"⚠ File not found: {file_path}")
145
+
146
+
147
+ def main():
148
+ parser = argparse.ArgumentParser(description="Deploy Byte Dream to Hugging Face Spaces")
149
+
150
+ parser.add_argument(
151
+ "--repo_id",
152
+ type=str,
153
+ required=True,
154
+ help="Space repository ID (e.g., username/ByteDream-Space)"
155
+ )
156
+
157
+ parser.add_argument(
158
+ "--token",
159
+ type=str,
160
+ default=None,
161
+ help="Hugging Face API token (optional, will use cached token if not provided)"
162
+ )
163
+
164
+ parser.add_argument(
165
+ "--title",
166
+ type=str,
167
+ default="Byte Dream - AI Image Generator",
168
+ help="Title for the Space"
169
+ )
170
+
171
+ parser.add_argument(
172
+ "--private",
173
+ action="store_true",
174
+ help="Make the Space private"
175
+ )
176
+
177
+ parser.add_argument(
178
+ "--hardware",
179
+ type=str,
180
+ default="cpu-basic",
181
+ choices=["cpu-basic", "cpu-upgrade", "cpu-xl", "zero-a10g", "t4-small", "t4-medium", "a10g-small", "a10g-large"],
182
+ help="Hardware tier for the Space"
183
+ )
184
+
185
+ parser.add_argument(
186
+ "--update",
187
+ nargs="+",
188
+ help="Update specific files instead of full deployment"
189
+ )
190
+
191
+ args = parser.parse_args()
192
+
193
+ if args.update:
194
+ # Update specific files
195
+ update_space(args.repo_id, args.update, args.token)
196
+ else:
197
+ # Full deployment
198
+ deploy_to_spaces(
199
+ repo_id=args.repo_id,
200
+ space_title=args.title,
201
+ token=args.token,
202
+ private=args.private,
203
+ hardware=args.hardware,
204
+ )
205
+
206
+
207
+ if __name__ == "__main__":
208
+ main()