Spaces:
Runtime error
Runtime error
push to dataset
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ from fastapi import FastAPI, BackgroundTasks
|
|
| 18 |
from fastapi_utils.tasks import repeat_every
|
| 19 |
from fastapi.middleware.cors import CORSMiddleware
|
| 20 |
import boto3
|
| 21 |
-
|
| 22 |
from db import Database
|
| 23 |
|
| 24 |
AWS_ACCESS_KEY_ID = os.getenv('MY_AWS_ACCESS_KEY_ID')
|
|
@@ -65,7 +65,7 @@ async def upload_resize_image_url(session, image_url):
|
|
| 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 |
-
|
| 69 |
return fname
|
| 70 |
except Exception as e:
|
| 71 |
print(f"Error uploading image {image_url}: {e}")
|
|
@@ -144,6 +144,7 @@ async def get_all_new_models():
|
|
| 144 |
|
| 145 |
async def sync_data():
|
| 146 |
print("Fetching models")
|
|
|
|
| 147 |
all_models = await get_all_new_models()
|
| 148 |
print(f"Found {len(all_models)} models")
|
| 149 |
# save list of all models for ids
|
|
@@ -190,8 +191,9 @@ async def sync_data():
|
|
| 190 |
print("Try to update images again")
|
| 191 |
with database.get_db() as db:
|
| 192 |
cursor = db.cursor()
|
| 193 |
-
cursor.execute(
|
| 194 |
-
|
|
|
|
| 195 |
for model in tqdm(models_no_images):
|
| 196 |
model_id = model['id']
|
| 197 |
model_data = json.loads(model['data'])
|
|
@@ -207,8 +209,6 @@ async def sync_data():
|
|
| 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,8 +221,9 @@ async def sync_data():
|
|
| 221 |
db.commit()
|
| 222 |
|
| 223 |
print("Updating DB repository")
|
| 224 |
-
|
| 225 |
-
|
|
|
|
| 226 |
|
| 227 |
|
| 228 |
app = FastAPI()
|
|
|
|
| 18 |
from fastapi_utils.tasks import repeat_every
|
| 19 |
from fastapi.middleware.cors import CORSMiddleware
|
| 20 |
import boto3
|
| 21 |
+
import datetime
|
| 22 |
from db import Database
|
| 23 |
|
| 24 |
AWS_ACCESS_KEY_ID = os.getenv('MY_AWS_ACCESS_KEY_ID')
|
|
|
|
| 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 |
except Exception as e:
|
| 71 |
print(f"Error uploading image {image_url}: {e}")
|
|
|
|
| 144 |
|
| 145 |
async def sync_data():
|
| 146 |
print("Fetching models")
|
| 147 |
+
repo.git_pull()
|
| 148 |
all_models = await get_all_new_models()
|
| 149 |
print(f"Found {len(all_models)} models")
|
| 150 |
# save list of all models for ids
|
|
|
|
| 191 |
print("Try to update images again")
|
| 192 |
with database.get_db() as db:
|
| 193 |
cursor = db.cursor()
|
| 194 |
+
cursor.execute(
|
| 195 |
+
"SELECT * from models WHERE json_array_length(data, '$.images') < 1;")
|
| 196 |
+
models_no_images = list(cursor.fetchall())
|
| 197 |
for model in tqdm(models_no_images):
|
| 198 |
model_id = model['id']
|
| 199 |
model_data = json.loads(model['data'])
|
|
|
|
| 209 |
[json.dumps(model_data), model_id])
|
| 210 |
db.commit()
|
| 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 |
+
time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 225 |
+
subprocess.Popen(
|
| 226 |
+
f"git add . && git commit --amend -m 'update at {time}' && git push --force", cwd=DB_FOLDER, shell=True)
|
| 227 |
|
| 228 |
|
| 229 |
app = FastAPI()
|