Spaces:
Running
Running
const fs = require('fs'); | |
// Read and Parse JSON Data | |
const data = JSON.parse(fs.readFileSync(__dirname+'/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 | |
} | |
function queryCommunityModels(queryParams) { | |
let result = data; | |
if (queryParams.sortBy) { | |
result = sortData(result, queryParams.sortBy); | |
} | |
if (queryParams.filterBy && queryParams.threshold) { | |
result = filterData(result, queryParams.filterBy, queryParams.threshold ? | |
parseFloat(queryParams.threshold) | |
: | |
0.5 | |
); | |
} | |
if (queryParams.optimized_only === 'true') { | |
result = filterOptimizedOnly(result); | |
} | |
console.log(result.length, " items returned"); | |
return(makeReadable(result)); | |
} | |
module.exports={queryCommunityModels} |