"""Diagnose qwen3-tts engine init - run inside container"""
import sys, os, time, traceback
os.chdir("/models")
sys.path.insert(0, "/opt/qwen3-tts")
os.environ["PYTHONUNBUFFERED"] = "1"

print("=== Step 1: Assets + Tokenizer ===", flush=True)
try:
    from qwen3_tts_gguf.inference.assets import AssetsManager
    from tokenizers import Tokenizer
    assets = AssetsManager("qwen3-tts")
    tok = Tokenizer.from_file("/models/qwen3-tts/tokenizer.json")
    print(f"  OK: assets loaded, vocab_size={tok.get_vocab_size()}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== Step 2: Codec + Speaker Encoders ===", flush=True)
try:
    from qwen3_tts_gguf.inference.encoder import CodecEncoder, SpeakerEncoder
    ce = CodecEncoder("/models/qwen3-tts/qwen3_tts_codec_encoder.fp16.onnx")
    se = SpeakerEncoder("/models/qwen3-tts/qwen3_tts_speaker_encoder.fp16.onnx")
    print(f"  OK: codec_encoder + speaker_encoder loaded", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== Step 3: DecoderProxy ===", flush=True)
try:
    from qwen3_tts_gguf.inference.decoder import DecoderProxy
    t0 = time.time()
    dec = DecoderProxy("/models/qwen3-tts/qwen3_tts_decoder.fp16.onnx", onnx_provider="CPUExecutionProvider", chunk_size=2048)
    print(f"  DecoderProxy created in {time.time()-t0:.2f}s", flush=True)
    print(f"  Waiting for ready (20s timeout)...", flush=True)
    ready = dec.wait_until_ready(timeout=20)
    print(f"  ready={ready}, states={getattr(dec, 'ready_states', 'N/A')}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== Step 4: GGUF / llama.cpp ===", flush=True)
try:
    from qwen3_tts_gguf.inference import llama
    print(f"  llama module loaded: {dir(llama)}", flush=True)
    t_path = "qwen3-tts/qwen3_tts_talker.q5_k.gguf"
    p_path = "qwen3-tts/qwen3_tts_predictor.q8_0.gguf"
    print(f"  Loading talker from {t_path}...", flush=True)
    t0 = time.time()
    talker = llama.LlamaModel(t_path, n_gpu_layers=-1)
    print(f"  Talker loaded in {time.time()-t0:.2f}s", flush=True)
    print(f"  Loading predictor from {p_path}...", flush=True)
    t0 = time.time()
    predictor = llama.LlamaModel(p_path, n_gpu_layers=-1)
    print(f"  Predictor loaded in {time.time()-t0:.2f}s", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== Step 5: Full TTSEngine ===", flush=True)
try:
    from qwen3_tts_gguf.inference import TTSEngine
    t0 = time.time()
    engine = TTSEngine(model_dir="qwen3-tts", onnx_provider="CPUExecutionProvider")
    print(f"  Engine created in {time.time()-t0:.2f}s, ready={engine.ready}", flush=True)
    if engine.ready:
        stream = engine.create_stream(n_ctx=2048)
        print(f"  stream={stream}", flush=True)
except Exception as e:
    print(f"  FAIL: {e}", flush=True)
    traceback.print_exc()

print("\n=== DONE ===", flush=True)
