Spaces:
Running
Running
File size: 1,412 Bytes
e5a1d97 |
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 |
const express = require('express');
const fs = require('fs');
// Read and Parse JSON Data
const data = JSON.parse(fs.readFileSync('diffusers-unfiltered.json', 'utf8'));
const makeReadable=(data)=>{
return data.map(item=>{
return {
id: item.id,
class: item.class,
tags: item.meta.tags|| [],
activation_phrase: item.meta.instance_prompt || "not required",
example_prompts:item.meta.widget || "none provided"
}
})
}
// Sorting Function
function sortData(data, sortBy) {
return data.sort((a, b) => b[sortBy] - a[sortBy]);
}
// Filtering Functions
function filterData(data, filterBy, threshold=0.5) {
return data.filter(item => item["class"][filterBy] >= threshold);
}
function filterOptimizedOnly(data) {
data= data.filter(item => item.meta.inference)
sortData(data, "downloads");
return data
}
async function queryCommunityModels(req, res) {
let result = data;
if (req.query.sortBy) {
result = sortData(result, req.query.sortBy);
}
if (req.query.filterBy && req.query.threshold) {
result = filterData(result, req.query.filterBy, req.query.threshold ?
parseFloat(req.query.threshold)
:
0.5
);
}
if (req.query.optimized_only === 'true') {
result = filterOptimizedOnly(result);
}
console.log(result.length, " items returned");
res.json(makeReadable(result));
}
module.exports={queryCommunityModels} |