less load: inference shouldn't take longer than 3 seconds...
Browse files- utils/hf_logger.py +20 -16
utils/hf_logger.py
CHANGED
@@ -5,13 +5,13 @@ import io
|
|
5 |
import datetime
|
6 |
from PIL import Image
|
7 |
import logging
|
8 |
-
from huggingface_hub import HfApi, CommitScheduler
|
9 |
import numpy as np
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
HF_DATASET_NAME = "aiwithoutborders-xyz/degentic_rd0"
|
14 |
-
LOCAL_LOG_DIR = "./hf_inference_logs"
|
15 |
|
16 |
# Custom JSON Encoder to handle numpy types
|
17 |
class NumpyEncoder(json.JSONEncoder):
|
@@ -20,18 +20,21 @@ class NumpyEncoder(json.JSONEncoder):
|
|
20 |
return float(obj)
|
21 |
return json.JSONEncoder.default(self, obj)
|
22 |
|
23 |
-
def
|
24 |
-
"""
|
25 |
-
# Explicitly check if the input is a PIL Image
|
26 |
if not isinstance(image, Image.Image):
|
27 |
raise TypeError(f"Expected a PIL Image, but received type: {type(image)}")
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
if image.mode != 'RGB':
|
32 |
image = image.convert('RGB')
|
33 |
-
image.save(
|
34 |
-
|
|
|
35 |
|
36 |
# The initialize_dataset function will change significantly or be removed/simplified
|
37 |
# as we are no longer appending to a datasets.Dataset object directly in memory
|
@@ -59,26 +62,26 @@ def log_inference_data(
|
|
59 |
try:
|
60 |
api = initialize_dataset_repo() # Get or create the repository
|
61 |
|
62 |
-
|
63 |
|
64 |
-
|
65 |
for img_item in forensic_images:
|
66 |
if img_item is not None:
|
67 |
if not isinstance(img_item, Image.Image):
|
68 |
try:
|
69 |
img_item = Image.fromarray(img_item)
|
70 |
except Exception as e:
|
71 |
-
logger.error(f"Error converting forensic image to PIL for
|
72 |
continue
|
73 |
-
|
74 |
|
75 |
new_entry = {
|
76 |
"timestamp": datetime.datetime.now().isoformat(),
|
77 |
-
"image":
|
78 |
"inference_request": inference_params,
|
79 |
"model_predictions": model_predictions,
|
80 |
"ensemble_output": ensemble_output,
|
81 |
-
"forensic_outputs":
|
82 |
"agent_monitoring_data": agent_monitoring_data,
|
83 |
"human_feedback": human_feedback if human_feedback is not None else {}
|
84 |
}
|
@@ -102,7 +105,8 @@ def log_inference_data(
|
|
102 |
every=10 # Commit every 10 files
|
103 |
)
|
104 |
|
105 |
-
|
|
|
106 |
|
107 |
except Exception as e:
|
108 |
logger.error(f"Failed to log inference data to local file: {e}")
|
|
|
5 |
import datetime
|
6 |
from PIL import Image
|
7 |
import logging
|
8 |
+
from huggingface_hub import HfApi, CommitScheduler
|
9 |
import numpy as np
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
HF_DATASET_NAME = "aiwithoutborders-xyz/degentic_rd0"
|
14 |
+
LOCAL_LOG_DIR = "./hf_inference_logs"
|
15 |
|
16 |
# Custom JSON Encoder to handle numpy types
|
17 |
class NumpyEncoder(json.JSONEncoder):
|
|
|
20 |
return float(obj)
|
21 |
return json.JSONEncoder.default(self, obj)
|
22 |
|
23 |
+
def _save_pil_image_to_file(image: Image.Image, directory: str, prefix: str) -> str:
|
24 |
+
"""Saves a PIL Image to a file and returns its filename."""
|
|
|
25 |
if not isinstance(image, Image.Image):
|
26 |
raise TypeError(f"Expected a PIL Image, but received type: {type(image)}")
|
27 |
|
28 |
+
os.makedirs(directory, exist_ok=True)
|
29 |
+
timestamp_str = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
|
30 |
+
filename = f"{prefix}_{timestamp_str}.png"
|
31 |
+
file_path = os.path.join(directory, filename)
|
32 |
+
|
33 |
if image.mode != 'RGB':
|
34 |
image = image.convert('RGB')
|
35 |
+
image.save(file_path, format="PNG")
|
36 |
+
logger.info(f"Saved image to: {file_path}")
|
37 |
+
return filename
|
38 |
|
39 |
# The initialize_dataset function will change significantly or be removed/simplified
|
40 |
# as we are no longer appending to a datasets.Dataset object directly in memory
|
|
|
62 |
try:
|
63 |
api = initialize_dataset_repo() # Get or create the repository
|
64 |
|
65 |
+
original_image_filename = _save_pil_image_to_file(original_image, LOCAL_LOG_DIR, "original")
|
66 |
|
67 |
+
forensic_images_filenames = []
|
68 |
for img_item in forensic_images:
|
69 |
if img_item is not None:
|
70 |
if not isinstance(img_item, Image.Image):
|
71 |
try:
|
72 |
img_item = Image.fromarray(img_item)
|
73 |
except Exception as e:
|
74 |
+
logger.error(f"Error converting forensic image to PIL for saving: {e}")
|
75 |
continue
|
76 |
+
forensic_images_filenames.append(_save_pil_image_to_file(img_item, LOCAL_LOG_DIR, "forensic"))
|
77 |
|
78 |
new_entry = {
|
79 |
"timestamp": datetime.datetime.now().isoformat(),
|
80 |
+
"image": original_image_filename,
|
81 |
"inference_request": inference_params,
|
82 |
"model_predictions": model_predictions,
|
83 |
"ensemble_output": ensemble_output,
|
84 |
+
"forensic_outputs": forensic_images_filenames,
|
85 |
"agent_monitoring_data": agent_monitoring_data,
|
86 |
"human_feedback": human_feedback if human_feedback is not None else {}
|
87 |
}
|
|
|
105 |
every=10 # Commit every 10 files
|
106 |
)
|
107 |
|
108 |
+
with scheduler:
|
109 |
+
logger.info(f"Inference data logged successfully to local file: {log_file_path}")
|
110 |
|
111 |
except Exception as e:
|
112 |
logger.error(f"Failed to log inference data to local file: {e}")
|