Spaces:
Sleeping
Sleeping
Improve image file handling and add debug logging for image_name matching
Browse files
app.py
CHANGED
|
@@ -1457,37 +1457,58 @@ with gr.Blocks(
|
|
| 1457 |
image_dict = {} # Maps filename -> base64
|
| 1458 |
original_filenames = [] # Track original filenames for error messages
|
| 1459 |
|
|
|
|
| 1460 |
if uploaded_images:
|
| 1461 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1462 |
try:
|
| 1463 |
if img_file is None:
|
|
|
|
| 1464 |
continue
|
| 1465 |
|
| 1466 |
# Extract filename and process image
|
| 1467 |
filename = None
|
| 1468 |
img_b64 = None
|
|
|
|
| 1469 |
|
| 1470 |
-
# Handle different file input formats
|
| 1471 |
if isinstance(img_file, str):
|
| 1472 |
-
# File path
|
| 1473 |
-
|
| 1474 |
-
|
| 1475 |
-
|
| 1476 |
-
|
| 1477 |
-
|
| 1478 |
-
|
| 1479 |
-
|
| 1480 |
elif isinstance(img_file, dict):
|
| 1481 |
-
# Gradio file dict format: {"name": "...", "path": "...", ...}
|
| 1482 |
file_path = img_file.get("path") or img_file.get("name")
|
|
|
|
|
|
|
| 1483 |
if file_path:
|
| 1484 |
-
|
|
|
|
|
|
|
|
|
|
| 1485 |
img_b64 = gradio_image_to_base64(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1486 |
else:
|
| 1487 |
-
# Try to process as image directly
|
| 1488 |
img_b64 = gradio_image_to_base64(img_file)
|
| 1489 |
-
|
| 1490 |
-
|
|
|
|
| 1491 |
|
| 1492 |
if img_b64:
|
| 1493 |
image_list.append(img_b64)
|
|
@@ -1503,9 +1524,17 @@ with gr.Blocks(
|
|
| 1503 |
if base_name and base_name != filename:
|
| 1504 |
image_dict[base_name] = img_b64
|
| 1505 |
image_dict[base_name.lower()] = img_b64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1506 |
except Exception as e:
|
| 1507 |
-
logger.
|
| 1508 |
continue
|
|
|
|
|
|
|
| 1509 |
|
| 1510 |
# Validate and import items
|
| 1511 |
imported_count = 0
|
|
@@ -1546,20 +1575,32 @@ with gr.Blocks(
|
|
| 1546 |
|
| 1547 |
# Try to find matching image (case-insensitive)
|
| 1548 |
image_name_clean = image_name.strip()
|
| 1549 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1550 |
|
| 1551 |
if not img_b64:
|
| 1552 |
# Try matching just the filename without path
|
| 1553 |
basename = os.path.basename(image_name_clean)
|
| 1554 |
img_b64 = image_dict.get(basename) or image_dict.get(basename.lower())
|
|
|
|
|
|
|
| 1555 |
|
| 1556 |
if not img_b64:
|
| 1557 |
# Try matching without extension
|
| 1558 |
base_name = os.path.splitext(image_name_clean)[0]
|
| 1559 |
if base_name:
|
| 1560 |
img_b64 = image_dict.get(base_name) or image_dict.get(base_name.lower())
|
|
|
|
|
|
|
| 1561 |
|
| 1562 |
-
if
|
|
|
|
|
|
|
| 1563 |
# Show available filenames for debugging
|
| 1564 |
available_str = ', '.join(original_filenames[:5])
|
| 1565 |
if len(original_filenames) > 5:
|
|
@@ -1567,7 +1608,7 @@ with gr.Blocks(
|
|
| 1567 |
if not original_filenames:
|
| 1568 |
available_str = "none uploaded"
|
| 1569 |
# Log warning but continue - don't fail the entire import
|
| 1570 |
-
logger.warning(f"Item {i+1}: Image '{
|
| 1571 |
elif "image_index" in item:
|
| 1572 |
# Reference uploaded image by index
|
| 1573 |
img_idx = item["image_index"]
|
|
|
|
| 1457 |
image_dict = {} # Maps filename -> base64
|
| 1458 |
original_filenames = [] # Track original filenames for error messages
|
| 1459 |
|
| 1460 |
+
# Handle case where uploaded_images might be None, empty list, or single file
|
| 1461 |
if uploaded_images:
|
| 1462 |
+
# Ensure it's a list
|
| 1463 |
+
if not isinstance(uploaded_images, list):
|
| 1464 |
+
uploaded_images = [uploaded_images]
|
| 1465 |
+
logger.info(f"Processing {len(uploaded_images)} uploaded image(s)")
|
| 1466 |
+
for idx, img_file in enumerate(uploaded_images):
|
| 1467 |
try:
|
| 1468 |
if img_file is None:
|
| 1469 |
+
logger.warning(f"Image {idx} is None, skipping")
|
| 1470 |
continue
|
| 1471 |
|
| 1472 |
# Extract filename and process image
|
| 1473 |
filename = None
|
| 1474 |
img_b64 = None
|
| 1475 |
+
file_path = None
|
| 1476 |
|
| 1477 |
+
# Handle different file input formats (Gradio 6.1.0 returns file paths as strings)
|
| 1478 |
if isinstance(img_file, str):
|
| 1479 |
+
# File path (most common in Gradio 6.x)
|
| 1480 |
+
file_path = img_file
|
| 1481 |
+
if os.path.exists(file_path):
|
| 1482 |
+
filename = os.path.basename(file_path)
|
| 1483 |
+
img_b64 = gradio_image_to_base64(file_path)
|
| 1484 |
+
logger.info(f"Processed image from path: {filename}")
|
| 1485 |
+
else:
|
| 1486 |
+
logger.warning(f"File path does not exist: {file_path}")
|
| 1487 |
elif isinstance(img_file, dict):
|
| 1488 |
+
# Gradio file dict format: {"name": "...", "path": "...", "orig_name": "...", ...}
|
| 1489 |
file_path = img_file.get("path") or img_file.get("name")
|
| 1490 |
+
# Try to get original filename first, then fall back to path basename
|
| 1491 |
+
orig_name = img_file.get("orig_name") or img_file.get("name")
|
| 1492 |
if file_path:
|
| 1493 |
+
if orig_name:
|
| 1494 |
+
filename = os.path.basename(orig_name)
|
| 1495 |
+
else:
|
| 1496 |
+
filename = os.path.basename(file_path)
|
| 1497 |
img_b64 = gradio_image_to_base64(file_path)
|
| 1498 |
+
logger.info(f"Processed image from dict: {filename} (path: {file_path})")
|
| 1499 |
+
elif hasattr(img_file, 'name'):
|
| 1500 |
+
# File object with name attribute
|
| 1501 |
+
file_path = img_file.name if hasattr(img_file, 'name') else str(img_file)
|
| 1502 |
+
filename = os.path.basename(file_path) if file_path else None
|
| 1503 |
+
if file_path and os.path.exists(file_path):
|
| 1504 |
+
img_b64 = gradio_image_to_base64(file_path)
|
| 1505 |
+
logger.info(f"Processed image from file object: {filename}")
|
| 1506 |
else:
|
| 1507 |
+
# Try to process as image directly (numpy array, PIL Image, etc.)
|
| 1508 |
img_b64 = gradio_image_to_base64(img_file)
|
| 1509 |
+
if img_b64:
|
| 1510 |
+
filename = f"image_{len(image_list)}.png"
|
| 1511 |
+
logger.info(f"Processed image as direct input: {filename}")
|
| 1512 |
|
| 1513 |
if img_b64:
|
| 1514 |
image_list.append(img_b64)
|
|
|
|
| 1524 |
if base_name and base_name != filename:
|
| 1525 |
image_dict[base_name] = img_b64
|
| 1526 |
image_dict[base_name.lower()] = img_b64
|
| 1527 |
+
logger.info(f"Stored image: {filename} (keys: {filename}, {filename.lower()})")
|
| 1528 |
+
else:
|
| 1529 |
+
logger.warning(f"Image processed but no filename extracted, using index")
|
| 1530 |
+
image_dict[f"image_{len(image_list)-1}"] = img_b64
|
| 1531 |
+
else:
|
| 1532 |
+
logger.warning(f"Failed to convert image {idx} to base64 (type: {type(img_file)})")
|
| 1533 |
except Exception as e:
|
| 1534 |
+
logger.error(f"Failed to process uploaded image {idx}: {str(e)}\n{traceback.format_exc()}")
|
| 1535 |
continue
|
| 1536 |
+
|
| 1537 |
+
logger.info(f"Successfully processed {len(image_list)} images. Available filenames: {original_filenames}")
|
| 1538 |
|
| 1539 |
# Validate and import items
|
| 1540 |
imported_count = 0
|
|
|
|
| 1575 |
|
| 1576 |
# Try to find matching image (case-insensitive)
|
| 1577 |
image_name_clean = image_name.strip()
|
| 1578 |
+
logger.info(f"Item {i+1}: Looking for image '{image_name_clean}' in {len(image_dict)} stored images")
|
| 1579 |
+
|
| 1580 |
+
# Try exact match first
|
| 1581 |
+
img_b64 = image_dict.get(image_name_clean)
|
| 1582 |
+
if not img_b64:
|
| 1583 |
+
# Try case-insensitive match
|
| 1584 |
+
img_b64 = image_dict.get(image_name_clean.lower())
|
| 1585 |
|
| 1586 |
if not img_b64:
|
| 1587 |
# Try matching just the filename without path
|
| 1588 |
basename = os.path.basename(image_name_clean)
|
| 1589 |
img_b64 = image_dict.get(basename) or image_dict.get(basename.lower())
|
| 1590 |
+
if img_b64:
|
| 1591 |
+
logger.info(f"Item {i+1}: Matched image by basename '{basename}'")
|
| 1592 |
|
| 1593 |
if not img_b64:
|
| 1594 |
# Try matching without extension
|
| 1595 |
base_name = os.path.splitext(image_name_clean)[0]
|
| 1596 |
if base_name:
|
| 1597 |
img_b64 = image_dict.get(base_name) or image_dict.get(base_name.lower())
|
| 1598 |
+
if img_b64:
|
| 1599 |
+
logger.info(f"Item {i+1}: Matched image by base name '{base_name}'")
|
| 1600 |
|
| 1601 |
+
if img_b64:
|
| 1602 |
+
logger.info(f"Item {i+1}: Successfully matched image '{image_name_clean}'")
|
| 1603 |
+
else:
|
| 1604 |
# Show available filenames for debugging
|
| 1605 |
available_str = ', '.join(original_filenames[:5])
|
| 1606 |
if len(original_filenames) > 5:
|
|
|
|
| 1608 |
if not original_filenames:
|
| 1609 |
available_str = "none uploaded"
|
| 1610 |
# Log warning but continue - don't fail the entire import
|
| 1611 |
+
logger.warning(f"Item {i+1}: Image '{image_name_clean}' not found. Available images: {available_str}. Image dict keys: {list(image_dict.keys())[:10]}")
|
| 1612 |
elif "image_index" in item:
|
| 1613 |
# Reference uploaded image by index
|
| 1614 |
img_idx = item["image_index"]
|