dergitarama / push_hf_space.bat
kmkarakaya's picture
Deploy Murat Karakaya Akademi Dergi Tarama to HF Space
26fb08f
Raw
History Blame Contribute Delete
6.82 kB
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SPACE_REPO=https://huggingface.co/spaces/kmkarakaya/dergitarama"
set "COMMIT_MESSAGE=Deploy Murat Karakaya Akademi Dergi Tarama to HF Space"
set "SOURCE_DIR=%~dp0"
set "WORK_DIR="
set "TEMP_WORK_DIR_CREATED=0"
set "DRY_RUN=0"
set "SKIP_DOCKER_CHECK=0"
set "GIT_XET_PATH=C:\Program Files\Git-Xet"
set "DOCKER_TAG=murat-karakaya-akademi-dergi-tarama-hf-preflight"
set "DOCKER_CONTAINER=murat-karakaya-akademi-dergi-tarama-hf-preflight-container"
set "DOCKER_TEST_PORT=8870"
if "%SOURCE_DIR:~-1%"=="\" set "SOURCE_DIR=%SOURCE_DIR:~0,-1%"
if exist "%GIT_XET_PATH%\git-xet.exe" set "PATH=%GIT_XET_PATH%;%PATH%"
set "POSITIONAL_INDEX=0"
:parse_args
if "%~1"=="" goto :args_done
if /I "%~1"=="/help" goto :help
if /I "%~1"=="-h" goto :help
if /I "%~1"=="--help" goto :help
if /I "%~1"=="/dry-run" (
set "DRY_RUN=1"
shift
goto :parse_args
)
if /I "%~1"=="/skip-docker-check" (
set "SKIP_DOCKER_CHECK=1"
shift
goto :parse_args
)
set /a POSITIONAL_INDEX+=1
if %POSITIONAL_INDEX%==1 set "SPACE_REPO=%~1"
if %POSITIONAL_INDEX%==2 set "COMMIT_MESSAGE=%~1"
shift
goto :parse_args
:args_done
echo [1/7] Checking required tools and files...
where git >nul 2>nul || (
echo ERROR: git not found in PATH.
exit /b 1
)
where robocopy >nul 2>nul || (
echo ERROR: robocopy not found in PATH.
exit /b 1
)
git xet --version >nul 2>nul || (
echo ERROR: git-xet is not installed.
echo.
echo Hugging Face rejected the Excel files because binary files must be pushed through Xet storage.
echo Install it on Windows with one of these options:
echo winget install git-xet
echo or install the MSI from the git-xet Windows release page
echo.
echo Then run:
echo "C:\Program Files\Git-Xet\git-xet.exe" install
echo.
echo After that, rerun this script.
exit /b 1
)
for %%F in ("Dockerfile" "README.md" "requirements.txt" "app.py" "journal_engine.py" "ubyt.xlsx" "Elsevier.xlsx" "Wiley.xlsx") do (
if not exist "%SOURCE_DIR%\%%~F" (
echo ERROR: Required file missing: %SOURCE_DIR%\%%~F
exit /b 1
)
)
for %%F in ("static\index.html" "static\app.js" "static\styles.css") do (
if not exist "%SOURCE_DIR%\%%~F" (
echo ERROR: Required file missing: %SOURCE_DIR%\%%~F
exit /b 1
)
)
findstr /B /C:"sdk: docker" "%SOURCE_DIR%\README.md" >nul || (
echo ERROR: README.md is missing `sdk: docker` in the YAML frontmatter.
exit /b 1
)
findstr /B /C:"app_port: 7860" "%SOURCE_DIR%\README.md" >nul || (
echo ERROR: README.md is missing `app_port: 7860` in the YAML frontmatter.
exit /b 1
)
echo Source repo : %SOURCE_DIR%
echo HF Space : %SPACE_REPO%
echo Commit msg : %COMMIT_MESSAGE%
if "%DRY_RUN%"=="1" (
echo.
echo Dry run enabled. No files will be copied or pushed.
exit /b 0
)
echo.
echo [2/7] Validating Docker build...
if "%SKIP_DOCKER_CHECK%"=="1" (
echo Docker preflight skipped by request.
) else (
where docker >nul 2>nul || (
echo ERROR: docker not found in PATH. Use /skip-docker-check to bypass this preflight.
exit /b 1
)
where powershell >nul 2>nul || (
echo ERROR: powershell not found in PATH. Use /skip-docker-check to bypass this preflight.
exit /b 1
)
docker rm -f %DOCKER_CONTAINER% >nul 2>nul
docker build -t %DOCKER_TAG% "%SOURCE_DIR%" || (
echo ERROR: docker build failed. Aborting push.
exit /b 1
)
docker run -d --rm --name %DOCKER_CONTAINER% -p %DOCKER_TEST_PORT%:7860 %DOCKER_TAG% >nul || (
echo ERROR: docker container failed to start. Aborting push.
docker image rm %DOCKER_TAG% >nul 2>nul
exit /b 1
)
powershell -NoProfile -Command "$deadline=(Get-Date).AddSeconds(30); $ok=$false; while((Get-Date) -lt $deadline){ try { $response=Invoke-RestMethod -Uri 'http://127.0.0.1:%DOCKER_TEST_PORT%/health' -TimeoutSec 3; if ($response.status -eq 'ok') { $ok=$true; break } } catch {}; Start-Sleep -Milliseconds 500 }; if(-not $ok){ exit 1 }" || (
echo ERROR: docker container started but /health did not return status ok.
echo --- container logs ---
docker logs %DOCKER_CONTAINER%
docker rm -f %DOCKER_CONTAINER% >nul 2>nul
docker image rm %DOCKER_TAG% >nul 2>nul
exit /b 1
)
docker rm -f %DOCKER_CONTAINER% >nul 2>nul
docker image rm %DOCKER_TAG% >nul 2>nul
)
echo.
echo [3/7] Preparing temporary HF Space clone...
set "WORK_DIR=%TEMP%\hf-space-%RANDOM%%RANDOM%"
set "TEMP_WORK_DIR_CREATED=1"
echo Temporary dir: %WORK_DIR%
git clone "%SPACE_REPO%" "%WORK_DIR%" || goto :fail_cleanup
git -C "%WORK_DIR%" fetch origin || goto :fail_cleanup
git -C "%WORK_DIR%" checkout main || goto :fail_cleanup
git -C "%WORK_DIR%" reset --hard origin/main || goto :fail_cleanup
git -C "%WORK_DIR%" clean -fd || goto :fail_cleanup
echo.
echo [4/7] Syncing files to Space working tree...
robocopy "%SOURCE_DIR%" "%WORK_DIR%" /MIR /XD .git .venv __pycache__ .pytest_cache .mypy_cache .vscode node_modules /XF "~$*.xlsx"
set "ROBOCOPY_EXIT=%ERRORLEVEL%"
if %ROBOCOPY_EXIT% GEQ 8 (
echo ERROR: robocopy failed with exit code %ROBOCOPY_EXIT%.
goto :robocopy_fail
)
del /Q "%WORK_DIR%\~$*.xlsx" 2>nul
echo.
echo [5/7] Checking for changes...
pushd "%WORK_DIR%" || exit /b 1
set "HAS_CHANGES=0"
for /f "delims=" %%I in ('git status --porcelain') do (
set "HAS_CHANGES=1"
goto :changes_found
)
:changes_found
if "%HAS_CHANGES%"=="0" (
echo No changes detected. Nothing to commit.
popd
goto :success_cleanup
)
echo.
echo [6/7] Creating commit...
git add . || (
popd
goto :fail_cleanup
)
git diff --cached --quiet && (
echo No staged changes detected after sync. Nothing to commit.
popd
goto :success_cleanup
)
git commit -m "%COMMIT_MESSAGE%" || (
echo ERROR: git commit failed.
popd
goto :fail_cleanup
)
echo.
echo [7/7] Pushing to Hugging Face Space...
git push origin HEAD || (
echo ERROR: git push failed.
popd
goto :fail_cleanup
)
popd
call :cleanup_work_dir
echo.
echo Done. Check build logs at:
echo %SPACE_REPO%
exit /b 0
:robocopy_fail
call :cleanup_work_dir
exit /b %ROBOCOPY_EXIT%
:fail_cleanup
call :cleanup_work_dir
exit /b 1
:success_cleanup
call :cleanup_work_dir
exit /b 0
:cleanup_work_dir
if "%TEMP_WORK_DIR_CREATED%"=="1" if defined WORK_DIR if exist "%WORK_DIR%" rmdir /S /Q "%WORK_DIR%" 2>nul
exit /b 0
:help
echo Usage:
echo push_hf_space.bat [/dry-run] [/skip-docker-check] [space_repo] [commit_message]
echo.
echo Examples:
echo push_hf_space.bat
echo push_hf_space.bat /dry-run
echo push_hf_space.bat /skip-docker-check
echo push_hf_space.bat https://huggingface.co/spaces/kmkarakaya/dergitarama "Deploy latest app"
exit /b 0