Spaces:
Running
on
Zero
Running
on
Zero
Create log.py
Browse files
log.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import boto3
|
2 |
+
import uuid
|
3 |
+
import time
|
4 |
+
import os
|
5 |
+
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
|
9 |
+
|
10 |
+
MAX_PIXELS = 2048
|
11 |
+
|
12 |
+
AWS_BUCKET_NAME = os.environ.get("AWS_BUCKET_NAME", "")
|
13 |
+
AWS_INFERENCE_LOG_TABLE = os.environ.get("AWS_INFERENCE_LOG_TABLE", "")
|
14 |
+
AWS_FEEDBACK_LOG_TABLE = os.environ.get("AWS_FEEDBACK_LOG_TABLE", "")
|
15 |
+
|
16 |
+
|
17 |
+
AWS_REGION = os.environ.get("AWS_REGION", "")
|
18 |
+
AWS_ACCESS_ID = os.environ.get("AWS_ACCESS_ID", "")
|
19 |
+
AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY", "")
|
20 |
+
|
21 |
+
|
22 |
+
aws_cfg = {
|
23 |
+
"aws_access_key_id": AWS_ACCESS_ID,
|
24 |
+
"aws_secret_access_key": AWS_ACCESS_KEY,
|
25 |
+
"region_name": AWS_REGION,
|
26 |
+
}
|
27 |
+
|
28 |
+
s3_client = boto3.client("s3", **aws_cfg)
|
29 |
+
dynamodb = boto3.resource("dynamodb", **aws_cfg)
|
30 |
+
|
31 |
+
inference_log = dynamodb.Table(AWS_INFERENCE_LOG_TABLE)
|
32 |
+
feedback_log = dynamodb.Table(AWS_FEEDBACK_LOG_TABLE)
|
33 |
+
|
34 |
+
|
35 |
+
def get_metadata():
|
36 |
+
return {
|
37 |
+
"_id": uuid.uuid4().hex,
|
38 |
+
"created_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
|
39 |
+
}
|
40 |
+
|
41 |
+
|
42 |
+
def insert_log(table_type: str, data: dict):
|
43 |
+
assert table_type in ["inference", "feedback"], "Invalid table type"
|
44 |
+
table = inference_log if table_type == "inference" else feedback_log
|
45 |
+
metadata = get_metadata()
|
46 |
+
response = table.put_item(
|
47 |
+
Item={
|
48 |
+
**data,
|
49 |
+
**metadata,
|
50 |
+
}
|
51 |
+
)
|
52 |
+
return response, metadata["_id"]
|
53 |
+
|
54 |
+
|
55 |
+
# Example usage:
|
56 |
+
# insert_log("inference", {"data": "test"})
|
57 |
+
# insert_log("feedback", {"data": "test"})
|
58 |
+
|
59 |
+
|
60 |
+
def get_image_obj(image: Image) -> BytesIO:
|
61 |
+
image.thumbnail((MAX_PIXELS, MAX_PIXELS))
|
62 |
+
image_obj = BytesIO()
|
63 |
+
image.save(image_obj, format="WEBP")
|
64 |
+
image_obj.seek(0)
|
65 |
+
return image_obj
|
66 |
+
|
67 |
+
|
68 |
+
def log_image(image: Image) -> str:
|
69 |
+
metadata = get_metadata()
|
70 |
+
image_obj = get_image_obj(image)
|
71 |
+
s3_key = f"images/{metadata['_id']}.webp"
|
72 |
+
s3_client.upload_fileobj(image_obj, AWS_BUCKET_NAME, s3_key)
|
73 |
+
return metadata["_id"]
|
74 |
+
|
75 |
+
|
76 |
+
# Example usage:
|
77 |
+
# image = Image.open("examples/doge.jpg")
|
78 |
+
# log_image(image)
|