Spaces:
Sleeping
Sleeping
File size: 3,175 Bytes
b840b29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | #!/usr/bin/env python3
"""
Script to modify replicate.py for downloading data at runtime.
This script adds data downloading capability to the Replicate predictor,
allowing it to fetch data files during initialization rather than bundling them.
"""
import os
import re
import sys
import shutil
# The file to modify
TARGET_FILE = "replicate.py"
BACKUP_FILE = "replicate.py.bak"
# The download code to insert
DOWNLOAD_CODE = """
# Download data files if needed
try:
import subprocess
print("Checking data files...")
# Run the download script
download_result = subprocess.run(
[sys.executable, "download_data.py", "--skip-verify"],
capture_output=True,
text=True
)
if download_result.returncode != 0:
print(f"Warning: Data download script returned non-zero exit code: {download_result.returncode}")
print(f"Error output: {download_result.stderr}")
else:
print("Data files are ready")
except Exception as e:
print(f"Warning: Failed to download data files: {e}")
# Continue anyway - we'll check for files below
"""
def backup_file(file_path):
"""Create a backup of the file."""
if os.path.exists(file_path):
shutil.copy2(file_path, BACKUP_FILE)
print(f"Created backup: {BACKUP_FILE}")
return True
else:
print(f"Error: File {file_path} not found")
return False
def modify_replicate_file():
"""Modify the replicate.py file to add data downloading."""
if not os.path.exists(TARGET_FILE):
print(f"Error: {TARGET_FILE} not found")
return False
# Make a backup first
if not backup_file(TARGET_FILE):
return False
# Read the file
with open(TARGET_FILE, "r") as f:
content = f.read()
# Look for the setup method
setup_pattern = r"(def setup\(self\):.*?# Check data files exist.*?for file_path in required_files:.*?raise FileNotFoundError.*?)\n"
# Replace with modified code - keep the original check but add download before it
if re.search(setup_pattern, content, re.DOTALL):
modified_content = re.sub(
setup_pattern,
r"\1\n" + DOWNLOAD_CODE + "\n",
content,
flags=re.DOTALL
)
# Write the modified content
with open(TARGET_FILE, "w") as f:
f.write(modified_content)
print(f"Successfully modified {TARGET_FILE} to include data downloading")
return True
else:
print(f"Error: Could not find the setup method pattern in {TARGET_FILE}")
return False
def main():
"""Main function."""
print(f"Modifying {TARGET_FILE} to add data downloading capabilities...")
success = modify_replicate_file()
if success:
print("Modification complete!")
return 0
else:
print("Modification failed. See errors above.")
return 1
if __name__ == "__main__":
sys.exit(main()) |