drdata commited on
Commit
9a7a411
·
verified ·
1 Parent(s): 40dafca

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. SYMBOLIC_LINK_SOLUTION.md +62 -0
  2. app.py +121 -2
  3. app_security.log +9 -0
  4. hf_deploy/launcher.py +269 -0
SYMBOLIC_LINK_SOLUTION.md ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ✅ SYMBOLIC LINK SOLUTION IMPLEMENTED
2
+
3
+ ## Solution Overview
4
+ Your `app.py` has been successfully updated to use the symbolic link method with proper access permissions for the `Generated/`, `static/`, and `view_session/` folders.
5
+
6
+ ## How It Works
7
+
8
+ ### Directory Structure:
9
+ - **Root Directory**: Contains symbolic links (`Generated -> .cache/Generated`)
10
+ - **Cache Directory**: Contains real directories with proper permissions (755)
11
+ - **BytePlus App**: Runs from cache but accesses directories via symlinks
12
+
13
+ ### Flow:
14
+ 1. `app.py` creates directories with proper permissions (755)
15
+ 2. Downloads BytePlus space to `.cache/`
16
+ 3. Creates real directories in `.cache/` with 755 permissions
17
+ 4. Creates symbolic links from root to cache directories
18
+ 5. BytePlus app can access directories through either path
19
+
20
+ ### Permissions:
21
+ - **Cache Directory**: Restrictive (700) for security
22
+ - **Target Directories**: Accessible (755) for functionality
23
+ - **Files**: Standard (644) for proper access
24
+
25
+ ## Key Features
26
+
27
+ ### ✅ Automatic Setup
28
+ - No manual permission fixes needed
29
+ - Handles existing directories gracefully
30
+ - Moves content if directories already exist
31
+ - Creates proper symbolic link structure
32
+
33
+ ### ✅ Error Handling
34
+ - Fallback to real directories if symlinks fail
35
+ - Comprehensive error messages
36
+ - Graceful handling of permission issues
37
+
38
+ ### ✅ Cross-Platform Compatibility
39
+ - Works on macOS (your system)
40
+ - Handles different filesystem limitations
41
+ - Uses proper Python Path operations
42
+
43
+ ## Verification Results
44
+
45
+ ✅ **Directory Access**: `Generated`, `static`, `view_session` all accessible
46
+ ✅ **Write Permissions**: Can create/write files in directories
47
+ ✅ **Symbolic Links**: Properly created from root to cache
48
+ ✅ **App Launch**: BytePlus launches successfully with access to directories
49
+
50
+ ## Commands That Work:
51
+ ```bash
52
+ python app.py # Launches with proper directory setup
53
+ ls -la Generated # Shows accessible directory
54
+ touch Generated/test # Can write to directories
55
+ ```
56
+
57
+ ## File Updates:
58
+ - ✅ **app.py**: Updated with symbolic link creation functions
59
+ - ✅ **hf_deploy/launcher.py**: Deployment version available
60
+ - ✅ **Directory Structure**: Proper symlinks from root to `.cache/`
61
+
62
+ The solution ensures BytePlus has full access to the required directories while maintaining security through the hidden cache structure.
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import sys
 
3
  from pathlib import Path
4
  from huggingface_hub import snapshot_download
5
  import importlib.util
@@ -52,6 +53,101 @@ def setup_cache_directory():
52
  public_cache.mkdir(exist_ok=True)
53
  return public_cache
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  def download_space(cache_dir):
56
  token = os.environ.get("HF_TOKEN")
57
  repo_id = os.environ.get("REPO_ID")
@@ -144,7 +240,30 @@ def load_app(cache_dir):
144
  )
145
 
146
  if __name__ == "__main__":
 
 
 
 
 
 
147
  cache_dir = setup_cache_directory()
 
 
 
148
  if download_space(cache_dir):
149
- demo = load_app(cache_dir)
150
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import sys
3
+ import shutil
4
  from pathlib import Path
5
  from huggingface_hub import snapshot_download
6
  import importlib.util
 
53
  public_cache.mkdir(exist_ok=True)
54
  return public_cache
55
 
56
+ def create_symbolic_links(cache_dir):
57
+ """Create symbolic links for Generated, static, and view_session directories.
58
+
59
+ The strategy is to make sure we have real directories in cache and symbolic links from root to cache.
60
+ This ensures the BytePlus app can access these directories even when running from cache.
61
+ """
62
+ root_dir = Path.cwd()
63
+ directories_to_link = ["Generated", "static", "view_session"]
64
+
65
+ for dir_name in directories_to_link:
66
+ # Root directory path (should be symlink)
67
+ root_link = root_dir / dir_name
68
+ # Cache directory path (should be real directory)
69
+ cache_target = cache_dir / dir_name
70
+
71
+ try:
72
+ # Ensure we have a real directory in cache
73
+ if cache_target.is_symlink():
74
+ # Remove the symlink in cache if it exists
75
+ cache_target.unlink()
76
+ print(f"Removed cache symlink: {cache_target}")
77
+
78
+ if not cache_target.exists():
79
+ cache_target.mkdir(parents=True, exist_ok=True)
80
+ print(f"Created cache directory: {cache_target}")
81
+
82
+ # Set proper permissions on cache directory (755)
83
+ cache_target.chmod(0o755)
84
+ print(f"Set permissions 755 on: {cache_target}")
85
+
86
+ # Ensure root has symlink to cache (if it doesn't already exist)
87
+ if not root_link.is_symlink():
88
+ if root_link.exists():
89
+ # If it's a real directory, we might need to move content or just replace
90
+ if root_link.is_dir():
91
+ # Try to move content to cache first
92
+ try:
93
+ for item in root_link.iterdir():
94
+ dest = cache_target / item.name
95
+ if not dest.exists():
96
+ shutil.move(str(item), str(dest))
97
+ shutil.rmtree(root_link)
98
+ print(f"Moved content from {root_link} to {cache_target}")
99
+ except Exception as e:
100
+ print(f"Could not move content: {e}")
101
+ shutil.rmtree(root_link)
102
+ else:
103
+ root_link.unlink()
104
+
105
+ # Create the symlink from root to cache
106
+ root_link.symlink_to(cache_target)
107
+ print(f"Created root symlink: {root_link} -> {cache_target}")
108
+ else:
109
+ print(f"Root symlink already exists: {root_link}")
110
+
111
+ except Exception as e:
112
+ print(f"Error setting up links for {dir_name}: {e}")
113
+ # Fallback: ensure at least the cache directory exists
114
+ try:
115
+ if not cache_target.exists():
116
+ cache_target.mkdir(parents=True, exist_ok=True)
117
+ cache_target.chmod(0o755)
118
+ print(f"Created fallback cache directory: {cache_target}")
119
+ except Exception as fe:
120
+ print(f"Fallback failed for {dir_name}: {fe}")
121
+
122
+ def ensure_directory_permissions():
123
+ """Ensure proper permissions on key directories in the current working directory."""
124
+ directories_to_fix = ["Generated", "static", "view_session"]
125
+
126
+ for dir_name in directories_to_fix:
127
+ dir_path = Path(dir_name)
128
+ try:
129
+ if not dir_path.exists():
130
+ # Create directory if it doesn't exist
131
+ dir_path.mkdir(parents=True, exist_ok=True)
132
+ print(f"Created directory: {dir_path}")
133
+
134
+ # Set directory permissions to 755 (owner rwx, group rx, others rx)
135
+ dir_path.chmod(0o755)
136
+ print(f"Set permissions 755 on directory: {dir_path}")
137
+
138
+ # Set permissions on all subdirectories and files
139
+ for item in dir_path.rglob('*'):
140
+ try:
141
+ if item.is_dir():
142
+ item.chmod(0o755)
143
+ else:
144
+ item.chmod(0o644) # Files: owner rw, group r, others r
145
+ except Exception as e:
146
+ print(f"Warning: Could not set permissions on {item}: {e}")
147
+
148
+ except Exception as e:
149
+ print(f"Error ensuring permissions for {dir_name}: {e}")
150
+
151
  def download_space(cache_dir):
152
  token = os.environ.get("HF_TOKEN")
153
  repo_id = os.environ.get("REPO_ID")
 
240
  )
241
 
242
  if __name__ == "__main__":
243
+ print("Setting up BytePlus Image Generation Studio...")
244
+
245
+ # First ensure proper permissions on existing directories
246
+ ensure_directory_permissions()
247
+
248
+ # Setup cache directory
249
  cache_dir = setup_cache_directory()
250
+ print(f"Cache directory: {cache_dir}")
251
+
252
+ # Download the space
253
  if download_space(cache_dir):
254
+ print("Successfully downloaded BytePlus space")
255
+
256
+ # Create symbolic links for directory access
257
+ create_symbolic_links(cache_dir)
258
+
259
+ # Load and launch the app
260
+ try:
261
+ demo = load_app(cache_dir)
262
+ print("Launching BytePlus Image Generation Studio...")
263
+ demo.launch()
264
+ except Exception as e:
265
+ print(f"Error launching app: {e}")
266
+ sys.exit(1)
267
+ else:
268
+ print("Failed to download space. Please check your HF_TOKEN and REPO_ID environment variables.")
269
+ sys.exit(1)
app_security.log CHANGED
@@ -19,3 +19,12 @@
19
  2025-09-24 16:42:54,206 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events "HTTP/1.1 200 OK"
20
  2025-09-24 16:42:54,291 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ "HTTP/1.1 200 OK"
21
  2025-09-24 16:42:54,712 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
 
 
 
 
 
 
 
 
 
 
19
  2025-09-24 16:42:54,206 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events "HTTP/1.1 200 OK"
20
  2025-09-24 16:42:54,291 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ "HTTP/1.1 200 OK"
21
  2025-09-24 16:42:54,712 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
22
+ 2025-09-24 17:21:35,831 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events "HTTP/1.1 200 OK"
23
+ 2025-09-24 17:21:35,909 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ "HTTP/1.1 200 OK"
24
+ 2025-09-24 17:21:36,327 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
25
+ 2025-09-24 17:23:35,213 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events "HTTP/1.1 200 OK"
26
+ 2025-09-24 17:23:35,231 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ "HTTP/1.1 200 OK"
27
+ 2025-09-24 17:23:35,735 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
28
+ 2025-09-24 17:26:13,078 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events "HTTP/1.1 200 OK"
29
+ 2025-09-24 17:26:13,151 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ "HTTP/1.1 200 OK"
30
+ 2025-09-24 17:26:13,778 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
hf_deploy/launcher.py ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import shutil
4
+ from pathlib import Path
5
+ from huggingface_hub import snapshot_download
6
+ import importlib.util
7
+
8
+ from dotenv import load_dotenv
9
+ load_dotenv()
10
+
11
+ def setup_cache_directory():
12
+ """Create a hidden cache directory '.cache'.
13
+
14
+ If an existing 'cache' directory exists, migrate its contents into '.cache'.
15
+ Set restrictive permissions (owner rwx) and on macOS set the Finder hidden flag
16
+ unless the environment variable `HIDE_CACHE` is explicitly set to '0'.
17
+ """
18
+ hidden_cache = Path(".cache")
19
+ public_cache = Path("cache")
20
+
21
+ # If the hidden cache doesn't exist but a public one does, move it.
22
+ try:
23
+ if not hidden_cache.exists():
24
+ if public_cache.exists():
25
+ # Prefer move to preserve contents and metadata
26
+ public_cache.rename(hidden_cache)
27
+ else:
28
+ hidden_cache.mkdir(exist_ok=True)
29
+ else:
30
+ # ensure it exists
31
+ hidden_cache.mkdir(exist_ok=True)
32
+
33
+ # Restrict permissions to owner only (rwx------)
34
+ try:
35
+ hidden_cache.chmod(0o700)
36
+ except Exception:
37
+ # chmod may fail on some filesystems or platforms; ignore
38
+ pass
39
+
40
+ # On macOS, optionally set Finder hidden flag for extra concealment
41
+ if sys.platform == "darwin":
42
+ hide_flag = os.environ.get("HIDE_CACHE", "1")
43
+ if hide_flag != "0":
44
+ try:
45
+ # 'chflags hidden <path>' will make the folder hidden in Finder
46
+ os.system(f"/usr/bin/chflags hidden {hidden_cache}")
47
+ except Exception:
48
+ pass
49
+
50
+ return hidden_cache
51
+ except Exception:
52
+ # Fallback: try to create a simple cache folder named 'cache'
53
+ public_cache.mkdir(exist_ok=True)
54
+ return public_cache
55
+
56
+ def create_symbolic_links(cache_dir):
57
+ """Create symbolic links for Generated, static, and view_session directories.
58
+
59
+ The strategy is to make sure we have real directories in cache and symbolic links from root to cache.
60
+ This ensures the BytePlus app can access these directories even when running from cache.
61
+ """
62
+ root_dir = Path.cwd()
63
+ directories_to_link = ["Generated", "static", "view_session"]
64
+
65
+ for dir_name in directories_to_link:
66
+ # Root directory path (should be symlink)
67
+ root_link = root_dir / dir_name
68
+ # Cache directory path (should be real directory)
69
+ cache_target = cache_dir / dir_name
70
+
71
+ try:
72
+ # Ensure we have a real directory in cache
73
+ if cache_target.is_symlink():
74
+ # Remove the symlink in cache if it exists
75
+ cache_target.unlink()
76
+ print(f"Removed cache symlink: {cache_target}")
77
+
78
+ if not cache_target.exists():
79
+ cache_target.mkdir(parents=True, exist_ok=True)
80
+ print(f"Created cache directory: {cache_target}")
81
+
82
+ # Set proper permissions on cache directory (755)
83
+ cache_target.chmod(0o755)
84
+ print(f"Set permissions 755 on: {cache_target}")
85
+
86
+ # Ensure root has symlink to cache (if it doesn't already exist)
87
+ if not root_link.is_symlink():
88
+ if root_link.exists():
89
+ # If it's a real directory, we might need to move content or just replace
90
+ if root_link.is_dir():
91
+ # Try to move content to cache first
92
+ try:
93
+ for item in root_link.iterdir():
94
+ dest = cache_target / item.name
95
+ if not dest.exists():
96
+ shutil.move(str(item), str(dest))
97
+ shutil.rmtree(root_link)
98
+ print(f"Moved content from {root_link} to {cache_target}")
99
+ except Exception as e:
100
+ print(f"Could not move content: {e}")
101
+ shutil.rmtree(root_link)
102
+ else:
103
+ root_link.unlink()
104
+
105
+ # Create the symlink from root to cache
106
+ root_link.symlink_to(cache_target)
107
+ print(f"Created root symlink: {root_link} -> {cache_target}")
108
+ else:
109
+ print(f"Root symlink already exists: {root_link}")
110
+
111
+ except Exception as e:
112
+ print(f"Error setting up links for {dir_name}: {e}")
113
+ # Fallback: ensure at least the cache directory exists
114
+ try:
115
+ if not cache_target.exists():
116
+ cache_target.mkdir(parents=True, exist_ok=True)
117
+ cache_target.chmod(0o755)
118
+ print(f"Created fallback cache directory: {cache_target}")
119
+ except Exception as fe:
120
+ print(f"Fallback failed for {dir_name}: {fe}")
121
+
122
+ def ensure_directory_permissions():
123
+ """Ensure proper permissions on key directories in the current working directory."""
124
+ directories_to_fix = ["Generated", "static", "view_session"]
125
+
126
+ for dir_name in directories_to_fix:
127
+ dir_path = Path(dir_name)
128
+ try:
129
+ if not dir_path.exists():
130
+ # Create directory if it doesn't exist
131
+ dir_path.mkdir(parents=True, exist_ok=True)
132
+ print(f"Created directory: {dir_path}")
133
+
134
+ # Set directory permissions to 755 (owner rwx, group rx, others rx)
135
+ dir_path.chmod(0o755)
136
+ print(f"Set permissions 755 on directory: {dir_path}")
137
+
138
+ # Set permissions on all subdirectories and files
139
+ for item in dir_path.rglob('*'):
140
+ try:
141
+ if item.is_dir():
142
+ item.chmod(0o755)
143
+ else:
144
+ item.chmod(0o644) # Files: owner rw, group r, others r
145
+ except Exception as e:
146
+ print(f"Warning: Could not set permissions on {item}: {e}")
147
+
148
+ except Exception as e:
149
+ print(f"Error ensuring permissions for {dir_name}: {e}")
150
+
151
+ def download_space(cache_dir):
152
+ token = os.environ.get("HF_TOKEN")
153
+ repo_id = os.environ.get("REPO_ID")
154
+
155
+ if not token or not repo_id:
156
+ return False
157
+
158
+ try:
159
+ snapshot_download(
160
+ repo_id=repo_id,
161
+ repo_type="space",
162
+ local_dir=cache_dir,
163
+ token=token
164
+ )
165
+ return True
166
+ except:
167
+ return False
168
+
169
+ def load_app(cache_dir):
170
+ sys.path.insert(0, str(cache_dir))
171
+ app_path = cache_dir / "app.py"
172
+ spec = importlib.util.spec_from_file_location("app", app_path)
173
+ app = importlib.util.module_from_spec(spec)
174
+ spec.loader.exec_module(app)
175
+
176
+ # First try the common 'demo' attribute used by many Gradio Spaces
177
+ if hasattr(app, "demo"):
178
+ return app.demo
179
+
180
+ # Otherwise, search for any attribute with a callable 'launch' method
181
+ for name in dir(app):
182
+ try:
183
+ attr = getattr(app, name)
184
+ except Exception:
185
+ continue
186
+ # Skip classes (types) since their 'launch' will be an unbound function
187
+ if isinstance(attr, type):
188
+ continue
189
+ if hasattr(attr, "launch") and callable(getattr(attr, "launch")):
190
+ return attr
191
+
192
+ # Next, accept top-level callables that return an object with a 'launch' method.
193
+ # This covers Spaces that expose a factory function instead of a 'demo' object.
194
+ factory_names = [
195
+ "create_secure_interface",
196
+ "create_app",
197
+ "create_demo",
198
+ "main",
199
+ "generator",
200
+ ]
201
+
202
+ for name in factory_names:
203
+ if hasattr(app, name):
204
+ try:
205
+ candidate = getattr(app, name)
206
+ if callable(candidate):
207
+ created = candidate()
208
+ if hasattr(created, "launch") and callable(getattr(created, "launch")):
209
+ return created
210
+ # If the factory itself is a Gradio Interface (callable with launch), return it
211
+ if hasattr(candidate, "launch") and callable(getattr(candidate, "launch")):
212
+ return candidate
213
+ except Exception:
214
+ # ignore failures from calling the factory and continue searching
215
+ pass
216
+
217
+ # Also accept any top-level callable that when called returns an object with 'launch'
218
+ for name in dir(app):
219
+ if name.startswith("__"):
220
+ continue
221
+ try:
222
+ attr = getattr(app, name)
223
+ except Exception:
224
+ continue
225
+ if callable(attr):
226
+ try:
227
+ created = attr()
228
+ if hasattr(created, "launch") and callable(getattr(created, "launch")):
229
+ return created
230
+ except Exception:
231
+ # if calling fails, skip
232
+ continue
233
+
234
+ # If nothing found, raise a helpful error describing the problem
235
+ available = [n for n in dir(app) if not n.startswith("__")]
236
+ raise AttributeError(
237
+ "Could not find a demo application in the downloaded space."
238
+ f" Looked for attribute 'demo' and any attribute with a callable 'launch'."
239
+ f" Available top-level names: {available}"
240
+ )
241
+
242
+ if __name__ == "__main__":
243
+ print("Setting up BytePlus Image Generation Studio...")
244
+
245
+ # First ensure proper permissions on existing directories
246
+ ensure_directory_permissions()
247
+
248
+ # Setup cache directory
249
+ cache_dir = setup_cache_directory()
250
+ print(f"Cache directory: {cache_dir}")
251
+
252
+ # Download the space
253
+ if download_space(cache_dir):
254
+ print("Successfully downloaded BytePlus space")
255
+
256
+ # Create symbolic links for directory access
257
+ create_symbolic_links(cache_dir)
258
+
259
+ # Load and launch the app
260
+ try:
261
+ demo = load_app(cache_dir)
262
+ print("Launching BytePlus Image Generation Studio...")
263
+ demo.launch()
264
+ except Exception as e:
265
+ print(f"Error launching app: {e}")
266
+ sys.exit(1)
267
+ else:
268
+ print("Failed to download space. Please check your HF_TOKEN and REPO_ID environment variables.")
269
+ sys.exit(1)