radames commited on
Commit
0708a05
1 Parent(s): 14a7251

move filter to backend

Browse files
Files changed (1) hide show
  1. app.py +28 -4
app.py CHANGED
@@ -224,8 +224,16 @@ class Sort(str, Enum):
224
  likes = "likes"
225
 
226
 
 
 
 
 
 
 
 
 
227
  @ app.get("/api/models")
228
- def get_page(page: int = 1, sort: Sort = Sort.trending):
229
  page = page if page > 0 else 1
230
  if sort == Sort.trending:
231
  sort_query = "((likes + downloads)/2) / MYPOWER((JULIANDAY('now') - JULIANDAY(datetime(json_extract(data, '$.lastModified')))) + 2, 1.5) DESC"
@@ -233,13 +241,27 @@ def get_page(page: int = 1, sort: Sort = Sort.trending):
233
  sort_query = "datetime(json_extract(data, '$.lastModified')) DESC"
234
  elif sort == Sort.likes:
235
  sort_query = "likes DESC"
 
 
 
 
 
 
 
 
 
 
236
 
237
  with database.get_db() as db:
238
  cursor = db.cursor()
239
  cursor.execute(f"""
240
- SELECT *, COUNT(*) OVER() AS total
241
- FROM models
242
- WHERE likes > 4
 
 
 
 
243
  ORDER BY {sort_query}
244
  LIMIT {MAX_PAGE_SIZE} OFFSET {(page - 1) * MAX_PAGE_SIZE}
245
  """)
@@ -248,10 +270,12 @@ def get_page(page: int = 1, sort: Sort = Sort.trending):
248
  total_pages = (total + MAX_PAGE_SIZE - 1) // MAX_PAGE_SIZE
249
  models_data = []
250
  for result in results:
 
251
  data = json.loads(result['data'])
252
  # update downloads and likes from db table
253
  data['downloads'] = result['downloads']
254
  data['likes'] = result['likes']
 
255
  models_data.append(data)
256
 
257
  return {
 
224
  likes = "likes"
225
 
226
 
227
+ class Style(str, Enum):
228
+ all = "all"
229
+ anime = "anime"
230
+ s3D = "3d"
231
+ realistic = "realistic"
232
+ nsfw = "nsfw"
233
+
234
+
235
  @ app.get("/api/models")
236
+ def get_page(page: int = 1, sort: Sort = Sort.trending, style: Style = Style.all):
237
  page = page if page > 0 else 1
238
  if sort == Sort.trending:
239
  sort_query = "((likes + downloads)/2) / MYPOWER((JULIANDAY('now') - JULIANDAY(datetime(json_extract(data, '$.lastModified')))) + 2, 1.5) DESC"
 
241
  sort_query = "datetime(json_extract(data, '$.lastModified')) DESC"
242
  elif sort == Sort.likes:
243
  sort_query = "likes DESC"
244
+ if style == Style.all:
245
+ style_query = "1"
246
+ elif style == Style.anime:
247
+ style_query = "json_extract(data, '$.class.anime') > 0.1 AND isNFSW = false"
248
+ elif style == Style.s3D:
249
+ style_query = "json_extract(data, '$.class.3d') > 0.1 AND isNFSW = false"
250
+ elif style == Style.realistic:
251
+ style_query = "json_extract(data, '$.class.real_life') > 0.1 AND isNFSW = false"
252
+ elif style == Style.nsfw:
253
+ style_query = "isNFSW = true"
254
 
255
  with database.get_db() as db:
256
  cursor = db.cursor()
257
  cursor.execute(f"""
258
+ SELECT *, COUNT(*) OVER() AS total, isNFSW
259
+ FROM (
260
+ SELECT * ,
261
+ json_extract(data, '$.class.explicit') > 0.3 OR json_extract(data, '$.class.suggestive') > 0.3 AS isNFSW
262
+ FROM models
263
+ )
264
+ WHERE likes > 0 AND {style_query}
265
  ORDER BY {sort_query}
266
  LIMIT {MAX_PAGE_SIZE} OFFSET {(page - 1) * MAX_PAGE_SIZE}
267
  """)
 
270
  total_pages = (total + MAX_PAGE_SIZE - 1) // MAX_PAGE_SIZE
271
  models_data = []
272
  for result in results:
273
+ print(list(result))
274
  data = json.loads(result['data'])
275
  # update downloads and likes from db table
276
  data['downloads'] = result['downloads']
277
  data['likes'] = result['likes']
278
+ data['isNFSW'] = bool(result['isNFSW'])
279
  models_data.append(data)
280
 
281
  return {