drdata commited on
Commit
2caf1d1
Β·
verified Β·
1 Parent(s): 074e23e

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +29 -162
app.py CHANGED
@@ -8,98 +8,9 @@ import importlib.util
8
  from dotenv import load_dotenv
9
  load_dotenv()
10
 
11
- def ensure_root_directories(main_workspace: Path) -> None:
12
- """Create required root-level folders and subfolders before any symlinks.
13
-
14
- Folders: Generated/, static/{css,js,images}/, view_session/
15
- Also drops a .gitkeep in each to ensure they can be tracked in a repo.
16
- """
17
- print("πŸ—οΈ Creating root-level directories (before symlinking)...")
18
- required = [
19
- main_workspace / "Generated",
20
- main_workspace / "static",
21
- main_workspace / "static" / "css",
22
- main_workspace / "static" / "js",
23
- main_workspace / "static" / "images",
24
- main_workspace / "view_session",
25
- ]
26
-
27
- for d in required:
28
- try:
29
- # If a symlink exists at the path, replace it with a real directory
30
- if d.is_symlink():
31
- try:
32
- d.unlink()
33
- print(f"πŸ” Replaced symlink with real directory: {d}")
34
- except Exception as e:
35
- print(f"⚠️ Could not remove symlink {d}: {e}")
36
- # Ensure directory exists
37
- d.mkdir(parents=True, exist_ok=True)
38
- try:
39
- d.chmod(0o755)
40
- except Exception:
41
- pass
42
- # Add README.md so directories are tracked per current .gitignore rules
43
- try:
44
- readme = d / "README.md"
45
- if not readme.exists():
46
- rel = d.relative_to(main_workspace)
47
- readme.write_text(f"# {rel}\n\nPlaceholder to ensure this directory is tracked.\n")
48
- except Exception:
49
- pass
50
- print(f"βœ… Ensured directory: {d}")
51
- except Exception as e:
52
- print(f"⚠️ Warning: could not ensure {d}: {e}")
53
-
54
- def ensure_view_session_points_to_generated(main_workspace: Path) -> None:
55
- """Ensure root-level view_session is a symlink to Generated (so UI views read from Generated)."""
56
- vs = main_workspace / "view_session"
57
- gen = main_workspace / "Generated"
58
- try:
59
- gen.mkdir(parents=True, exist_ok=True)
60
- if vs.exists() or vs.is_symlink():
61
- try:
62
- if vs.is_symlink() and vs.resolve() == gen.resolve():
63
- print(f"πŸ”— Root symlink already set: {vs} -> {gen}")
64
- return
65
- except Exception:
66
- pass
67
- if vs.is_symlink():
68
- vs.unlink()
69
- elif vs.is_dir():
70
- # migrate contents into Generated
71
- for item in vs.iterdir():
72
- dest = gen / item.name
73
- try:
74
- if item.is_dir():
75
- if dest.exists():
76
- for sub in item.iterdir():
77
- sub.rename(dest / sub.name)
78
- item.rmdir()
79
- else:
80
- item.rename(dest)
81
- else:
82
- if dest.exists():
83
- dest.unlink()
84
- item.rename(dest)
85
- except Exception as me:
86
- print(f"⚠️ Root migration warning for {item}: {me}")
87
- try:
88
- vs.rmdir()
89
- except Exception:
90
- pass
91
- else:
92
- vs.unlink()
93
- # create symlink view_session -> Generated
94
- vs.symlink_to(gen, target_is_directory=True)
95
- print(f"πŸ”— Root linked {vs} -> {gen}")
96
- except Exception as e:
97
- print(f"⚠️ Could not link root view_session to Generated: {e}")
98
-
99
  def create_symlinks_to_real_folders(cache_dir):
100
  """Create symbolic links in cache directory to real folders in main workspace."""
101
- # Use the directory containing this script as root workspace
102
- main_workspace = Path(__file__).parent.resolve()
103
 
104
  # Define the folders that need symbolic links
105
  folders_to_link = ["Generated", "static", "view_session"]
@@ -108,58 +19,24 @@ def create_symlinks_to_real_folders(cache_dir):
108
 
109
  for folder_name in folders_to_link:
110
  # Real folder path in main workspace
111
- # Map view_session to Generated so the UI reads sessions from Generated
112
- real_folder = (main_workspace / "Generated") if folder_name == "view_session" else (main_workspace / folder_name)
113
  # Symbolic link path in cache
114
  link_path = cache_dir / folder_name
115
 
116
  try:
117
- # Ensure real folder exists (defensive)
118
- real_folder.mkdir(parents=True, exist_ok=True)
119
-
120
- # If an entry exists at the link path, handle safely
121
  if link_path.exists() or link_path.is_symlink():
122
  if link_path.is_symlink():
123
- try:
124
- # If already correct, keep it
125
- if link_path.resolve() == real_folder.resolve():
126
- print(f"βœ… Symlink already set: {link_path} -> {real_folder}")
127
- continue
128
- except Exception:
129
- pass
130
  link_path.unlink()
131
  elif link_path.is_dir():
132
- # Migrate existing contents to the real folder before replacing
133
- for item in link_path.iterdir():
134
- dest = real_folder / item.name
135
- try:
136
- if item.is_dir():
137
- if dest.exists():
138
- # Merge directories
139
- for sub in item.iterdir():
140
- sub.rename(dest / sub.name)
141
- item.rmdir()
142
- else:
143
- item.rename(dest)
144
- else:
145
- if dest.exists():
146
- dest.unlink()
147
- item.rename(dest)
148
- except Exception as me:
149
- print(f"⚠️ Migration warning for {item}: {me}")
150
- # Remove the now-empty directory
151
- try:
152
- link_path.rmdir()
153
- except Exception:
154
- pass
155
  else:
156
- # Regular file – remove so we can place a symlink
157
  link_path.unlink()
158
-
159
  # Create symbolic link
160
  link_path.symlink_to(real_folder, target_is_directory=True)
161
  print(f"βœ… Created symlink: {link_path} -> {real_folder}")
162
-
163
  except Exception as e:
164
  print(f"⚠️ Warning: Could not create symlink for {folder_name}: {e}")
165
  # Fallback: create empty directory
@@ -169,32 +46,6 @@ def create_symlinks_to_real_folders(cache_dir):
169
  print("🎯 All symbolic links are ready!")
170
  return True
171
 
172
- def verify_cache_symlink(cache_dir: Path, name: str, root_dir: Path) -> bool:
173
- """Verify a specific symlink in cache points to the corresponding root dir and is writable."""
174
- link = cache_dir / name
175
- target = root_dir / name
176
- try:
177
- if not link.exists():
178
- print(f"❌ Missing link/path in cache: {link}")
179
- return False
180
- if not link.is_symlink():
181
- print(f"⚠️ Cache path is not a symlink (still usable): {link}")
182
- else:
183
- if link.resolve() != target.resolve():
184
- print(f"⚠️ Symlink target mismatch: {link.resolve()} != {target}")
185
- else:
186
- print(f"πŸ”— Verified symlink: {link} -> {target}")
187
- # Write/read test
188
- tf = link / ".__verify__"
189
- tf.write_text("ok")
190
- assert (target / ".__verify__").read_text() == "ok"
191
- tf.unlink(missing_ok=True)
192
- print(f"βœ… Write/Read OK via cache link: {link}")
193
- return True
194
- except Exception as e:
195
- print(f"❌ Verification failed for {link}: {e}")
196
- return False
197
-
198
  def modify_byteplus_for_directories(cache_dir):
199
  """Modify the BytePlus app to ensure proper directory handling."""
200
  app_file = cache_dir / "app.py"
@@ -386,6 +237,28 @@ def download_space(cache_dir):
386
  token = os.environ.get("HF_TOKEN")
387
  repo_id = os.environ.get("REPO_ID")
388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
  if not token or not repo_id:
390
  print("❌ HF_TOKEN or REPO_ID not found in environment variables")
391
  return False
@@ -478,17 +351,11 @@ def load_app(cache_dir):
478
  )
479
 
480
  if __name__ == "__main__":
481
- print("πŸš€ Setting up BytePlus Image Generation Studio with root directories + symlinks...")
482
 
483
  # Setup cache directory
484
  cache_dir = setup_cache_directory()
485
  print(f"πŸ“ Cache directory: {cache_dir}")
486
-
487
- # Ensure root directories exist first (as requested)
488
- root = Path(__file__).parent.resolve()
489
- ensure_root_directories(root)
490
- # Ensure view_session at root points to Generated for History/View/ZIP
491
- ensure_view_session_points_to_generated(root)
492
 
493
  # Download the space
494
  if download_space(cache_dir):
 
8
  from dotenv import load_dotenv
9
  load_dotenv()
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def create_symlinks_to_real_folders(cache_dir):
12
  """Create symbolic links in cache directory to real folders in main workspace."""
13
+ main_workspace = Path("/Users/data/SGS-1")
 
14
 
15
  # Define the folders that need symbolic links
16
  folders_to_link = ["Generated", "static", "view_session"]
 
19
 
20
  for folder_name in folders_to_link:
21
  # Real folder path in main workspace
22
+ real_folder = main_workspace / folder_name
 
23
  # Symbolic link path in cache
24
  link_path = cache_dir / folder_name
25
 
26
  try:
27
+ # Remove existing file/folder/link if it exists
 
 
 
28
  if link_path.exists() or link_path.is_symlink():
29
  if link_path.is_symlink():
 
 
 
 
 
 
 
30
  link_path.unlink()
31
  elif link_path.is_dir():
32
+ shutil.rmtree(link_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  else:
 
34
  link_path.unlink()
35
+
36
  # Create symbolic link
37
  link_path.symlink_to(real_folder, target_is_directory=True)
38
  print(f"βœ… Created symlink: {link_path} -> {real_folder}")
39
+
40
  except Exception as e:
41
  print(f"⚠️ Warning: Could not create symlink for {folder_name}: {e}")
42
  # Fallback: create empty directory
 
46
  print("🎯 All symbolic links are ready!")
47
  return True
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def modify_byteplus_for_directories(cache_dir):
50
  """Modify the BytePlus app to ensure proper directory handling."""
51
  app_file = cache_dir / "app.py"
 
237
  token = os.environ.get("HF_TOKEN")
238
  repo_id = os.environ.get("REPO_ID")
239
 
240
+ if not token or not repo_id:
241
+ print("❌ HF_TOKEN or REPO_ID not found in environment variables")
242
+ return False
243
+
244
+ try:
245
+ print(f"πŸ“₯ Downloading BytePlus space: {repo_id}")
246
+ snapshot_download(
247
+ repo_id=repo_id,
248
+ repo_type="space",
249
+ local_dir=cache_dir,
250
+ token=token
251
+ )
252
+ print("βœ… Successfully downloaded BytePlus space")
253
+ return True
254
+ except Exception as e:
255
+ print(f"❌ Error downloading space: {e}")
256
+ return False
257
+ def download_space(cache_dir):
258
+ """Download the BytePlus space from HuggingFace."""
259
+ token = os.environ.get("HF_TOKEN")
260
+ repo_id = os.environ.get("REPO_ID")
261
+
262
  if not token or not repo_id:
263
  print("❌ HF_TOKEN or REPO_ID not found in environment variables")
264
  return False
 
351
  )
352
 
353
  if __name__ == "__main__":
354
+ print("πŸš€ Setting up BytePlus Image Generation Studio with symbolic links...")
355
 
356
  # Setup cache directory
357
  cache_dir = setup_cache_directory()
358
  print(f"πŸ“ Cache directory: {cache_dir}")
 
 
 
 
 
 
359
 
360
  # Download the space
361
  if download_space(cache_dir):