prernajeet14 commited on
Commit
7eef206
·
verified ·
1 Parent(s): 481672c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -50
app.py CHANGED
@@ -497,9 +497,6 @@ def get_images_html():
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")
@@ -517,7 +514,55 @@ def get_images_html():
517
  image_files.sort(key=sort_key)
518
 
519
  # Create HTML for displaying images
520
- html = "<div class='image-gallery'>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
 
522
  current_page = None
523
  for image_file in image_files:
@@ -527,15 +572,20 @@ def get_images_html():
527
  page_num = parts[0].replace('page', '')
528
  # Add page section header if we're on a new page
529
  if current_page != page_num:
 
 
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>
@@ -544,51 +594,13 @@ def get_images_html():
544
  </div>
545
  """
546
 
 
 
 
547
  html += "</div>"
548
 
549
- # Add some basic CSS for the gallery
550
- html = f"""
551
- <style>
552
- .image-gallery {{
553
- display: flex;
554
- flex-wrap: wrap;
555
- gap: 20px;
556
- justify-content: flex-start;
557
- }}
558
- .page-header {{
559
- width: 100%;
560
- margin-top: 30px;
561
- margin-bottom: 15px;
562
- border-bottom: 1px solid #ccc;
563
- }}
564
- .image-card {{
565
- border: 1px solid #ddd;
566
- border-radius: 8px;
567
- padding: 15px;
568
- width: 300px;
569
- box-shadow: 0 2px 5px rgba(0,0,0,0.1);
570
- }}
571
- .image-container {{
572
- display: flex;
573
- justify-content: center;
574
- margin-bottom: 10px;
575
- }}
576
- .image-container img {{
577
- max-width: 100%;
578
- max-height: 200px;
579
- object-fit: contain;
580
- }}
581
- .image-details h3 {{
582
- font-size: 14px;
583
- margin-top: 0;
584
- white-space: nowrap;
585
- overflow: hidden;
586
- text-overflow: ellipsis;
587
- }}
588
- </style>
589
- """ + html
590
-
591
  return html
 
592
  # Get response from OpenAI API
593
  def get_openai_response(query, context_chunks=None, include_troubleshooting=True):
594
  """Get enhanced response from OpenAI model using RAG"""
 
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")
 
514
  image_files.sort(key=sort_key)
515
 
516
  # Create HTML for displaying images
517
+ html = """
518
+ <style>
519
+ .image-gallery {
520
+ display: flex;
521
+ flex-wrap: wrap;
522
+ gap: 20px;
523
+ }
524
+ .page-section {
525
+ width: 100%;
526
+ margin-bottom: 30px;
527
+ }
528
+ .page-header {
529
+ width: 100%;
530
+ margin-bottom: 15px;
531
+ border-bottom: 1px solid #ccc;
532
+ padding-bottom: 5px;
533
+ }
534
+ .image-card {
535
+ border: 1px solid #ddd;
536
+ border-radius: 8px;
537
+ overflow: hidden;
538
+ width: 300px;
539
+ box-shadow: 0 2px 5px rgba(0,0,0,0.1);
540
+ }
541
+ .image-container {
542
+ height: 200px;
543
+ overflow: hidden;
544
+ display: flex;
545
+ align-items: center;
546
+ justify-content: center;
547
+ }
548
+ .image-container img {
549
+ max-width: 100%;
550
+ max-height: 100%;
551
+ object-fit: contain;
552
+ }
553
+ .image-details {
554
+ padding: 10px;
555
+ }
556
+ .image-details h3 {
557
+ margin-top: 0;
558
+ font-size: 14px;
559
+ overflow: hidden;
560
+ text-overflow: ellipsis;
561
+ white-space: nowrap;
562
+ }
563
+ </style>
564
+ <div class='image-gallery'>
565
+ """
566
 
567
  current_page = None
568
  for image_file in image_files:
 
572
  page_num = parts[0].replace('page', '')
573
  # Add page section header if we're on a new page
574
  if current_page != page_num:
575
+ if current_page is not None:
576
+ html += "</div>" # Close previous page section
577
  current_page = page_num
578
+ html += f"<div class='page-section'><h2 class='page-header'>Page {page_num}</h2>"
579
 
580
+ # Create the image element with multiple fallback paths
 
581
  html += f"""
582
  <div class='image-card'>
583
  <div class='image-container'>
584
+ <img src="files/{image_file}"
585
+ onerror="if (this.src === 'files/{image_file}') this.src = 'static/{image_file}';
586
+ else if (this.src === 'static/{image_file}') this.src = 'images/{image_file}';
587
+ else if (this.src === 'images/{image_file}') this.src = '{image_file}';"
588
+ alt="{image_file}">
589
  </div>
590
  <div class='image-details'>
591
  <h3>{image_file}</h3>
 
594
  </div>
595
  """
596
 
597
+ # Close the last page section and the gallery
598
+ if current_page is not None:
599
+ html += "</div>"
600
  html += "</div>"
601
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
602
  return html
603
+
604
  # Get response from OpenAI API
605
  def get_openai_response(query, context_chunks=None, include_troubleshooting=True):
606
  """Get enhanced response from OpenAI model using RAG"""