feat: implement job resumption, custom queue reordering, and auto-venv start script setup
This commit is contained in:
@@ -21,18 +21,83 @@ import webbrowser
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
PID_FILE = os.path.join(BASE_DIR, ".uvicorn.pid")
|
||||
LOG_FILE = os.path.join(BASE_DIR, "server.log")
|
||||
# Detect the right Python interpreter:
|
||||
# 1. If running inside an activated venv, use that interpreter
|
||||
# 2. Otherwise look for venv/ then .venv/ in the project
|
||||
if sys.prefix != sys.base_prefix:
|
||||
# Already inside an activated venv — use it
|
||||
VENV_PYTHON = sys.executable
|
||||
else:
|
||||
VENV_PYTHON = os.path.join(BASE_DIR, "venv", "bin", "python")
|
||||
if not os.path.isfile(VENV_PYTHON):
|
||||
VENV_PYTHON = os.path.join(BASE_DIR, ".venv", "bin", "python")
|
||||
if not os.path.isfile(VENV_PYTHON):
|
||||
VENV_PYTHON = sys.executable # last resort: system python
|
||||
|
||||
VENV_PYTHON = sys.executable # default fallback
|
||||
|
||||
def ensure_venv():
|
||||
"""Detect or create the virtual environment and install dependencies."""
|
||||
global VENV_PYTHON
|
||||
|
||||
# 1. Already inside an activated venv — use it
|
||||
if sys.prefix != sys.base_prefix:
|
||||
VENV_PYTHON = sys.executable
|
||||
return
|
||||
|
||||
venv_dir = os.path.join(BASE_DIR, "venv")
|
||||
dot_venv_dir = os.path.join(BASE_DIR, ".venv")
|
||||
|
||||
# Check if either venv/ or .venv/ exists with a python interpreter
|
||||
selected_venv = None
|
||||
if os.path.isdir(os.path.join(venv_dir, "bin")):
|
||||
selected_venv = venv_dir
|
||||
elif os.path.isdir(os.path.join(dot_venv_dir, "bin")):
|
||||
selected_venv = dot_venv_dir
|
||||
|
||||
if selected_venv:
|
||||
python_exe = os.path.join(selected_venv, "bin", "python")
|
||||
if os.path.isfile(python_exe):
|
||||
VENV_PYTHON = python_exe
|
||||
return
|
||||
|
||||
# No valid venv found — let's build it!
|
||||
print("⚙️ Virtual environment not detected. Initializing setup...")
|
||||
print(f" Creating virtual environment at: {venv_dir}")
|
||||
try:
|
||||
import venv
|
||||
venv.create(venv_dir, with_pip=True)
|
||||
print("✅ Virtual environment created.")
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to create virtual environment via 'venv' module: {e}")
|
||||
print(" Attempting subprocess fallback...")
|
||||
try:
|
||||
subprocess.run([sys.executable, "-m", "venv", venv_dir], check=True)
|
||||
print("✅ Virtual environment created (fallback).")
|
||||
except Exception as err:
|
||||
print(f"❌ Subprocess fallback failed: {err}")
|
||||
sys.exit(1)
|
||||
|
||||
VENV_PYTHON = os.path.join(venv_dir, "bin", "python")
|
||||
pip_exe = os.path.join(venv_dir, "bin", "pip")
|
||||
|
||||
# Install dependencies
|
||||
requirements_file = os.path.join(BASE_DIR, "requirements.txt")
|
||||
if os.path.isfile(requirements_file):
|
||||
print("📦 Installing Python dependencies from requirements.txt...")
|
||||
try:
|
||||
# Upgrade pip
|
||||
subprocess.run([pip_exe, "install", "--upgrade", "pip", "--quiet"], check=True)
|
||||
# Install requirements
|
||||
subprocess.run([pip_exe, "install", "-r", requirements_file], check=True)
|
||||
print("✅ All Python dependencies installed successfully.")
|
||||
except Exception as e:
|
||||
print(f"❌ Error installing dependencies: {e}")
|
||||
sys.exit(1)
|
||||
else:
|
||||
print("⚠ requirements.txt not found. Skipping dependency installation.")
|
||||
|
||||
# Configure Real-ESRGAN binary permissions
|
||||
binary_path = os.path.join(BASE_DIR, "realesrgan-bin", "realesrgan-ncnn-vulkan")
|
||||
if os.path.isfile(binary_path):
|
||||
try:
|
||||
os.chmod(binary_path, 0o755)
|
||||
print("✅ Real-ESRGAN binary marked as executable.")
|
||||
except Exception as e:
|
||||
print(f"⚠ Failed to change binary permissions: {e}")
|
||||
|
||||
# Create working directories
|
||||
for d in ["uploads", "outputs", "temp"]:
|
||||
os.makedirs(os.path.join(BASE_DIR, d), exist_ok=True)
|
||||
print("✅ Working directories verified.")
|
||||
|
||||
|
||||
def _is_running(pid: int) -> bool:
|
||||
@@ -153,4 +218,5 @@ if __name__ == "__main__":
|
||||
help="Don't open the browser automatically")
|
||||
args = parser.parse_args()
|
||||
|
||||
ensure_venv()
|
||||
start(args.host, args.port, open_browser=not args.no_browser)
|
||||
|
||||
Reference in New Issue
Block a user