Spaces:
Paused
Paused
| import subprocess | |
| import os | |
| import sys | |
| # --- Configuration --- | |
| PORT = int(os.environ.get("PORT", 7860)) | |
| BIND_ADDRESS = "0.0.0.0" # Listen on all interfaces within the container | |
| # Combine address and port for the --bind argument | |
| COMBINED_BIND = f"{BIND_ADDRESS}:{PORT}" | |
| # Choose the g4f install variant (ensure it matches requirements.txt) | |
| G4F_VARIANT = "all" # Change to 'slim' or 'base' if needed | |
| # --- --- --- --- --- | |
| print(f"--- Starting gpt4free API Server (variant: {G4F_VARIANT}) ---") | |
| print(f"Attempting to bind to: {COMBINED_BIND}") # Updated print statement | |
| print(f"Python version: {sys.version}") | |
| print("-" * 30) | |
| # Construct the command using the combined --bind "<address>:<port>" format | |
| # Remove the separate --port argument | |
| command = [ | |
| sys.executable, | |
| "-m", | |
| "g4f", | |
| "--bind", COMBINED_BIND, # Use the combined address and port string | |
| "--debug" | |
| # No separate --port argument needed here | |
| ] | |
| print(f"Executing command: {' '.join(command)}") | |
| print("-" * 30, flush=True) | |
| try: | |
| process = subprocess.Popen(command, stdout=sys.stdout, stderr=sys.stderr) | |
| process.wait() | |
| except FileNotFoundError: | |
| print("\nERROR: Could not find the python interpreter or the 'g4f' module.") | |
| print("Please ensure Python is installed correctly and 'g4f' is in requirements.txt") | |
| sys.exit(1) | |
| except Exception as e: | |
| print(f"\nERROR: An unexpected error occurred while trying to start the server: {e}") | |
| sys.exit(1) | |
| print("-" * 30) | |
| print("--- gpt4free API Server process exited. ---") |