|
|
|
|
|
""" |
|
|
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 |
|
|
|
|
|
|
|
|
ENCRYPTED_FILES = { |
|
|
"app.py.encrypted": "app.py", |
|
|
} |
|
|
|
|
|
|
|
|
def decrypt_file(encrypted_path, decrypted_path, key): |
|
|
"""Decrypts an encrypted file.""" |
|
|
cipher = Fernet(key) |
|
|
|
|
|
|
|
|
with open(encrypted_path, 'rb') as f: |
|
|
encrypted_data = f.read() |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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) |
|
|
except Exception as e: |
|
|
print(f"β Invalid DECRYPT_KEY: {e}") |
|
|
sys.exit(1) |
|
|
|
|
|
print(f"β DECRYPT_KEY loaded from secrets") |
|
|
print() |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
import app |
|
|
|
|
|
|
|
|
if hasattr(app, 'demo'): |
|
|
print("β Launching Gradio app...") |
|
|
app.demo.launch() |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|