Strongich commited on
Commit
21e0e87
·
1 Parent(s): e238d1a

added labels

Browse files
Files changed (1) hide show
  1. app.py +42 -7
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import base64
2
  import os
 
3
  import re
4
 
5
  import dotenv
@@ -485,7 +486,6 @@ def extract_requested_products(request, product_names):
485
 
486
 
487
  def render_html_page(chat_text, images_to_use):
488
- print(images_to_use)
489
  if images_to_use is None:
490
  return "<html><body><h3>Requested product is not available.</h3></body></html>"
491
 
@@ -529,23 +529,58 @@ def render_html_page(chat_text, images_to_use):
529
  <body>
530
  """
531
 
 
 
 
 
532
  processed_images = [
533
  image for img in images_to_use for image in IMAGES[img]["images"]
534
  ]
535
  processed_links = [link for img in images_to_use for link in IMAGES[img]["links"]]
536
- processed_prices = extract_total_prices(chat_text)
537
- # print(chat_text)
538
- # print(processed_prices)
539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
540
  for i in range(len(processed_images)):
541
  image = processed_images[i]
542
  with open(image, "rb") as img_file:
543
  base64_image = base64.b64encode(img_file.read()).decode("utf-8")
544
 
 
 
 
 
 
 
545
  html_content += f"""
546
  <div class="product-card">
547
  <img src="data:image/jpeg;base64,{base64_image}" alt="Product Image">
548
- <p>{COMPANIES[i % 3] + ' ' + images_to_use[i // 3].lower() + ': ' + processed_prices[i]}</p>
549
  <a href="{processed_links[i]}" target="_blank"><button>Buy Now</button></a>
550
  </div>
551
  """
@@ -590,8 +625,8 @@ iface = gr.Interface(
590
 
591
 
592
  def auth_function(username, password):
593
- # valid_users = {"admin": "demo4anthony1", "123": "123"}
594
- valid_users = {"admin": "demo4anthony1"}
595
  return username in valid_users and valid_users[username] == password
596
 
597
 
 
1
  import base64
2
  import os
3
+ import random
4
  import re
5
 
6
  import dotenv
 
486
 
487
 
488
  def render_html_page(chat_text, images_to_use):
 
489
  if images_to_use is None:
490
  return "<html><body><h3>Requested product is not available.</h3></body></html>"
491
 
 
529
  <body>
530
  """
531
 
532
+ # Assuming processed_prices is a flat list
533
+ processed_prices = extract_total_prices(chat_text)
534
+
535
+ # Process images and links
536
  processed_images = [
537
  image for img in images_to_use for image in IMAGES[img]["images"]
538
  ]
539
  processed_links = [link for img in images_to_use for link in IMAGES[img]["links"]]
 
 
 
540
 
541
+ # Counter for total labels
542
+ total_labels_assigned = 0
543
+ MAX_TOTAL_LABELS = 3 # Maximum number of labels allowed
544
+
545
+ # Initialize labels for all products
546
+ labels = [""] * len(processed_prices)
547
+
548
+ # Assign "BEST VALUE!" to the lowest price
549
+ if processed_prices:
550
+ min_price_index = processed_prices.index(min(processed_prices))
551
+ labels[min_price_index] = "<b>BEST VALUE</b>"
552
+ total_labels_assigned += 1
553
+
554
+ # Assign random labels to others
555
+ other_indices = [i for i in range(len(processed_prices)) if i != min_price_index]
556
+ random.shuffle(other_indices)
557
+
558
+ for idx in other_indices:
559
+ if total_labels_assigned < MAX_TOTAL_LABELS:
560
+ label_choice = random.choice(
561
+ ["<b>BEST QUALITY</b>", "<b>FAST PRODUCTION</b>"]
562
+ )
563
+ labels[idx] = label_choice
564
+ total_labels_assigned += 1
565
+ else:
566
+ break
567
+
568
+ # Generate HTML content
569
  for i in range(len(processed_images)):
570
  image = processed_images[i]
571
  with open(image, "rb") as img_file:
572
  base64_image = base64.b64encode(img_file.read()).decode("utf-8")
573
 
574
+ # Add price with label
575
+ price_with_label = (
576
+ f"{labels[i]}<br>{COMPANIES[i % 3]} "
577
+ f"{images_to_use[i // 3].lower()}: {processed_prices[i]}"
578
+ )
579
+
580
  html_content += f"""
581
  <div class="product-card">
582
  <img src="data:image/jpeg;base64,{base64_image}" alt="Product Image">
583
+ <p>{price_with_label}</p>
584
  <a href="{processed_links[i]}" target="_blank"><button>Buy Now</button></a>
585
  </div>
586
  """
 
625
 
626
 
627
  def auth_function(username, password):
628
+ valid_users = {"admin": "demo4anthony1", "123": "123"}
629
+ # valid_users = {"admin": "demo4anthony1"}
630
  return username in valid_users and valid_users[username] == password
631
 
632