Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
+
|
| 6 |
+
# Get Space 2 URL from environment
|
| 7 |
+
SPACE2_URL = os.getenv("SPACE2_URL") # e.g., "username/space2-fetcher"
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
+
|
| 10 |
+
if not SPACE2_URL or not HF_TOKEN:
|
| 11 |
+
print("ERROR: SPACE2_URL and HF_TOKEN must be set as environment variables")
|
| 12 |
+
sys.exit(1)
|
| 13 |
+
|
| 14 |
+
# Download files from Space 2's repository
|
| 15 |
+
# Space 2 has already fetched the code from Space 1
|
| 16 |
+
try:
|
| 17 |
+
print(f"Downloading code from Space 2: {SPACE2_URL}")
|
| 18 |
+
|
| 19 |
+
local_dir = snapshot_download(
|
| 20 |
+
repo_id=SPACE2_URL,
|
| 21 |
+
repo_type="space",
|
| 22 |
+
token=HF_TOKEN,
|
| 23 |
+
local_dir="/app/code",
|
| 24 |
+
local_dir_use_symlinks=False,
|
| 25 |
+
allow_patterns=["fetched_code/**/*"] # Only get the fetched_code directory
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Move files from fetched_code subdirectory to /app/code
|
| 29 |
+
fetched_dir = Path("/app/code/fetched_code")
|
| 30 |
+
if fetched_dir.exists():
|
| 31 |
+
import shutil
|
| 32 |
+
for item in fetched_dir.iterdir():
|
| 33 |
+
dest = Path("/app/code") / item.name
|
| 34 |
+
if dest.exists():
|
| 35 |
+
if dest.is_dir():
|
| 36 |
+
shutil.rmtree(dest)
|
| 37 |
+
else:
|
| 38 |
+
dest.unlink()
|
| 39 |
+
shutil.move(str(item), str(dest))
|
| 40 |
+
|
| 41 |
+
# Remove the now-empty fetched_code directory
|
| 42 |
+
fetched_dir.rmdir()
|
| 43 |
+
|
| 44 |
+
print("✅ Successfully downloaded and extracted code")
|
| 45 |
+
|
| 46 |
+
# List files for verification
|
| 47 |
+
files = list(Path("/app/code").rglob("*"))
|
| 48 |
+
print(f"Total files: {len([f for f in files if f.is_file()])}")
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"❌ Error downloading code: {e}")
|
| 52 |
+
sys.exit(1)
|