Spaces:
Running
Running
Update generator_function/image_processor.py
Browse files
generator_function/image_processor.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os, zipfile, tempfile, logging
|
| 2 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 3 |
from typing import List, Tuple, Optional
|
| 4 |
|
|
@@ -29,6 +29,7 @@ def process_zip_and_generate_images(
|
|
| 29 |
existing_images: Optional[List[str]],
|
| 30 |
blur: bool,
|
| 31 |
uid: str,
|
|
|
|
| 32 |
) -> List[str]:
|
| 33 |
num_images = 1 if demo_mode else num_images
|
| 34 |
try:
|
|
@@ -39,7 +40,7 @@ def process_zip_and_generate_images(
|
|
| 39 |
image_files = [(os.path.basename(zip_path), zip_path)]
|
| 40 |
|
| 41 |
results = process_image_files(
|
| 42 |
-
image_files, category, size, quality, user_prompt, sentiment, platform, num_images, blur, uid
|
| 43 |
)
|
| 44 |
all_urls = [url for entry in results for url in entry["urls"]]
|
| 45 |
seen, deduped = set(), []
|
|
@@ -73,13 +74,14 @@ def get_valid_image_files(temp_dir: tempfile.TemporaryDirectory) -> List[Tuple[s
|
|
| 73 |
|
| 74 |
def process_image_files(image_files: List[Tuple[str, str]],category: str,size: str,
|
| 75 |
quality: str,user_prompt: str,sentiment: str,platform: str,num_images: int,blur: bool,uid: str,
|
|
|
|
| 76 |
) -> List[dict]:
|
| 77 |
final_results: List[dict] = []
|
| 78 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 79 |
futures = []
|
| 80 |
for file_name, file_path in image_files:
|
| 81 |
job_id: Optional[str] = None
|
| 82 |
-
if COL is not None:
|
| 83 |
try:
|
| 84 |
settings = {"size": size,"quality": quality,"sentiment": sentiment,"platform": platform,"num_images": num_images,"blur": bool(blur)}
|
| 85 |
inputs = {"file_name": file_name, "mode": "img_or_zip"}
|
|
@@ -98,7 +100,7 @@ def process_image_files(image_files: List[Tuple[str, str]],category: str,size: s
|
|
| 98 |
futures.append(
|
| 99 |
executor.submit(
|
| 100 |
process_single_image,
|
| 101 |
-
file_name, file_path, category, size, quality, user_prompt, sentiment, platform, num_images, blur, job_id,
|
| 102 |
)
|
| 103 |
)
|
| 104 |
for future in as_completed(futures):
|
|
@@ -110,12 +112,12 @@ def process_image_files(image_files: List[Tuple[str, str]],category: str,size: s
|
|
| 110 |
return final_results
|
| 111 |
|
| 112 |
def process_single_image(file_name: str,file_path: str,category: str,size: str,quality: str,user_prompt: str,sentiment: str,
|
| 113 |
-
platform: str,num_images: int,blur: bool,job_id: Optional[str],) -> Optional[dict]:
|
| 114 |
try:
|
| 115 |
image_urls = generate_images_from_prompts(
|
| 116 |
-
file_path, size, quality, category, sentiment, user_prompt, platform, num_images, blur
|
| 117 |
)
|
| 118 |
-
if COL is not None and job_id:
|
| 119 |
try:
|
| 120 |
finish_job(COL, job_id, status=("completed" if image_urls else "failed"), outputs_urls=image_urls)
|
| 121 |
except Exception:
|
|
@@ -125,7 +127,7 @@ def process_single_image(file_name: str,file_path: str,category: str,size: str,q
|
|
| 125 |
return None
|
| 126 |
except Exception as e:
|
| 127 |
logger.error(f"Processing failed for {file_name}: {e}")
|
| 128 |
-
if COL is not None and job_id:
|
| 129 |
try:
|
| 130 |
finish_job(COL, job_id, status="failed", outputs_urls=[])
|
| 131 |
except Exception:
|
|
@@ -134,7 +136,7 @@ def process_single_image(file_name: str,file_path: str,category: str,size: str,q
|
|
| 134 |
|
| 135 |
def generate_images_from_prompts(
|
| 136 |
file_path: str, size: str, quality: str, category: str, sentiment: str, user_prompt: str,
|
| 137 |
-
platform: str, num_images: int, blur: bool,
|
| 138 |
) -> List[str]:
|
| 139 |
image_urls: List[str] = []
|
| 140 |
|
|
@@ -143,6 +145,8 @@ def generate_images_from_prompts(
|
|
| 143 |
image_bytes = generate_image(file_path, size, quality, category, sentiment, user_prompt, platform, blur, i)
|
| 144 |
if not image_bytes: return None
|
| 145 |
image_with_metadata = meta_data_helper_function(image_bytes)
|
|
|
|
|
|
|
| 146 |
s3_url = upload_image_to_r2(image_with_metadata)
|
| 147 |
return s3_url
|
| 148 |
except Exception as e:
|
|
|
|
| 1 |
+
import os, zipfile, tempfile, logging, base64
|
| 2 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 3 |
from typing import List, Tuple, Optional
|
| 4 |
|
|
|
|
| 29 |
existing_images: Optional[List[str]],
|
| 30 |
blur: bool,
|
| 31 |
uid: str,
|
| 32 |
+
private_mode: bool = False,
|
| 33 |
) -> List[str]:
|
| 34 |
num_images = 1 if demo_mode else num_images
|
| 35 |
try:
|
|
|
|
| 40 |
image_files = [(os.path.basename(zip_path), zip_path)]
|
| 41 |
|
| 42 |
results = process_image_files(
|
| 43 |
+
image_files, category, size, quality, user_prompt, sentiment, platform, num_images, blur, uid, private_mode
|
| 44 |
)
|
| 45 |
all_urls = [url for entry in results for url in entry["urls"]]
|
| 46 |
seen, deduped = set(), []
|
|
|
|
| 74 |
|
| 75 |
def process_image_files(image_files: List[Tuple[str, str]],category: str,size: str,
|
| 76 |
quality: str,user_prompt: str,sentiment: str,platform: str,num_images: int,blur: bool,uid: str,
|
| 77 |
+
private_mode: bool,
|
| 78 |
) -> List[dict]:
|
| 79 |
final_results: List[dict] = []
|
| 80 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 81 |
futures = []
|
| 82 |
for file_name, file_path in image_files:
|
| 83 |
job_id: Optional[str] = None
|
| 84 |
+
if COL is not None and not private_mode:
|
| 85 |
try:
|
| 86 |
settings = {"size": size,"quality": quality,"sentiment": sentiment,"platform": platform,"num_images": num_images,"blur": bool(blur)}
|
| 87 |
inputs = {"file_name": file_name, "mode": "img_or_zip"}
|
|
|
|
| 100 |
futures.append(
|
| 101 |
executor.submit(
|
| 102 |
process_single_image,
|
| 103 |
+
file_name, file_path, category, size, quality, user_prompt, sentiment, platform, num_images, blur, job_id, private_mode,
|
| 104 |
)
|
| 105 |
)
|
| 106 |
for future in as_completed(futures):
|
|
|
|
| 112 |
return final_results
|
| 113 |
|
| 114 |
def process_single_image(file_name: str,file_path: str,category: str,size: str,quality: str,user_prompt: str,sentiment: str,
|
| 115 |
+
platform: str,num_images: int,blur: bool,job_id: Optional[str], private_mode: bool) -> Optional[dict]:
|
| 116 |
try:
|
| 117 |
image_urls = generate_images_from_prompts(
|
| 118 |
+
file_path, size, quality, category, sentiment, user_prompt, platform, num_images, blur, private_mode
|
| 119 |
)
|
| 120 |
+
if COL is not None and job_id and not private_mode:
|
| 121 |
try:
|
| 122 |
finish_job(COL, job_id, status=("completed" if image_urls else "failed"), outputs_urls=image_urls)
|
| 123 |
except Exception:
|
|
|
|
| 127 |
return None
|
| 128 |
except Exception as e:
|
| 129 |
logger.error(f"Processing failed for {file_name}: {e}")
|
| 130 |
+
if COL is not None and job_id and not private_mode:
|
| 131 |
try:
|
| 132 |
finish_job(COL, job_id, status="failed", outputs_urls=[])
|
| 133 |
except Exception:
|
|
|
|
| 136 |
|
| 137 |
def generate_images_from_prompts(
|
| 138 |
file_path: str, size: str, quality: str, category: str, sentiment: str, user_prompt: str,
|
| 139 |
+
platform: str, num_images: int, blur: bool, private_mode: bool,
|
| 140 |
) -> List[str]:
|
| 141 |
image_urls: List[str] = []
|
| 142 |
|
|
|
|
| 145 |
image_bytes = generate_image(file_path, size, quality, category, sentiment, user_prompt, platform, blur, i)
|
| 146 |
if not image_bytes: return None
|
| 147 |
image_with_metadata = meta_data_helper_function(image_bytes)
|
| 148 |
+
if private_mode:
|
| 149 |
+
return "data:image/png;base64," + base64.b64encode(image_with_metadata).decode("utf-8")
|
| 150 |
s3_url = upload_image_to_r2(image_with_metadata)
|
| 151 |
return s3_url
|
| 152 |
except Exception as e:
|