@echo off
REM ===================================================================
REM  FIKO AI - Ses motorlari: konusma tanima ve sesli okuma
REM
REM  STT : faster-whisper small   (yerel, ucretsiz)          port 9000
REM  TTS : Chatterbox Multilingual (yerel, ucretsiz, MIT)    port 5000
REM
REM  Ikisi de 127.0.0.1 uzerinde dinler, veri disari cikmaz.
REM  Ilk calistirmada whisper modeli ve Chatterbox ortami indirilir:
REM  venv, CUDA torch ve yaklasik 3 GB model. 10-20 dakika surebilir.
REM
REM  KADIN SESI: models\voices\referans.wav dosyasina 10-20 sn temiz ve
REM  ifadeli bir kadin ses klibi koy. Yoksa modelin yerlesik sesi kullanilir.
REM  Duygu yogunlugu: FIKO_TTS_EXAGGERATION ortam degiskeni, 0 ile 1 arasi.
REM ===================================================================

cd /d "%~dp0"

echo.
echo  [1/3] Whisper modeli kontrol ediliyor...
if not exist "models\faster-whisper-small\model.bin" (
  echo    indiriliyor - 461 MB, birkac dakika ...
  powershell -NoProfile -Command "New-Item -ItemType Directory -Force -Path models\faster-whisper-small | Out-Null"
  curl.exe -L --retry 3 -o models\faster-whisper-small\model.bin https://huggingface.co/Systran/faster-whisper-small/resolve/main/model.bin
  curl.exe -L --retry 3 -o models\faster-whisper-small\config.json https://huggingface.co/Systran/faster-whisper-small/resolve/main/config.json
  curl.exe -L --retry 3 -o models\faster-whisper-small\tokenizer.json https://huggingface.co/Systran/faster-whisper-small/resolve/main/tokenizer.json
  curl.exe -L --retry 3 -o models\faster-whisper-small\vocabulary.txt https://huggingface.co/Systran/faster-whisper-small/resolve/main/vocabulary.txt
) else (
  echo    [ok] mevcut
)

echo.
echo  [2/3] Konusma tanima bagimligi...
python -c "import faster_whisper" >nul 2>&1
if errorlevel 1 (
  echo    kuruluyor - birkac dakika, 150 MB ...
  python -m pip install --disable-pip-version-check faster-whisper
) else (
  echo    [ok] faster-whisper kurulu
)

echo.
echo  [3/3] Sesli okuma ortami - Chatterbox...
if not exist "tools\.venv-tts\Scripts\python.exe" goto :tts_kur
tools\.venv-tts\Scripts\python.exe -c "import torch, chatterbox, flask" >nul 2>&1
if errorlevel 1 goto :tts_kur
if not exist "models\hf-cache\hub\models--ResembleAI--chatterbox\snapshots" goto :tts_kur
echo    [ok] Chatterbox kurulu
goto :tts_hazir

:tts_kur
echo    kuruluyor - ilk seferde 10-20 dk surebilir: venv, CUDA torch, 3 GB model ...
powershell -NoProfile -ExecutionPolicy Bypass -File "tools\install-tts.ps1"
if errorlevel 1 (
  echo    [HATA] Chatterbox kurulamadi. Yukaridaki mesajlara bak.
  exit /b 1
)

:tts_hazir
if not exist "models\voices\referans.wav" (
  echo    [!] models\voices\referans.wav yok - yerlesik ses kullanilacak.
  echo        Kadin sesi icin 10-20 sn temiz bir kadin ses klibi koy.
)

echo.
echo  Motorlar baslatiliyor...
REM Not: Ollama + TTS + STT AYNI GPU'yu kullanirsa VRAM dolar ve motorlar birbirini
REM kovar: olcumde tek cumle TTS 1,6 sn yerine 131 sn surdu (RTF 30).
REM Varsayilan: STT CPU/int8 (kucuk model, hizli), TTS+LLM GPU'da. Boylece sohbet
REM modeli ve ses motoru yer bulur. Daha fazla GPU istersen:
REM   set FIKO_STT_DEVICE=cuda
REM   set FIKO_STT_COMPUTE=float16
if not defined FIKO_STT_DEVICE set FIKO_STT_DEVICE=cpu
if not defined FIKO_STT_COMPUTE set FIKO_STT_COMPUTE=int8
start "FIKO STT" cmd /k python tools\stt_server.py 9000 --model models\faster-whisper-small --device %FIKO_STT_DEVICE% --compute-type %FIKO_STT_COMPUTE%
start "FIKO TTS" cmd /k tools\.venv-tts\Scripts\python.exe -u tools\tts_server.py 5000

echo  [ok] Ses motorlari baslatildi: STT :9000 - TTS :5000
echo  [!] Chatterbox modeli yuklenirken 20-40 sn surer, ilk cumle gecikebilir.
exit /b 0