Spaces:
Running
on
Zero
Running
on
Zero
update
Browse files
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 👀
|
|
4 |
colorFrom: red
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
short_description: image_generator
|
|
|
4 |
colorFrom: red
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.44.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
short_description: image_generator
|
loader.py
CHANGED
@@ -1,46 +1,89 @@
|
|
1 |
import os
|
|
|
2 |
from huggingface_hub import hf_hub_download
|
3 |
|
4 |
def load_script(file_str: str):
|
5 |
"""
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
"""
|
9 |
try:
|
10 |
# Split the path by "/"
|
11 |
-
parts = file_str.split("/")
|
12 |
|
13 |
-
if len(parts) <
|
14 |
raise ValueError(
|
15 |
f"Invalid file specification '{file_str}'. "
|
16 |
f"Expected format: 'repo_id/[subfolder]/filename'"
|
17 |
)
|
18 |
|
19 |
-
# First two parts form the repo_id (e.g
|
20 |
repo_id = "/".join(parts[:2])
|
21 |
|
22 |
-
# Last part is the actual filename (e.g
|
23 |
filename = parts[-1]
|
24 |
|
25 |
# Anything between the second and last parts is a subfolder path
|
26 |
-
subfolder = None
|
27 |
-
if len(parts) > 3:
|
28 |
-
subfolder = "/".join(parts[2:-1])
|
29 |
|
30 |
# Retrieve HF token from environment
|
31 |
hf_token = os.getenv("HF_TOKEN", None)
|
|
|
|
|
32 |
|
33 |
# Download the file into current directory "."
|
34 |
file_path = hf_hub_download(
|
35 |
repo_id=repo_id,
|
36 |
filename=filename,
|
37 |
subfolder=subfolder,
|
38 |
-
token=hf_token,
|
39 |
repo_type="space",
|
40 |
-
|
|
|
|
|
41 |
)
|
42 |
|
43 |
-
print(f"Downloaded {filename} from {repo_id} to {file_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
return file_path
|
45 |
|
46 |
except Exception as e:
|
@@ -50,33 +93,40 @@ def load_script(file_str: str):
|
|
50 |
|
51 |
def load_scripts():
|
52 |
"""
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
"""
|
58 |
file_list = os.getenv("FILE_LIST", "").strip()
|
59 |
if not file_list:
|
60 |
-
print("No FILE_LIST environment variable set. Nothing to download.")
|
61 |
return
|
62 |
|
|
|
|
|
63 |
# Step 1: Download the file list itself
|
64 |
file_list_path = load_script(file_list)
|
65 |
if not file_list_path or not os.path.exists(file_list_path):
|
66 |
-
print(f"Could not download or find file list: {file_list_path}")
|
67 |
return
|
68 |
-
|
69 |
# Step 2: Read each line in the downloaded file list
|
70 |
try:
|
71 |
with open(file_list_path, 'r') as f:
|
72 |
lines = [line.strip() for line in f if line.strip()]
|
|
|
73 |
except Exception as e:
|
74 |
-
print(f"Error reading file list: {e}")
|
75 |
return
|
76 |
|
77 |
# Step 3: Download each file from the lines
|
78 |
downloaded_files = []
|
79 |
-
for file_str in lines:
|
|
|
80 |
file_path = load_script(file_str)
|
81 |
if file_path:
|
82 |
downloaded_files.append(file_path)
|
@@ -84,13 +134,17 @@ def load_scripts():
|
|
84 |
# Step 4: Execute the last downloaded file
|
85 |
if downloaded_files:
|
86 |
last_file_path = downloaded_files[-1]
|
87 |
-
print(f"Executing the last downloaded script: {last_file_path}")
|
88 |
try:
|
89 |
with open(last_file_path, 'r') as f:
|
90 |
-
|
|
|
|
|
91 |
except Exception as e:
|
92 |
-
print(f"Error executing the last downloaded script: {e}")
|
|
|
|
|
|
|
93 |
|
94 |
|
95 |
-
# Run the load_scripts function
|
96 |
load_scripts()
|
|
|
1 |
import os
|
2 |
+
import sys
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
|
5 |
def load_script(file_str: str):
|
6 |
"""
|
7 |
+
Downloads a file from the Hugging Face Hub and ensures a symlink exists in the current directory.
|
8 |
+
|
9 |
+
Parameters:
|
10 |
+
- file_str (str): Path in the format 'repo_id/[subfolder]/filename', e.g., 'myorg/myrepo/mysubfolder/myscript.py'
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
- str: The path to the downloaded file.
|
14 |
"""
|
15 |
try:
|
16 |
# Split the path by "/"
|
17 |
+
parts = file_str.strip().split("/")
|
18 |
|
19 |
+
if len(parts) < 2:
|
20 |
raise ValueError(
|
21 |
f"Invalid file specification '{file_str}'. "
|
22 |
f"Expected format: 'repo_id/[subfolder]/filename'"
|
23 |
)
|
24 |
|
25 |
+
# First two parts form the repo_id (e.g., 'myorg/myrepo')
|
26 |
repo_id = "/".join(parts[:2])
|
27 |
|
28 |
+
# Last part is the actual filename (e.g., 'myscript.py')
|
29 |
filename = parts[-1]
|
30 |
|
31 |
# Anything between the second and last parts is a subfolder path
|
32 |
+
subfolder = "/".join(parts[2:-1]) if len(parts) > 3 else None
|
|
|
|
|
33 |
|
34 |
# Retrieve HF token from environment
|
35 |
hf_token = os.getenv("HF_TOKEN", None)
|
36 |
+
if not hf_token:
|
37 |
+
print("Warning: 'HF_TOKEN' environment variable not set. Proceeding without authentication.")
|
38 |
|
39 |
# Download the file into current directory "."
|
40 |
file_path = hf_hub_download(
|
41 |
repo_id=repo_id,
|
42 |
filename=filename,
|
43 |
subfolder=subfolder,
|
|
|
44 |
repo_type="space",
|
45 |
+
token=hf_token,
|
46 |
+
local_dir=".", # Download into the current directory
|
47 |
+
force_download=True,
|
48 |
)
|
49 |
|
50 |
+
print(f"Downloaded '{filename}' from '{repo_id}' to '{file_path}'")
|
51 |
+
|
52 |
+
# Absolute paths for comparison
|
53 |
+
current_dir = os.path.abspath(".")
|
54 |
+
downloaded_file_abs = os.path.abspath(file_path)
|
55 |
+
downloaded_dir_abs = os.path.dirname(downloaded_file_abs)
|
56 |
+
|
57 |
+
# If the file is not in the current directory, create a symlink
|
58 |
+
if downloaded_dir_abs != current_dir:
|
59 |
+
symlink_path = os.path.join(current_dir, filename)
|
60 |
+
|
61 |
+
# If symlink exists, remove it
|
62 |
+
if os.path.islink(symlink_path) or os.path.exists(symlink_path):
|
63 |
+
try:
|
64 |
+
os.remove(symlink_path)
|
65 |
+
print(f"Removed existing link or file: '{symlink_path}'")
|
66 |
+
except Exception as e:
|
67 |
+
print(f"Error removing existing link '{symlink_path}': {e}")
|
68 |
+
return file_path # Return the actual file path even if symlink fails
|
69 |
+
|
70 |
+
# Create a relative symlink
|
71 |
+
relative_target = os.path.relpath(downloaded_file_abs, current_dir)
|
72 |
+
try:
|
73 |
+
os.symlink(relative_target, symlink_path)
|
74 |
+
print(f"Created symlink: '{symlink_path}' -> '{relative_target}'")
|
75 |
+
except OSError as e:
|
76 |
+
print(f"Failed to create symlink for '{filename}': {e}")
|
77 |
+
# On Windows, creating symlinks may require admin privileges
|
78 |
+
# Alternatively, you can copy the file instead of linking
|
79 |
+
# Uncomment the following lines to copy the file if symlink fails
|
80 |
+
# import shutil
|
81 |
+
# try:
|
82 |
+
# shutil.copy2(downloaded_file_abs, symlink_path)
|
83 |
+
# print(f"Copied '{filename}' to '{symlink_path}'")
|
84 |
+
# except Exception as copy_e:
|
85 |
+
# print(f"Failed to copy file for '{filename}': {copy_e}")
|
86 |
+
|
87 |
return file_path
|
88 |
|
89 |
except Exception as e:
|
|
|
93 |
|
94 |
def load_scripts():
|
95 |
"""
|
96 |
+
Downloads and executes scripts based on a file list from the Hugging Face Hub.
|
97 |
+
|
98 |
+
Steps:
|
99 |
+
1. Retrieve the 'FILE_LIST' environment variable, which specifies the file list path.
|
100 |
+
2. Download the file list using `load_script()`.
|
101 |
+
3. Read each line from the downloaded file list, where each line specifies another file to download.
|
102 |
+
4. After downloading all files, execute the last downloaded file.
|
103 |
"""
|
104 |
file_list = os.getenv("FILE_LIST", "").strip()
|
105 |
if not file_list:
|
106 |
+
print("No 'FILE_LIST' environment variable set. Nothing to download.")
|
107 |
return
|
108 |
|
109 |
+
print(f"FILE_LIST: '{file_list}'")
|
110 |
+
|
111 |
# Step 1: Download the file list itself
|
112 |
file_list_path = load_script(file_list)
|
113 |
if not file_list_path or not os.path.exists(file_list_path):
|
114 |
+
print(f"Could not download or find file list: '{file_list_path}'")
|
115 |
return
|
116 |
+
|
117 |
# Step 2: Read each line in the downloaded file list
|
118 |
try:
|
119 |
with open(file_list_path, 'r') as f:
|
120 |
lines = [line.strip() for line in f if line.strip()]
|
121 |
+
print(f"Found {len(lines)} files to download from the file list.")
|
122 |
except Exception as e:
|
123 |
+
print(f"Error reading file list '{file_list_path}': {e}")
|
124 |
return
|
125 |
|
126 |
# Step 3: Download each file from the lines
|
127 |
downloaded_files = []
|
128 |
+
for idx, file_str in enumerate(lines, start=1):
|
129 |
+
print(f"Downloading file {idx}/{len(lines)}: '{file_str}'")
|
130 |
file_path = load_script(file_str)
|
131 |
if file_path:
|
132 |
downloaded_files.append(file_path)
|
|
|
134 |
# Step 4: Execute the last downloaded file
|
135 |
if downloaded_files:
|
136 |
last_file_path = downloaded_files[-1]
|
137 |
+
print(f"Executing the last downloaded script: '{last_file_path}'")
|
138 |
try:
|
139 |
with open(last_file_path, 'r') as f:
|
140 |
+
script_content = f.read()
|
141 |
+
exec(script_content, globals())
|
142 |
+
print(f"Successfully executed '{last_file_path}'")
|
143 |
except Exception as e:
|
144 |
+
print(f"Error executing the last downloaded script '{last_file_path}': {e}")
|
145 |
+
else:
|
146 |
+
print("No files were downloaded to execute.")
|
147 |
+
|
148 |
|
149 |
|
|
|
150 |
load_scripts()
|