File size: 4,710 Bytes
065e391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
@echo off
cd /d "%~dp0"
setlocal enabledelayedexpansion

set "PROJECT_DIR=%CD%"
set "BACKEND_DIR=%PROJECT_DIR%\backend"
set "VENV_DIR=%PROJECT_DIR%\.venv"
set "VENV_PYTHON=%VENV_DIR%\Scripts\python.exe"
set "APP_URL=http://127.0.0.1:8501"

echo ==================================================
echo       RodSizer - Launcher
echo ==================================================
echo.

set "PY_EXE="

REM --- 1. Try 'python' command ---
python --version >nul 2>&1
if %errorlevel% equ 0 set "PY_EXE=python"
if defined PY_EXE goto FOUND_PYTHON

REM --- 2. Try 'py' launcher ---
py --version >nul 2>&1
if !errorlevel! equ 0 set "PY_EXE=py"
if defined PY_EXE goto FOUND_PYTHON

REM --- 3. Try Auto-Detect Common Paths ---
if exist "C:\Python39\python.exe" set "PY_EXE=C:\Python39\python.exe" & goto FOUND_PYTHON
if exist "C:\Python310\python.exe" set "PY_EXE=C:\Python310\python.exe" & goto FOUND_PYTHON
if exist "C:\Python311\python.exe" set "PY_EXE=C:\Python311\python.exe" & goto FOUND_PYTHON
if exist "C:\Program Files\Python39\python.exe" set "PY_EXE=C:\Program Files\Python39\python.exe" & goto FOUND_PYTHON
if exist "C:\Program Files\Python310\python.exe" set "PY_EXE=C:\Program Files\Python310\python.exe" & goto FOUND_PYTHON
if exist "C:\Program Files\Python311\python.exe" set "PY_EXE=C:\Program Files\Python311\python.exe" & goto FOUND_PYTHON

REM --- 4. User Input Fallback ---
:ASK_USER
echo.
echo [ERROR] Could not find Python automatically.
echo.
echo Please locate your 'python.exe' file.
echo TIP: Look in "C:\Program Files\Python3x" or "AppData\Local\Programs\Python"
echo.
echo IMPORTANT: Drag the FILE, not a shortcut.
echo.
set /p "PY_EXE=Drag and Drop python.exe here and press Enter: "
set "PY_EXE=!PY_EXE:"=!"

REM Validate Shortcut
echo "!PY_EXE!" | findstr /i ".lnk" >nul
if %errorlevel% equ 0 (
    echo.
    echo [ERROR] You provided a Shortcut (.lnk).
    echo Please use the real python.exe.
    goto ASK_USER
)

:FOUND_PYTHON
echo.
echo [INFO] Using Python: "!PY_EXE!"

REM Verify selected Python
"!PY_EXE!" --version >nul 2>&1
if errorlevel 1 (
    echo.
    echo [CRITICAL ERROR] Selected python path is invalid.
    pause
    exit /b 1
)

if not exist "%BACKEND_DIR%\requirements.txt" (
    echo.
    echo [CRITICAL ERROR] Missing backend\requirements.txt
    pause
    exit /b 1
)

if not exist "%BACKEND_DIR%\main.py" (
    echo.
    echo [CRITICAL ERROR] Missing backend\main.py
    pause
    exit /b 1
)

REM --- 5. Setup Virtual Environment ---
if exist "%VENV_PYTHON%" goto CHECK_ENV
echo.
echo [First Run] Setting up Python environment...
"!PY_EXE!" -m venv "%VENV_DIR%"
if errorlevel 1 (
    echo.
    echo [CRITICAL ERROR] Failed to create virtual environment.
    pause
    exit /b 1
)

REM Clear screen to focus attention
cls
echo ==================================================================
echo       FIRST RUN SETUP - INSTALLING DEPENDENCIES
echo ==================================================================
echo.
echo    Downloading: OpenCV, NumPy, FastAPI, Uvicorn, and others...
echo.
echo    PLEASE WAIT: This typically takes 1-3 minutes.
echo    You will see download progress below.
echo.
echo    (Do not close this window)
echo.
echo ==================================================================
echo.

REM Give user 2 seconds to read before text flies by
timeout /t 2 >nul

"%VENV_PYTHON%" -m pip install --upgrade pip
if errorlevel 1 (
    echo.
    echo [CRITICAL ERROR] Failed to upgrade pip.
    pause
    exit /b 1
)

"%VENV_PYTHON%" -m pip install -r "%BACKEND_DIR%\requirements.txt"
if errorlevel 1 (
    echo.
    echo [CRITICAL ERROR] Dependency installation failed.
    echo Please run RodSizer_DEBUG_Windows.bat for a guided repair.
    pause
    exit /b 1
)

:CHECK_ENV
echo.
echo [INFO] Checking Python environment...
"%VENV_PYTHON%" -c "import cv2, numpy, fastapi, uvicorn, pandas, openpyxl, multipart, tifffile, h5py, skimage, scipy; import ncempy.io; from stardist.models import StarDist2D; from csbdeep.utils import normalize" >nul 2>&1
if errorlevel 1 (
    echo.
    echo [CRITICAL ERROR] The RodSizer environment is incomplete or damaged.
    echo Please run RodSizer_DEBUG_Windows.bat to repair it.
    pause
    exit /b 1
)

echo Activating environment...

REM --- 6. Open Browser ---
echo.
echo Starting Server on PORT 8501...
echo The browser will open in 10 seconds...
echo.
echo IF PAGE FAILS TO LOAD:
echo 1. Keep this window open.
echo 2. Open Chrome/Edge and go to: %APP_URL%
echo.

start "" cmd /c "timeout /t 10 >nul & start %APP_URL%"

REM --- 7. Run Server ---
echo Close this window to stop the app.
cd /d "%BACKEND_DIR%"
"%VENV_PYTHON%" -m uvicorn main:app --reload --host 127.0.0.1 --port 8501

pause