File size: 3,257 Bytes
3bad34d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { NextRequest, NextResponse } from "next/server";

interface HuggingFaceModel {
    id?: string;
    modelId?: string;
    tags?: string[];
}

type ModelOption = { id: string; name: string };

type CacheEntry = {
    timestamp: number;
    data: ModelOption[];
};

const CACHE_DURATION = 1000 * 60 * 5; // 5 minutes
const cache = new Map<string, CacheEntry>();

function getCacheKey(query: string, limit: number) {
    return `${query}::${limit}`;
}

function normalizeModelId(model: HuggingFaceModel) {
    return model.id || model.modelId || "";
}

function hasTag(model: HuggingFaceModel, tag: string) {
    return Array.isArray(model.tags) && model.tags.includes(tag);
}

function isExcludedFormat(model: HuggingFaceModel) {
    if (!Array.isArray(model.tags)) return false;
    const tags = new Set(model.tags);
    if (tags.has("gguf")) return true;
    if (tags.has("mlx")) return true;
    if (tags.has("ggml")) return true;
    return false;
}

export async function GET(request: NextRequest) {
    const query = request.nextUrl.searchParams.get("q")?.trim() ?? "";
    const limitParam = Number(request.nextUrl.searchParams.get("limit") ?? "20");
    const limit = Number.isFinite(limitParam) ? Math.max(1, Math.min(50, limitParam)) : 20;

    const cacheKey = getCacheKey(query.toLowerCase(), limit);
    const now = Date.now();

    const cached = cache.get(cacheKey);
    if (cached && now - cached.timestamp < CACHE_DURATION) {
        return NextResponse.json(cached.data);
    }

    try {
        const url = new URL("https://huggingface.co/api/models");

        if (query) {
            url.searchParams.set("search", query);
        } else {
            url.searchParams.set("sort", "downloads");
            url.searchParams.set("direction", "-1");
        }

        // Restrict to models that have the `safetensors` tag.
        url.searchParams.append("filter", "safetensors");

        url.searchParams.set("limit", String(limit));

        const response = await fetch(url.toString(), {
            headers: {
                Accept: "application/json",
            },
            next: { revalidate: 300 },
        });

        if (!response.ok) {
            throw new Error(`Hugging Face API error: ${response.status}`);
        }

        const data = (await response.json()) as HuggingFaceModel[];

        const seen = new Set<string>();
        const models: ModelOption[] = Array.isArray(data)
            ? data
                .filter((m) => hasTag(m, "safetensors") && !isExcludedFormat(m))
                .map((m) => normalizeModelId(m))
                .filter(Boolean)
                .filter((id) => {
                    if (seen.has(id)) return false;
                    seen.add(id);
                    return true;
                })
                .map((id) => ({
                    id,
                    name: id,
                }))
            : [];

        cache.set(cacheKey, { timestamp: now, data: models });

        return NextResponse.json(models);
    } catch (error) {
        console.error("Error fetching Hugging Face models:", error);
        if (cached) {
            return NextResponse.json(cached.data);
        }
        return NextResponse.json([], { status: 500 });
    }
}