Spaces:
Running
Running
import { HF_TOKEN } from "$env/static/private"; | |
import { json, error } from "@sveltejs/kit"; | |
if (!HF_TOKEN) { | |
console.error("HF_TOKEN environment variable not set."); | |
throw error(500, "Server configuration error: Missing API Token"); | |
} | |
/** @type {import('./$types').RequestHandler} */ | |
export async function POST({ request }) { | |
const { query } = await request.json(); | |
if (!query) { | |
throw error(400, "Missing search query in request body"); | |
} | |
console.log(`API: Searching Fal LoRAs for query: "${query}"`); | |
const searchParams = new URLSearchParams({ | |
filter: "lora", | |
limit: "1000", // Increase limit slightly for UI | |
config: "false", | |
full: "true", | |
inference_provider: "fal-ai", | |
search: query, | |
}); | |
const url = `https://huggingface.co/api/models?${searchParams.toString()}`; | |
try { | |
const response = await fetch(url, { | |
method: "GET", | |
headers: { Authorization: `Bearer ${HF_TOKEN}` }, | |
}); | |
if (!response.ok) { | |
const errorText = await response.text(); | |
console.error(`Hugging Face API Error: ${response.status} ${errorText}`); | |
throw error( | |
response.status, | |
`Error fetching LoRAs from Hugging Face: ${errorText}` | |
); | |
} | |
const data = await response.json(); | |
const loras = data.map((model) => { | |
// Find the first image file in siblings for the thumbnail | |
const imageSibling = model.siblings?.find((sibling) => | |
/\.(png|jpg|jpeg|webp)$/i.test(sibling.rfilename) | |
); | |
const thumbnailUrl = imageSibling | |
? `https://huggingface.co/${model.modelId}/resolve/main/${imageSibling.rfilename}` | |
: null; // Or a default placeholder image URL | |
if (!thumbnailUrl) { | |
const thumbnailUrl = | |
"https://huggingface.co/front/assets/huggingface_logo-noborder.svg"; | |
} | |
return { | |
id: model.modelId, | |
author: model.author, | |
thumbnail: thumbnailUrl, | |
// Add other fields if needed later | |
}; | |
}); | |
console.log( | |
"API: Returning LoRAs:", | |
JSON.stringify(loras.slice(0, 5), null, 2) | |
); // Log first 5 results | |
return json(loras); | |
} catch (err) { | |
console.error("Error in /api/search-loras:", err); | |
// Throw SvelteKit error to ensure proper HTTP response | |
if (err.status) throw err; // Re-throw SvelteKit errors | |
throw error(500, "Internal Server Error"); | |
} | |
} | |