MMAPI-2 / query_models_api.js
DeFactOfficial's picture
Upload 2 files
e5a1d97 verified
raw
history blame
1.41 kB
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}