File size: 1,404 Bytes
e5a1d97
 
242ffe8
e5a1d97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc1e12
e5a1d97
 
bdc1e12
 
e5a1d97
 
bdc1e12
 
 
e5a1d97
 
 
 
 
bdc1e12
e5a1d97
 
 
bdc1e12
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
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
}

async 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}