"""Asenkron eslenikli cikarim — birden fazla gorseli paralel isle.

asyncio.gather ile ayni anda birden fazla cikarim istegi gonderilir.
Yuksek throughput gerektiren pipeline'lar icin ideal.

Kullanim:
    python 07_async_pipeline.py
"""

import asyncio
from pathlib import Path

from evren_sdk import AsyncEvrenClient


async def process_image(client: AsyncEvrenClient, img: str, model: str):
    result = await client.predict(model, img, confidence=0.25)
    return Path(img).name, result


async def main():
    images = [f"img_{i}.jpg" for i in range(1, 6)]
    model = "kullanici/smoke-detection"

    async with AsyncEvrenClient(api_key="evren_xxxxx") as client:

        # --- warmup ---
        await client.warmup([model])
        print("warmup tamam\n")

        # --- paralel cikarim ---
        tasks = [process_image(client, img, model) for img in images]
        results = await asyncio.gather(*tasks, return_exceptions=True)

        for item in results:
            if isinstance(item, Exception):
                print(f"  HATA: {item}")
                continue
            name, r = item
            print(f"  {name}: {r.count} tespit, {r.inference_ms:.0f} ms")

        # --- async batch alternatifi ---
        batch = await client.predict_batch(model, images[:3])
        print(f"\nbatch: {batch.count} gorsel, {batch.total_ms:.0f} ms")

        # --- model bilgileri ---
        models = await client.list_models()
        print(f"\n{len(models)} model mevcut:")
        for m in models[:5]:
            print(f"  {m.full_slug} — {m.architecture}")


asyncio.run(main())
