File size: 1,611 Bytes
17b22d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Live metadata from the Hub, for the panels that exist to show it.

Separate from `catalog.py` because these calls can be slow or fail: the app
must start and chat without them.
"""

from __future__ import annotations

import httpx

HUB = "https://huggingface.co/api/models"


async def model_info(model_id: str, token: str | None) -> dict:
    """Provider routing and card metadata for one model."""
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    params = {"expand[]": ["inferenceProviderMapping", "pipeline_tag", "gated", "downloads"]}
    try:
        async with httpx.AsyncClient(timeout=15.0) as client:
            r = await client.get(f"{HUB}/{model_id}", headers=headers, params=params)
            if r.status_code >= 400:
                return {"id": model_id, "error": f"hub returned {r.status_code}"}
            data = r.json()
    except httpx.HTTPError as e:
        return {"id": model_id, "error": f"{type(e).__name__}: {e}"}

    mapping = data.get("inferenceProviderMapping") or {}
    providers = []
    if isinstance(mapping, dict):
        for name, spec in mapping.items():
            providers.append({
                "provider": name,
                "status": spec.get("status"),
                "providerId": spec.get("providerId"),
                "task": spec.get("task"),
                "firstParty": bool(spec.get("isModelAuthor")),
            })
    return {
        "id": model_id,
        "pipeline": data.get("pipeline_tag"),
        "gated": data.get("gated"),
        "downloads": data.get("downloads"),
        "providers": providers,
    }