Spaces:
Running
Running
Update generator_function/multimodel_image_processor.py
Browse files
generator_function/multimodel_image_processor.py
CHANGED
|
@@ -36,6 +36,7 @@ def process_zip_and_generate_images_multimodel(
|
|
| 36 |
uid: str,
|
| 37 |
selected_model: str = "gpt_default",
|
| 38 |
model_params: Optional[dict] = None,
|
|
|
|
| 39 |
) -> List[str]:
|
| 40 |
"""Enhanced image processor that supports both GPT and multimodel approaches"""
|
| 41 |
num_images = 1 if demo_mode else num_images
|
|
@@ -48,7 +49,7 @@ def process_zip_and_generate_images_multimodel(
|
|
| 48 |
|
| 49 |
results = process_image_files_multimodel(
|
| 50 |
image_files, category, size, quality, user_prompt, sentiment, platform,
|
| 51 |
-
num_images, blur, uid, selected_model, model_params
|
| 52 |
)
|
| 53 |
all_urls = [url for entry in results for url in entry["urls"]]
|
| 54 |
seen, deduped = set(), []
|
|
@@ -82,14 +83,14 @@ def get_valid_image_files(temp_dir: tempfile.TemporaryDirectory) -> List[Tuple[s
|
|
| 82 |
|
| 83 |
def process_image_files_multimodel(image_files: List[Tuple[str, str]], category: str, size: str,
|
| 84 |
quality: str, user_prompt: str, sentiment: str, platform: str, num_images: int, blur: bool,
|
| 85 |
-
uid: str, selected_model: str, model_params: Optional[dict]) -> List[dict]:
|
| 86 |
"""Process image files with multimodel support"""
|
| 87 |
final_results: List[dict] = []
|
| 88 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 89 |
futures = []
|
| 90 |
for file_name, file_path in image_files:
|
| 91 |
job_id: Optional[str] = None
|
| 92 |
-
if COL is not None:
|
| 93 |
try:
|
| 94 |
settings = {
|
| 95 |
"size": size, "quality": quality, "sentiment": sentiment,
|
|
@@ -112,7 +113,7 @@ def process_image_files_multimodel(image_files: List[Tuple[str, str]], category:
|
|
| 112 |
executor.submit(
|
| 113 |
process_single_image_multimodel,
|
| 114 |
file_name, file_path, category, size, quality, user_prompt, sentiment,
|
| 115 |
-
platform, num_images, blur, job_id, selected_model, model_params,
|
| 116 |
)
|
| 117 |
)
|
| 118 |
for future in as_completed(futures):
|
|
@@ -125,14 +126,14 @@ def process_image_files_multimodel(image_files: List[Tuple[str, str]], category:
|
|
| 125 |
|
| 126 |
def process_single_image_multimodel(file_name: str, file_path: str, category: str, size: str,
|
| 127 |
quality: str, user_prompt: str, sentiment: str, platform: str, num_images: int, blur: bool,
|
| 128 |
-
job_id: Optional[str], selected_model: str, model_params: Optional[dict]) -> Optional[dict]:
|
| 129 |
"""Process single image with multimodel support"""
|
| 130 |
try:
|
| 131 |
image_urls = generate_images_from_prompts_multimodel(
|
| 132 |
file_path, size, quality, category, sentiment, user_prompt, platform,
|
| 133 |
-
num_images, blur, selected_model, model_params
|
| 134 |
)
|
| 135 |
-
if COL is not None and job_id:
|
| 136 |
try:
|
| 137 |
finish_job(COL, job_id, status=("completed" if image_urls else "failed"), outputs_urls=image_urls)
|
| 138 |
except Exception:
|
|
@@ -142,7 +143,7 @@ def process_single_image_multimodel(file_name: str, file_path: str, category: st
|
|
| 142 |
return None
|
| 143 |
except Exception as e:
|
| 144 |
logger.error(f"Processing failed for {file_name}: {e}")
|
| 145 |
-
if COL is not None and job_id:
|
| 146 |
try:
|
| 147 |
finish_job(COL, job_id, status="failed", outputs_urls=[])
|
| 148 |
except Exception:
|
|
@@ -152,6 +153,7 @@ def process_single_image_multimodel(file_name: str, file_path: str, category: st
|
|
| 152 |
def generate_images_from_prompts_multimodel(
|
| 153 |
file_path: str, size: str, quality: str, category: str, sentiment: str, user_prompt: str,
|
| 154 |
platform: str, num_images: int, blur: bool, selected_model: str, model_params: Optional[dict],
|
|
|
|
| 155 |
) -> List[str]:
|
| 156 |
"""Generate images using either GPT or multimodel approach"""
|
| 157 |
image_urls: List[str] = []
|
|
@@ -170,6 +172,8 @@ def generate_images_from_prompts_multimodel(
|
|
| 170 |
|
| 171 |
if not image_bytes: return None
|
| 172 |
image_with_metadata = meta_data_helper_function(image_bytes)
|
|
|
|
|
|
|
| 173 |
s3_url = upload_image_to_r2(image_with_metadata)
|
| 174 |
return s3_url
|
| 175 |
except Exception as e:
|
|
|
|
| 36 |
uid: str,
|
| 37 |
selected_model: str = "gpt_default",
|
| 38 |
model_params: Optional[dict] = None,
|
| 39 |
+
private_mode: bool = False,
|
| 40 |
) -> List[str]:
|
| 41 |
"""Enhanced image processor that supports both GPT and multimodel approaches"""
|
| 42 |
num_images = 1 if demo_mode else num_images
|
|
|
|
| 49 |
|
| 50 |
results = process_image_files_multimodel(
|
| 51 |
image_files, category, size, quality, user_prompt, sentiment, platform,
|
| 52 |
+
num_images, blur, uid, selected_model, model_params, private_mode
|
| 53 |
)
|
| 54 |
all_urls = [url for entry in results for url in entry["urls"]]
|
| 55 |
seen, deduped = set(), []
|
|
|
|
| 83 |
|
| 84 |
def process_image_files_multimodel(image_files: List[Tuple[str, str]], category: str, size: str,
|
| 85 |
quality: str, user_prompt: str, sentiment: str, platform: str, num_images: int, blur: bool,
|
| 86 |
+
uid: str, selected_model: str, model_params: Optional[dict], private_mode: bool) -> List[dict]:
|
| 87 |
"""Process image files with multimodel support"""
|
| 88 |
final_results: List[dict] = []
|
| 89 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
| 90 |
futures = []
|
| 91 |
for file_name, file_path in image_files:
|
| 92 |
job_id: Optional[str] = None
|
| 93 |
+
if COL is not None and not private_mode:
|
| 94 |
try:
|
| 95 |
settings = {
|
| 96 |
"size": size, "quality": quality, "sentiment": sentiment,
|
|
|
|
| 113 |
executor.submit(
|
| 114 |
process_single_image_multimodel,
|
| 115 |
file_name, file_path, category, size, quality, user_prompt, sentiment,
|
| 116 |
+
platform, num_images, blur, job_id, selected_model, model_params, private_mode,
|
| 117 |
)
|
| 118 |
)
|
| 119 |
for future in as_completed(futures):
|
|
|
|
| 126 |
|
| 127 |
def process_single_image_multimodel(file_name: str, file_path: str, category: str, size: str,
|
| 128 |
quality: str, user_prompt: str, sentiment: str, platform: str, num_images: int, blur: bool,
|
| 129 |
+
job_id: Optional[str], selected_model: str, model_params: Optional[dict], private_mode: bool) -> Optional[dict]:
|
| 130 |
"""Process single image with multimodel support"""
|
| 131 |
try:
|
| 132 |
image_urls = generate_images_from_prompts_multimodel(
|
| 133 |
file_path, size, quality, category, sentiment, user_prompt, platform,
|
| 134 |
+
num_images, blur, selected_model, model_params, private_mode
|
| 135 |
)
|
| 136 |
+
if COL is not None and job_id and not private_mode:
|
| 137 |
try:
|
| 138 |
finish_job(COL, job_id, status=("completed" if image_urls else "failed"), outputs_urls=image_urls)
|
| 139 |
except Exception:
|
|
|
|
| 143 |
return None
|
| 144 |
except Exception as e:
|
| 145 |
logger.error(f"Processing failed for {file_name}: {e}")
|
| 146 |
+
if COL is not None and job_id and not private_mode:
|
| 147 |
try:
|
| 148 |
finish_job(COL, job_id, status="failed", outputs_urls=[])
|
| 149 |
except Exception:
|
|
|
|
| 153 |
def generate_images_from_prompts_multimodel(
|
| 154 |
file_path: str, size: str, quality: str, category: str, sentiment: str, user_prompt: str,
|
| 155 |
platform: str, num_images: int, blur: bool, selected_model: str, model_params: Optional[dict],
|
| 156 |
+
private_mode: bool,
|
| 157 |
) -> List[str]:
|
| 158 |
"""Generate images using either GPT or multimodel approach"""
|
| 159 |
image_urls: List[str] = []
|
|
|
|
| 172 |
|
| 173 |
if not image_bytes: return None
|
| 174 |
image_with_metadata = meta_data_helper_function(image_bytes)
|
| 175 |
+
if private_mode:
|
| 176 |
+
return "data:image/png;base64," + base64.b64encode(image_with_metadata).decode("utf-8")
|
| 177 |
s3_url = upload_image_to_r2(image_with_metadata)
|
| 178 |
return s3_url
|
| 179 |
except Exception as e:
|