bellmake's picture
SAM3 Video Segmentation - Clean deployment
14114e8
#!/usr/bin/env python3
"""
Encrypted Code Loader - Hugging Face Space Entry Point
This file decrypts and executes the encrypted app.py.
DECRYPT_KEY must be stored in Hugging Face Space Secrets.
"""
import os
import sys
from cryptography.fernet import Fernet
# List of files to decrypt
ENCRYPTED_FILES = {
"app.py.encrypted": "app.py",
}
def decrypt_file(encrypted_path, decrypted_path, key):
"""Decrypts an encrypted file."""
cipher = Fernet(key)
# Read encrypted file
with open(encrypted_path, 'rb') as f:
encrypted_data = f.read()
# Decrypt
try:
decrypted_data = cipher.decrypt(encrypted_data)
except Exception as e:
print(f"❌ Decryption failed for {encrypted_path}: {e}")
print(" Please check that DECRYPT_KEY is correctly set in Space Secrets.")
sys.exit(1)
# Save decrypted file to temporary location (current directory in this case)
with open(decrypted_path, 'wb') as f:
f.write(decrypted_data)
print(f"βœ“ Decrypted: {encrypted_path} β†’ {decrypted_path} ({len(decrypted_data):,} bytes)")
def main():
print("=" * 60)
print("πŸ”“ Decrypting source code...")
print("=" * 60)
# Get decryption key from Secrets
key_str = os.getenv("DECRYPT_KEY")
if not key_str:
print("❌ ERROR: DECRYPT_KEY not found in environment variables!")
print(" Please set DECRYPT_KEY in Hugging Face Space Secrets.")
print(" Go to: Settings β†’ Variables and secrets β†’ Add secret")
sys.exit(1)
try:
key = key_str.encode('utf-8')
cipher = Fernet(key) # Validate key
except Exception as e:
print(f"❌ Invalid DECRYPT_KEY: {e}")
sys.exit(1)
print(f"βœ“ DECRYPT_KEY loaded from secrets")
print()
# Decrypt files
for encrypted_file, decrypted_file in ENCRYPTED_FILES.items():
if not os.path.exists(encrypted_file):
print(f"⚠️ Warning: {encrypted_file} not found, skipping...")
continue
decrypt_file(encrypted_file, decrypted_file, key)
print()
print("=" * 60)
print("βœ… Decryption complete! Starting application...")
print("=" * 60)
print()
# Execute decrypted app.py
# app.py automatically launches the Gradio app upon import
import app
# Explicitly launch if a demo object exists
if hasattr(app, 'demo'):
print("βœ“ Launching Gradio app...")
app.demo.launch()
if __name__ == "__main__":
main()