radames commited on
Commit
834bcf3
1 Parent(s): 1b35184

add filtering options

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import re
3
  import aiohttp
@@ -176,12 +177,12 @@ async def sync_data():
176
  "class": classifier
177
  })])
178
  db.commit()
179
-
180
- if(len(models) > 0):
181
  print("Updating repository")
182
  subprocess.Popen(
183
  "git add . && git commit --amend -m 'update' && git push --force", cwd=DB_FOLDER, shell=True)
184
- else:
185
  print("No new models found")
186
 
187
  app = FastAPI()
@@ -203,18 +204,31 @@ app.add_middleware(
203
  MAX_PAGE_SIZE = 30
204
 
205
 
 
 
 
 
 
 
206
  @ app.get("/api/models")
207
- def get_page(page: int = 1):
208
  page = page if page > 0 else 1
 
 
 
 
 
 
 
209
  with database.get_db() as db:
210
  cursor = db.cursor()
211
- cursor.execute("""
212
  SELECT *, COUNT(*) OVER() AS total
213
  FROM models
214
  WHERE json_extract(data, '$.likes') > 4
215
- ORDER BY datetime(json_extract(data, '$.lastModified')) DESC, json_extract(data, '$.likes') DESC
216
- LIMIT ? OFFSET ?
217
- """, (MAX_PAGE_SIZE, (page - 1) * MAX_PAGE_SIZE))
218
  results = cursor.fetchall()
219
  total = results[0]['total'] if results else 0
220
  total_pages = (total + MAX_PAGE_SIZE - 1) // MAX_PAGE_SIZE
@@ -230,8 +244,8 @@ def read_root():
230
  return "Just a bot to sync data from diffusers gallery"
231
 
232
 
233
- @app.on_event("startup")
234
- @repeat_every(seconds=60 * 60 * 24, wait_first=False)
235
- async def repeat_sync():
236
- await sync_data()
237
- return "Synced data to huggingface datasets"
 
1
+ from enum import Enum
2
  import os
3
  import re
4
  import aiohttp
 
177
  "class": classifier
178
  })])
179
  db.commit()
180
+
181
+ if (len(models) > 0):
182
  print("Updating repository")
183
  subprocess.Popen(
184
  "git add . && git commit --amend -m 'update' && git push --force", cwd=DB_FOLDER, shell=True)
185
+ else:
186
  print("No new models found")
187
 
188
  app = FastAPI()
 
204
  MAX_PAGE_SIZE = 30
205
 
206
 
207
+ class Sort(str, Enum):
208
+ trending = "trending"
209
+ recent = "recent"
210
+ likes = "likes"
211
+
212
+
213
  @ app.get("/api/models")
214
+ def get_page(page: int = 1, sort: Sort = Sort.trending):
215
  page = page if page > 0 else 1
216
+ if sort == Sort.trending:
217
+ sort_query = "json_extract(data, '$.likes') / POWER((JULIANDAY('now') - JULIANDAY(datetime(json_extract(data, '$.lastModified')))) + 2, 2) DESC"
218
+ elif sort == Sort.recent:
219
+ sort_query = "datetime(json_extract(data, '$.lastModified')) DESC"
220
+ elif sort == Sort.likes:
221
+ sort_query = "json_extract(data, '$.likes') DESC"
222
+
223
  with database.get_db() as db:
224
  cursor = db.cursor()
225
+ cursor.execute(f"""
226
  SELECT *, COUNT(*) OVER() AS total
227
  FROM models
228
  WHERE json_extract(data, '$.likes') > 4
229
+ ORDER BY {sort_query}
230
+ LIMIT {MAX_PAGE_SIZE} OFFSET {(page - 1) * MAX_PAGE_SIZE}
231
+ """)
232
  results = cursor.fetchall()
233
  total = results[0]['total'] if results else 0
234
  total_pages = (total + MAX_PAGE_SIZE - 1) // MAX_PAGE_SIZE
 
244
  return "Just a bot to sync data from diffusers gallery"
245
 
246
 
247
+ # @app.on_event("startup")
248
+ # @repeat_every(seconds=60 * 60 * 24, wait_first=True)
249
+ # async def repeat_sync():
250
+ # await sync_data()
251
+ # return "Synced data to huggingface datasets"