Spaces:
Sleeping
Sleeping
update with logging and error handling
Browse files- image_processor.py +31 -17
image_processor.py
CHANGED
|
@@ -9,6 +9,9 @@ from r2_uploader import upload_image_to_r2
|
|
| 9 |
from mongo_logger import create_log, update_log_status
|
| 10 |
from helpers import encode_image_to_base64, is_valid_image
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def process_zip_and_generate_images(
|
| 14 |
zip_path, category, size, quality, user_prompt,
|
|
@@ -33,25 +36,24 @@ def process_zip_and_generate_images(
|
|
| 33 |
return existing_images + modified_urls if existing_images else modified_urls
|
| 34 |
|
| 35 |
except Exception as e:
|
|
|
|
| 36 |
return [f"Error: {str(e)}"]
|
| 37 |
|
| 38 |
|
| 39 |
-
except Exception as e:
|
| 40 |
-
log_error(f"Global error in zip processing: {e}")
|
| 41 |
-
return [], [f"Global error: {e}"]
|
| 42 |
-
|
| 43 |
-
|
| 44 |
def extract_zip_file(zip_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
temp_dir = tempfile.TemporaryDirectory()
|
| 47 |
-
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 48 |
-
zip_ref.extractall(temp_dir.name)
|
| 49 |
-
|
| 50 |
-
return temp_dir
|
| 51 |
|
| 52 |
|
| 53 |
def get_valid_image_files(temp_dir):
|
| 54 |
-
|
| 55 |
valid_files = []
|
| 56 |
zip_contents = os.listdir(temp_dir.name)
|
| 57 |
|
|
@@ -59,10 +61,14 @@ def get_valid_image_files(temp_dir):
|
|
| 59 |
file_path = os.path.join(temp_dir.name, file)
|
| 60 |
if is_valid_image(file) and "__MACOSX" not in file:
|
| 61 |
valid_files.append((file, file_path))
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
return valid_files
|
| 64 |
|
| 65 |
|
|
|
|
| 66 |
def process_image_files(image_files, category, size, quality, user_prompt, sentiment, platform, num_images):
|
| 67 |
final_results = []
|
| 68 |
logs = []
|
|
@@ -92,9 +98,12 @@ def process_image_files(image_files, category, size, quality, user_prompt, senti
|
|
| 92 |
)
|
| 93 |
|
| 94 |
for future in as_completed(futures):
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
return final_results
|
| 100 |
|
|
@@ -121,19 +130,24 @@ def process_single_image(file_name, file_path, category, size, quality, user_pro
|
|
| 121 |
return None
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
update_log_status(log_id, "failed", urls=[], message=str(e))
|
| 126 |
return None
|
| 127 |
|
| 128 |
|
|
|
|
| 129 |
def generate_images_from_prompts(file_path, size, quality, category, sentiment, user_prompt, platform, num_images):
|
| 130 |
image_urls = []
|
| 131 |
|
| 132 |
for _ in range(num_images):
|
| 133 |
try:
|
| 134 |
img_url = generate_image(file_path, size, quality, category, sentiment, user_prompt, platform)
|
| 135 |
-
|
| 136 |
s3_url = upload_image_to_r2(img_url)
|
|
|
|
|
|
|
| 137 |
image_urls.append(s3_url)
|
| 138 |
|
| 139 |
except Exception as e:
|
|
@@ -143,4 +157,4 @@ def generate_images_from_prompts(file_path, size, quality, category, sentiment,
|
|
| 143 |
|
| 144 |
|
| 145 |
def log_error(message):
|
| 146 |
-
|
|
|
|
| 9 |
from mongo_logger import create_log, update_log_status
|
| 10 |
from helpers import encode_image_to_base64, is_valid_image
|
| 11 |
|
| 12 |
+
import logging
|
| 13 |
+
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
def process_zip_and_generate_images(
|
| 17 |
zip_path, category, size, quality, user_prompt,
|
|
|
|
| 36 |
return existing_images + modified_urls if existing_images else modified_urls
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
+
logger.exception(f"Global error during processing file: {zip_path}")
|
| 40 |
return [f"Error: {str(e)}"]
|
| 41 |
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def extract_zip_file(zip_path):
|
| 44 |
+
try:
|
| 45 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 46 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 47 |
+
zip_ref.extractall(temp_dir.name)
|
| 48 |
+
logger.info(f"Extracted ZIP file: {zip_path}")
|
| 49 |
+
return temp_dir
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logger.exception(f"Failed to extract ZIP file: {zip_path}")
|
| 52 |
+
raise
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
def get_valid_image_files(temp_dir):
|
|
|
|
| 57 |
valid_files = []
|
| 58 |
zip_contents = os.listdir(temp_dir.name)
|
| 59 |
|
|
|
|
| 61 |
file_path = os.path.join(temp_dir.name, file)
|
| 62 |
if is_valid_image(file) and "__MACOSX" not in file:
|
| 63 |
valid_files.append((file, file_path))
|
| 64 |
+
else:
|
| 65 |
+
logger.warning(f"Ignored non-image or unwanted file: {file}")
|
| 66 |
|
| 67 |
+
logger.info(f"Found {len(valid_files)} valid images.")
|
| 68 |
return valid_files
|
| 69 |
|
| 70 |
|
| 71 |
+
|
| 72 |
def process_image_files(image_files, category, size, quality, user_prompt, sentiment, platform, num_images):
|
| 73 |
final_results = []
|
| 74 |
logs = []
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
for future in as_completed(futures):
|
| 101 |
+
try:
|
| 102 |
+
result = future.result()
|
| 103 |
+
if result:
|
| 104 |
+
final_results.append(result)
|
| 105 |
+
except Exception as e:
|
| 106 |
+
logger.exception("Unhandled exception during image processing thread.")
|
| 107 |
|
| 108 |
return final_results
|
| 109 |
|
|
|
|
| 130 |
return None
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
+
error_msg = f"Processing failed for {file_name}: {e}"
|
| 134 |
+
logger.error(error_msg)
|
| 135 |
+
logs.append(error_msg)
|
| 136 |
update_log_status(log_id, "failed", urls=[], message=str(e))
|
| 137 |
return None
|
| 138 |
|
| 139 |
|
| 140 |
+
|
| 141 |
def generate_images_from_prompts(file_path, size, quality, category, sentiment, user_prompt, platform, num_images):
|
| 142 |
image_urls = []
|
| 143 |
|
| 144 |
for _ in range(num_images):
|
| 145 |
try:
|
| 146 |
img_url = generate_image(file_path, size, quality, category, sentiment, user_prompt, platform)
|
| 147 |
+
|
| 148 |
s3_url = upload_image_to_r2(img_url)
|
| 149 |
+
logger.info(f"Generated and uploaded image to: {s3_url}")
|
| 150 |
+
|
| 151 |
image_urls.append(s3_url)
|
| 152 |
|
| 153 |
except Exception as e:
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
def log_error(message):
|
| 160 |
+
logger.error(message)
|