radames commited on
Commit
b648312
1 Parent(s): 650a247
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -67,8 +67,8 @@ async def upload_resize_image_url(session, image_url):
67
  s3.upload_fileobj(Fileobj=image_bytes, Bucket=AWS_S3_BUCKET_NAME, Key="diffusers-gallery/" + fname,
68
  ExtraArgs={"ContentType": "image/jpeg", "CacheControl": "max-age=31536000"})
69
  return fname
70
-
71
  except Exception as e:
 
72
  return None
73
 
74
 
@@ -116,7 +116,6 @@ def run_classifier(images):
116
 
117
  # data response is array data:[[{img0}, {img1}, {img2}...], Label, Gallery],
118
  class_data = response['data'][0][0]
119
- print(class_data)
120
  class_data_parsed = {row['label']: round(
121
  row['score'], 3) for row in class_data}
122
 
@@ -171,8 +170,9 @@ async def sync_data():
171
  downloads = model['downloads']
172
  model_card = fetch_model_card(model_id)
173
  images = await find_image_in_model_card(model_card)
174
- images = [i for i in images if i is not None]
175
  classifier = run_classifier(images)
 
176
  # update model row with image and classifier data
177
  with database.get_db() as db:
178
  cursor = db.cursor()
@@ -194,24 +194,21 @@ async def sync_data():
194
  models_no_images =list(cursor.fetchall())
195
  for model in tqdm(models_no_images):
196
  model_id = model['id']
 
197
  print("Updating model", model_id)
198
  model_card = fetch_model_card(model_id)
199
  images = await find_image_in_model_card(model_card)
200
- images = [i for i in images if i is not None]
201
- print(images)
202
  classifier = run_classifier(images)
 
203
  # update model row with image and classifier data
204
  with database.get_db() as db:
205
  cursor = db.cursor()
206
- cursor.execute("UPDATE models SET data = json_set(data, '$.images', ?) WHERE id = ?",
207
- [json.dumps(images), model_id])
208
- cursor.execute("UPDATE models SET data = json_set(data, '$.class', ?) WHERE id = ?",
209
- [json.dumps(classifier), model_id])
210
  db.commit()
211
 
212
 
213
 
214
-
215
  print("Update likes and downloads")
216
  for model in tqdm(all_models):
217
  model_id = model['id']
@@ -224,8 +221,8 @@ async def sync_data():
224
  db.commit()
225
 
226
  print("Updating DB repository")
227
- subprocess.Popen(
228
- "git add . && git commit --amend -m 'update' && git push --force", cwd=DB_FOLDER, shell=True)
229
 
230
 
231
  app = FastAPI()
@@ -300,7 +297,6 @@ def get_page(page: int = 1, sort: Sort = Sort.trending, style: Style = Style.all
300
  total_pages = (total + MAX_PAGE_SIZE - 1) // MAX_PAGE_SIZE
301
  models_data = []
302
  for result in results:
303
- print(list(result))
304
  data = json.loads(result['data'])
305
  # update downloads and likes from db table
306
  data['downloads'] = result['downloads']
 
67
  s3.upload_fileobj(Fileobj=image_bytes, Bucket=AWS_S3_BUCKET_NAME, Key="diffusers-gallery/" + fname,
68
  ExtraArgs={"ContentType": "image/jpeg", "CacheControl": "max-age=31536000"})
69
  return fname
 
70
  except Exception as e:
71
+ print(f"Error uploading image {image_url}: {e}")
72
  return None
73
 
74
 
 
116
 
117
  # data response is array data:[[{img0}, {img1}, {img2}...], Label, Gallery],
118
  class_data = response['data'][0][0]
 
119
  class_data_parsed = {row['label']: round(
120
  row['score'], 3) for row in class_data}
121
 
 
170
  downloads = model['downloads']
171
  model_card = fetch_model_card(model_id)
172
  images = await find_image_in_model_card(model_card)
173
+
174
  classifier = run_classifier(images)
175
+ print(images, classifier)
176
  # update model row with image and classifier data
177
  with database.get_db() as db:
178
  cursor = db.cursor()
 
194
  models_no_images =list(cursor.fetchall())
195
  for model in tqdm(models_no_images):
196
  model_id = model['id']
197
+ model_data = json.loads(model['data'])
198
  print("Updating model", model_id)
199
  model_card = fetch_model_card(model_id)
200
  images = await find_image_in_model_card(model_card)
 
 
201
  classifier = run_classifier(images)
202
+
203
  # update model row with image and classifier data
204
  with database.get_db() as db:
205
  cursor = db.cursor()
206
+ cursor.execute("UPDATE models SET data = ? WHERE id = ?",
207
+ [json.dumps(model_data), model_id])
 
 
208
  db.commit()
209
 
210
 
211
 
 
212
  print("Update likes and downloads")
213
  for model in tqdm(all_models):
214
  model_id = model['id']
 
221
  db.commit()
222
 
223
  print("Updating DB repository")
224
+ # subprocess.Popen(
225
+ # "git add . && git commit --amend -m 'update' && git push --force", cwd=DB_FOLDER, shell=True)
226
 
227
 
228
  app = FastAPI()
 
297
  total_pages = (total + MAX_PAGE_SIZE - 1) // MAX_PAGE_SIZE
298
  models_data = []
299
  for result in results:
 
300
  data = json.loads(result['data'])
301
  # update downloads and likes from db table
302
  data['downloads'] = result['downloads']