File size: 1,449 Bytes
101ebaa |
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 |
import { NextResponse } from "next/server";
interface HFDataset {
id: string;
author: string;
downloads: number;
likes: number;
tags: string[];
}
let cachedDatasets: { id: string; name: string }[] | null = null;
let cacheTimestamp: number = 0;
const CACHE_DURATION = 1000 * 60 * 60; // 1 hour
export async function GET() {
try {
const now = Date.now();
if (cachedDatasets && now - cacheTimestamp < CACHE_DURATION) {
return NextResponse.json(cachedDatasets);
}
const response = await fetch(
"https://huggingface.co/api/datasets?author=TeichAI&limit=100",
{
headers: {
Accept: "application/json",
},
next: { revalidate: 3600 },
}
);
if (!response.ok) {
throw new Error(`HF API error: ${response.status}`);
}
const data: HFDataset[] = await response.json();
cachedDatasets = data.map((dataset) => ({
id: dataset.id,
name: dataset.id.replace("TeichAI/", ""),
}));
cacheTimestamp = now;
return NextResponse.json(cachedDatasets);
} catch (error) {
console.error("Error fetching TeichAI datasets:", error);
if (cachedDatasets) {
return NextResponse.json(cachedDatasets);
}
return NextResponse.json([], { status: 500 });
}
}
|