#!/usr/bin/env python3
"""Diagnose TTSEngine init inside qwen3-tts container."""
import sys, os, traceback

MODEL_DIR = "/models/qwen3-tts"

print("=== Step 1: Check model dir ===")
if os.path.isdir(MODEL_DIR):
    for f in sorted(os.listdir(MODEL_DIR)):
        fp = os.path.join(MODEL_DIR, f)
        if os.path.isfile(fp):
            print(f"  {f}  ({os.path.getsize(fp):,} bytes)")
        else:
            print(f"  {f}/")
else:
    print(f"  ERROR: {MODEL_DIR} does not exist!")
    sys.exit(1)

print("\n=== Step 2: Import qwen3_tts_gguf ===")
try:
    import qwen3_tts_gguf
    print(f"  OK: {qwen3_tts_gguf.__file__}")
except Exception as e:
    print(f"  FAIL: {e}")
    traceback.print_exc()
    sys.exit(1)

print("\n=== Step 3: Import TTSEngine ===")
try:
    from qwen3_tts_gguf.inference import TTSEngine
    print(f"  OK: {TTSEngine}")
except Exception as e:
    print(f"  FAIL: {e}")
    traceback.print_exc()
    sys.exit(1)

print("\n=== Step 4: Check inference bin dir ===")
bin_dir = os.path.join(os.path.dirname(qwen3_tts_gguf.__file__), "inference", "bin")
if os.path.isdir(bin_dir):
    for f in sorted(os.listdir(bin_dir)):
        print(f"  {f}")
else:
    print(f"  WARN: {bin_dir} does not exist")

print("\n=== Step 5: Init TTSEngine ===")
try:
    engine = TTSEngine(model_dir=MODEL_DIR)
    print(f"  OK: engine={engine}")
except Exception as e:
    print(f"  FAIL: {e}")
    traceback.print_exc()
    sys.exit(1)

print("\n=== DONE: Engine initialized successfully ===")
