| 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; |
|
|
| 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 }); |
| } |
| } |
|
|