prernajeet14 commited on
Commit
481672c
·
verified ·
1 Parent(s): 165ef5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -497,14 +497,20 @@ def get_images_html():
497
  "page8_img1_85bc9423.jpeg"
498
  ]
499
 
 
 
 
500
  # Sort images by page number and then by image number
501
  def sort_key(filename):
502
  # Extract page and image numbers from filename (e.g., "page10_img1_af0cd090.jpeg")
503
  parts = filename.split('_')
504
  if len(parts) >= 2:
505
- page_num = int(parts[0].replace('page', ''))
506
- img_num = int(parts[1].replace('img', ''))
507
- return (page_num, img_num)
 
 
 
508
  return (999, 999) # Default for any files that don't match the pattern
509
 
510
  # Sort the image files
@@ -513,9 +519,6 @@ def get_images_html():
513
  # Create HTML for displaying images
514
  html = "<div class='image-gallery'>"
515
 
516
- # The problem was that we were checking if image_files is empty, but it's not empty
517
- # We have the list of files, we just need to display them
518
-
519
  current_page = None
520
  for image_file in image_files:
521
  # Extract page number for section headers
@@ -527,14 +530,12 @@ def get_images_html():
527
  current_page = page_num
528
  html += f"<h2 class='page-header'>Page {page_num}</h2>"
529
 
530
- # In Hugging Face Spaces, files need the correct path
531
- # This is the likely source of your error - incorrect path to the images
532
- img_path = f"files/{image_file}" # Adjust this path based on your Hugging Face Space structure
533
-
534
  html += f"""
535
  <div class='image-card'>
536
  <div class='image-container'>
537
- <img src="{img_path}" alt="{image_file}">
538
  </div>
539
  <div class='image-details'>
540
  <h3>{image_file}</h3>
 
497
  "page8_img1_85bc9423.jpeg"
498
  ]
499
 
500
+ if not image_files:
501
+ return "<div class='image-gallery'><p>No images found in the database.</p></div>"
502
+
503
  # Sort images by page number and then by image number
504
  def sort_key(filename):
505
  # Extract page and image numbers from filename (e.g., "page10_img1_af0cd090.jpeg")
506
  parts = filename.split('_')
507
  if len(parts) >= 2:
508
+ try:
509
+ page_num = int(parts[0].replace('page', ''))
510
+ img_num = int(parts[1].replace('img', ''))
511
+ return (page_num, img_num)
512
+ except ValueError:
513
+ return (999, 999)
514
  return (999, 999) # Default for any files that don't match the pattern
515
 
516
  # Sort the image files
 
519
  # Create HTML for displaying images
520
  html = "<div class='image-gallery'>"
521
 
 
 
 
522
  current_page = None
523
  for image_file in image_files:
524
  # Extract page number for section headers
 
530
  current_page = page_num
531
  html += f"<h2 class='page-header'>Page {page_num}</h2>"
532
 
533
+ # Try multiple possible paths for Hugging Face Spaces
534
+ # Option 1: Direct path (if images are in the root of the Space)
 
 
535
  html += f"""
536
  <div class='image-card'>
537
  <div class='image-container'>
538
+ <img src="{image_file}" alt="{image_file}" onerror="this.onerror=null; this.src='static/{image_file}'; this.onerror=null; this.src='files/{image_file}'; this.onerror=null; this.src='images/{image_file}';">
539
  </div>
540
  <div class='image-details'>
541
  <h3>{image_file}</h3>