Spaces:
Running
Running
| import os | |
| import sys | |
| import subprocess | |
| # Set environment variables for clean startup | |
| os.environ["PYTHONUTF8"] = "1" | |
| os.environ["PYTHONWARNINGS"] = "ignore:Multiple distributions found for package optimum" | |
| os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" | |
| os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python" | |
| os.environ["FAKESHIELD_SKIP_WARMUP"] = "0" # Enable background model pre-loading | |
| # Handle optional arguments | |
| if "--fast" in sys.argv: | |
| os.environ["FAKESHIELD_SKIP_WARMUP"] = "1" | |
| print(">> Fast Mode enabled: Skipping forensic model pre-loading.") | |
| # Define the command to run (using uvicorn directly for stable startup) | |
| # Use the PORT environment variable if set, otherwise default to Hugging Face's 7860. | |
| port = int(os.environ.get("PORT", os.environ.get("API_PORT", "7860"))) | |
| cmd = [sys.executable, "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", str(port)] | |
| # Change directory to the script's directory (backend) | |
| os.chdir(os.path.dirname(os.path.abspath(__file__))) | |
| print(f"Starting FakeShield Backend v10.0 (UTF-8 Mode) on port {port}...") | |
| try: | |
| subprocess.run(cmd, check=True) | |
| except KeyboardInterrupt: | |
| print("\nBackend stopped by user.") | |
| except Exception as e: | |
| print(f"\nBackend crashed: {e}") | |