Update app_main.py
Browse files- app_main.py +21 -6
app_main.py
CHANGED
|
@@ -17,7 +17,7 @@ import tempfile
|
|
| 17 |
import torch
|
| 18 |
from langchain_groq import ChatGroq
|
| 19 |
from langgraph.prebuilt import create_react_agent
|
| 20 |
-
import logging
|
| 21 |
|
| 22 |
# Configure logging
|
| 23 |
logging.basicConfig(
|
|
@@ -126,9 +126,12 @@ def extract_images_from_pdf(pdf_path, output_json_path):
|
|
| 126 |
f"❌ Failed to extract images from PDF: {str(e)}")
|
| 127 |
|
| 128 |
try:
|
|
|
|
| 129 |
with open(output_json_path, "w") as f:
|
| 130 |
json.dump([element.to_dict()
|
| 131 |
for element in elements], f, indent=4)
|
|
|
|
|
|
|
| 132 |
except Exception as e:
|
| 133 |
raise RuntimeError(f"❌ Failed to write extracted.json: {str(e)}")
|
| 134 |
|
|
@@ -172,6 +175,7 @@ def extract_images_from_pdf(pdf_path, output_json_path):
|
|
| 172 |
start_count = 1
|
| 173 |
|
| 174 |
sprite_count = start_count
|
|
|
|
| 175 |
for i, element in enumerate(file_elements):
|
| 176 |
if "image_base64" in element["metadata"]:
|
| 177 |
try:
|
|
@@ -246,7 +250,9 @@ def extract_images_from_pdf(pdf_path, output_json_path):
|
|
| 246 |
sprite_count += 1
|
| 247 |
except Exception as e:
|
| 248 |
print(f"⚠️ Error processing Sprite {i+1}: {str(e)}")
|
| 249 |
-
|
|
|
|
|
|
|
| 250 |
# Save manipulated JSON
|
| 251 |
with open(final_json_path, "w") as sprite_file:
|
| 252 |
json.dump(manipulated_json, sprite_file, indent=4)
|
|
@@ -294,12 +300,15 @@ def similarity_matching(input_json_path: str) -> str:
|
|
| 294 |
sprites_data = json.load(f)
|
| 295 |
|
| 296 |
sprite_ids, texts, sprite_base64 = [], [], []
|
|
|
|
| 297 |
for sid, sprite in sprites_data.items():
|
| 298 |
sprite_ids.append(sid)
|
| 299 |
texts.append(
|
| 300 |
"This is " + sprite.get("description", sprite.get("name", "")))
|
| 301 |
sprite_base64.append(sprite["base64"])
|
| 302 |
-
|
|
|
|
|
|
|
| 303 |
# ============================== #
|
| 304 |
# INITIALIZE CLIP EMBEDDER #
|
| 305 |
# ============================== #
|
|
@@ -340,13 +349,16 @@ def similarity_matching(input_json_path: str) -> str:
|
|
| 340 |
# ============================== #
|
| 341 |
temp_dir = tempfile.mkdtemp()
|
| 342 |
sprite_image_paths = []
|
|
|
|
| 343 |
for idx, b64 in enumerate(sprite_base64):
|
| 344 |
image_data = base64.b64decode(b64.split(",")[-1])
|
| 345 |
img = Image.open(BytesIO(image_data)).convert("RGB")
|
| 346 |
temp_path = os.path.join(temp_dir, f"sprite_{idx}.png")
|
| 347 |
img.save(temp_path)
|
| 348 |
sprite_image_paths.append(temp_path)
|
| 349 |
-
|
|
|
|
|
|
|
| 350 |
# ============================== #
|
| 351 |
# EMBED SPRITE IMAGES #
|
| 352 |
# ============================== #
|
|
@@ -367,6 +379,7 @@ def similarity_matching(input_json_path: str) -> str:
|
|
| 367 |
# ============= Match and copy ================
|
| 368 |
project_data, backdrop_data = [], []
|
| 369 |
copied_folders = set()
|
|
|
|
| 370 |
for sprite_idx, matched_idx in enumerate(most_similar_indices):
|
| 371 |
matched_entry = embedding_json[matched_idx]
|
| 372 |
# matched_image_path = os.path.normpath(folder_image_paths[matched_idx])
|
|
@@ -413,11 +426,13 @@ def similarity_matching(input_json_path: str) -> str:
|
|
| 413 |
"agent": "OpenAI ScratchVision Agent"
|
| 414 |
}
|
| 415 |
}
|
| 416 |
-
|
| 417 |
for sprite in project_data:
|
| 418 |
if not sprite.get("isStage", False):
|
| 419 |
final_project["targets"].append(sprite)
|
| 420 |
-
|
|
|
|
|
|
|
| 421 |
if backdrop_data:
|
| 422 |
all_costumes, sounds = [], []
|
| 423 |
for idx, bd in enumerate(backdrop_data):
|
|
|
|
| 17 |
import torch
|
| 18 |
from langchain_groq import ChatGroq
|
| 19 |
from langgraph.prebuilt import create_react_agent
|
| 20 |
+
import logging, time
|
| 21 |
|
| 22 |
# Configure logging
|
| 23 |
logging.basicConfig(
|
|
|
|
| 126 |
f"❌ Failed to extract images from PDF: {str(e)}")
|
| 127 |
|
| 128 |
try:
|
| 129 |
+
start_time = time.perf_counter()
|
| 130 |
with open(output_json_path, "w") as f:
|
| 131 |
json.dump([element.to_dict()
|
| 132 |
for element in elements], f, indent=4)
|
| 133 |
+
elapsed = time.perf_counter() - start_time
|
| 134 |
+
logger.info(f"✅ extracted.json write in {elapsed:.2f} seconds")
|
| 135 |
except Exception as e:
|
| 136 |
raise RuntimeError(f"❌ Failed to write extracted.json: {str(e)}")
|
| 137 |
|
|
|
|
| 175 |
start_count = 1
|
| 176 |
|
| 177 |
sprite_count = start_count
|
| 178 |
+
start_time = time.perf_counter()
|
| 179 |
for i, element in enumerate(file_elements):
|
| 180 |
if "image_base64" in element["metadata"]:
|
| 181 |
try:
|
|
|
|
| 250 |
sprite_count += 1
|
| 251 |
except Exception as e:
|
| 252 |
print(f"⚠️ Error processing Sprite {i+1}: {str(e)}")
|
| 253 |
+
elapsed = time.perf_counter() - start_time
|
| 254 |
+
logger.info(f"✅ extracted_sprites.json write in {elapsed:.2f} seconds")
|
| 255 |
+
|
| 256 |
# Save manipulated JSON
|
| 257 |
with open(final_json_path, "w") as sprite_file:
|
| 258 |
json.dump(manipulated_json, sprite_file, indent=4)
|
|
|
|
| 300 |
sprites_data = json.load(f)
|
| 301 |
|
| 302 |
sprite_ids, texts, sprite_base64 = [], [], []
|
| 303 |
+
start_time = time.perf_counter()
|
| 304 |
for sid, sprite in sprites_data.items():
|
| 305 |
sprite_ids.append(sid)
|
| 306 |
texts.append(
|
| 307 |
"This is " + sprite.get("description", sprite.get("name", "")))
|
| 308 |
sprite_base64.append(sprite["base64"])
|
| 309 |
+
elapsed = time.perf_counter() - start_time
|
| 310 |
+
logger.info(f"✅ Append Sprite's Name and Description in {elapsed:.2f} seconds")
|
| 311 |
+
|
| 312 |
# ============================== #
|
| 313 |
# INITIALIZE CLIP EMBEDDER #
|
| 314 |
# ============================== #
|
|
|
|
| 349 |
# ============================== #
|
| 350 |
temp_dir = tempfile.mkdtemp()
|
| 351 |
sprite_image_paths = []
|
| 352 |
+
start_time = time.perf_counter()
|
| 353 |
for idx, b64 in enumerate(sprite_base64):
|
| 354 |
image_data = base64.b64decode(b64.split(",")[-1])
|
| 355 |
img = Image.open(BytesIO(image_data)).convert("RGB")
|
| 356 |
temp_path = os.path.join(temp_dir, f"sprite_{idx}.png")
|
| 357 |
img.save(temp_path)
|
| 358 |
sprite_image_paths.append(temp_path)
|
| 359 |
+
elapsed = time.perf_counter() - start_time
|
| 360 |
+
logger.info(f"✅ Decoded Sprite Base64 in {elapsed:.2f} seconds")
|
| 361 |
+
|
| 362 |
# ============================== #
|
| 363 |
# EMBED SPRITE IMAGES #
|
| 364 |
# ============================== #
|
|
|
|
| 379 |
# ============= Match and copy ================
|
| 380 |
project_data, backdrop_data = [], []
|
| 381 |
copied_folders = set()
|
| 382 |
+
start_time = time.perf_counter()
|
| 383 |
for sprite_idx, matched_idx in enumerate(most_similar_indices):
|
| 384 |
matched_entry = embedding_json[matched_idx]
|
| 385 |
# matched_image_path = os.path.normpath(folder_image_paths[matched_idx])
|
|
|
|
| 426 |
"agent": "OpenAI ScratchVision Agent"
|
| 427 |
}
|
| 428 |
}
|
| 429 |
+
start_time = time.perf_counter()
|
| 430 |
for sprite in project_data:
|
| 431 |
if not sprite.get("isStage", False):
|
| 432 |
final_project["targets"].append(sprite)
|
| 433 |
+
elapsed = time.perf_counter() - start_time
|
| 434 |
+
logger.info(f"✅ Append sprite 'targets' in {elapsed:.2f} seconds")
|
| 435 |
+
|
| 436 |
if backdrop_data:
|
| 437 |
all_costumes, sounds = [], []
|
| 438 |
for idx, bd in enumerate(backdrop_data):
|