radames commited on
Commit
650a247
1 Parent(s): 8e9514a

try again on model cards every cicle

Browse files
Files changed (1) hide show
  1. app.py +42 -13
app.py CHANGED
@@ -54,19 +54,22 @@ database = Database(DB_FOLDER)
54
 
55
  async def upload_resize_image_url(session, image_url):
56
  print(f"Uploading image {image_url}")
57
- async with session.get(image_url) as response:
58
- if response.status == 200 and response.headers['content-type'].startswith('image'):
59
- image = Image.open(BytesIO(await response.read())).convert('RGB')
60
- # resize image proportional
61
- image = ImageOps.fit(image, (400, 400), Image.LANCZOS)
62
- image_bytes = BytesIO()
63
- image.save(image_bytes, format="JPEG")
64
- image_bytes.seek(0)
65
- fname = f'{uuid.uuid4()}.jpg'
66
- s3.upload_fileobj(Fileobj=image_bytes, Bucket=AWS_S3_BUCKET_NAME, Key="diffusers-gallery/" + fname,
67
- ExtraArgs={"ContentType": "image/jpeg", "CacheControl": "max-age=31536000"})
68
- return fname
69
- return None
 
 
 
70
 
71
 
72
  def fetch_models(page=0):
@@ -168,6 +171,7 @@ async def sync_data():
168
  downloads = model['downloads']
169
  model_card = fetch_model_card(model_id)
170
  images = await find_image_in_model_card(model_card)
 
171
  classifier = run_classifier(images)
172
  # update model row with image and classifier data
173
  with database.get_db() as db:
@@ -183,6 +187,31 @@ async def sync_data():
183
  downloads
184
  ])
185
  db.commit()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  print("Update likes and downloads")
187
  for model in tqdm(all_models):
188
  model_id = model['id']
 
54
 
55
  async def upload_resize_image_url(session, image_url):
56
  print(f"Uploading image {image_url}")
57
+ try:
58
+ async with session.get(image_url) as response:
59
+ if response.status == 200 and (response.headers['content-type'].startswith('image') or response.headers['content-type'].startswith('application')):
60
+ image = Image.open(BytesIO(await response.read())).convert('RGB')
61
+ # resize image proportional
62
+ image = ImageOps.fit(image, (400, 400), Image.LANCZOS)
63
+ image_bytes = BytesIO()
64
+ image.save(image_bytes, format="JPEG")
65
+ image_bytes.seek(0)
66
+ fname = f'{uuid.uuid4()}.jpg'
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
 
75
  def fetch_models(page=0):
 
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:
 
187
  downloads
188
  ])
189
  db.commit()
190
+ print("Try to update images again")
191
+ with database.get_db() as db:
192
+ cursor = db.cursor()
193
+ cursor.execute("SELECT * from models WHERE json_array_length(data, '$.images') < 1;")
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']