File size: 2,557 Bytes
14114e8 |
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 |
#!/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()
|