SamiKoen commited on
Commit
24d840d
·
verified ·
1 Parent(s): b2bcfd7

Stok API + XML hibrit sistem: Stok APIden, fiyat/görsel/link XMLden

Browse files
Files changed (1) hide show
  1. app.py +60 -49
app.py CHANGED
@@ -486,63 +486,74 @@ def chatbot_fn(user_message, history, image=None):
486
  })
487
  print(f"Stok bilgisi eklendi: {stock_info}")
488
 
489
- # ESKİ STOK SİSTEMİ - XML'den gelen bilgiler (yedek olarak kalacak)
490
- # Stok sorgusu değilse veya API'den veri alınamadıysa XML'den bak
491
- skip_xml = is_stock_query(user_message) and 'stock_info' in locals() and stock_info is not None
492
- if not skip_xml:
493
- input_words = user_message.lower().split()
494
- added_products_count = 0
495
- for word in input_words:
496
- if added_products_count >= 5: # Token limiti için maksimum 5 ürün
497
- break
498
- for product_info in products:
499
- if word in product_info[0] or word in product_info[2].lower():
500
- if product_info[1][0] == "stokta":
501
- normal_price = f"\\nFiyat: {product_info[1][1]} TL"
502
-
503
- rebate_price = ""
504
- discount_info = ""
505
- eft_price = ""
506
- rebate_money_order_price = ""
 
 
 
 
507
 
508
- if len(product_info[1]) > 4 and product_info[1][4] and product_info[1][4] != "":
509
- rebate_price = f"\\nKampanyalı fiyat: {product_info[1][4]} TL"
 
 
510
 
511
- try:
512
- normal_price_float = float(product_info[1][1])
513
- rebate_price_float = float(product_info[1][4])
514
- discount_amount = normal_price_float - rebate_price_float
515
-
516
- if discount_amount > 0:
517
- if discount_amount > 200000:
518
- discount_amount_rounded = round(discount_amount / 5000) * 5000
519
- elif discount_amount > 30000:
520
- discount_amount_rounded = round(discount_amount / 1000) * 1000
521
- elif discount_amount > 10000:
522
- discount_amount_rounded = round(discount_amount / 100) * 100
523
- else:
524
- discount_amount_rounded = round(discount_amount / 10) * 10
525
-
526
- discount_info = f"\\nYapılan indirim: {discount_amount_rounded:.0f} TL"
527
- except (ValueError, TypeError):
528
- discount_info = ""
529
-
530
- rebate_money_order_price = ""
531
- else:
532
- if product_info[1][3] and product_info[1][3] != "":
533
- eft_price = f"\\nHavale indirimli fiyat: {product_info[1][3]} TL"
534
-
535
- product_link = f"\\nÜrün linki: {product_info[1][2]}"
536
- product_image = ""
537
- if len(product_info[1]) > 6 and product_info[1][6]:
538
- product_image = f"\\nÜrün resmi: {product_info[1][6]}"
539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
540
  new_msg = f"{product_info[2]} {product_info[1][0]}\\n{normal_price}{rebate_price}{discount_info}{eft_price}{rebate_money_order_price}{product_link}{product_image}"
 
 
 
 
 
541
  else:
542
  new_msg = f"{product_info[2]} {product_info[1][0]}"
543
 
544
  # Token limiti için mesaj boyutunu kontrol et
545
- if len(new_msg) < 5000: # Maksimum 5000 karakter
546
  system_messages.append({"role": "system", "content": new_msg})
547
  added_products_count += 1
548
  break
 
486
  })
487
  print(f"Stok bilgisi eklendi: {stock_info}")
488
 
489
+ # XML'DEN FİYAT/GÖRSEL/LİNK BİLGİLERİ - Her zaman çalışır
490
+ # Stok bilgisi API'den alındıysa, sadece fiyat/görsel/link için XML'e bak
491
+ has_stock_from_api = is_stock_query(user_message) and 'stock_info' in locals() and stock_info is not None
492
+
493
+ # XML'den ürün bilgilerini al (fiyat, görsel, link için)
494
+ input_words = user_message.lower().split()
495
+ added_products_count = 0
496
+ for word in input_words:
497
+ if added_products_count >= 5: # Token limiti için maksimum 5 ürün
498
+ break
499
+ for product_info in products:
500
+ if word in product_info[0] or word in product_info[2].lower():
501
+ if product_info[1][0] == "stokta":
502
+ normal_price = f"\\nFiyat: {product_info[1][1]} TL"
503
+
504
+ rebate_price = ""
505
+ discount_info = ""
506
+ eft_price = ""
507
+ rebate_money_order_price = ""
508
+
509
+ if len(product_info[1]) > 4 and product_info[1][4] and product_info[1][4] != "":
510
+ rebate_price = f"\\nKampanyalı fiyat: {product_info[1][4]} TL"
511
 
512
+ try:
513
+ normal_price_float = float(product_info[1][1])
514
+ rebate_price_float = float(product_info[1][4])
515
+ discount_amount = normal_price_float - rebate_price_float
516
 
517
+ if discount_amount > 0:
518
+ if discount_amount > 200000:
519
+ discount_amount_rounded = round(discount_amount / 5000) * 5000
520
+ elif discount_amount > 30000:
521
+ discount_amount_rounded = round(discount_amount / 1000) * 1000
522
+ elif discount_amount > 10000:
523
+ discount_amount_rounded = round(discount_amount / 100) * 100
524
+ else:
525
+ discount_amount_rounded = round(discount_amount / 10) * 10
526
+
527
+ discount_info = f"\\nYapılan indirim: {discount_amount_rounded:.0f} TL"
528
+ except (ValueError, TypeError):
529
+ discount_info = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
530
 
531
+ rebate_money_order_price = ""
532
+ else:
533
+ if product_info[1][3] and product_info[1][3] != "":
534
+ eft_price = f"\\nHavale indirimli fiyat: {product_info[1][3]} TL"
535
+
536
+ product_link = f"\\nÜrün linki: {product_info[1][2]}"
537
+ product_image = ""
538
+ if len(product_info[1]) > 6 and product_info[1][6]:
539
+ product_image = f"\\nÜrün resmi: {product_info[1][6]}"
540
+
541
+ # API'den stok alındıysa sadece fiyat/görsel/link ekle
542
+ if has_stock_from_api:
543
+ new_msg = f"ÜRÜN BİLGİLERİ (Fiyat/Link):\\n{normal_price}{rebate_price}{discount_info}{eft_price}{rebate_money_order_price}{product_link}{product_image}"
544
+ else:
545
+ # API'den stok alınmadıysa hem stok hem fiyat bilgisi ekle
546
  new_msg = f"{product_info[2]} {product_info[1][0]}\\n{normal_price}{rebate_price}{discount_info}{eft_price}{rebate_money_order_price}{product_link}{product_image}"
547
+ else:
548
+ # Stokta değilse
549
+ if has_stock_from_api:
550
+ # API'den stok bilgisi varsa XML'den stok bilgisi ekleme
551
+ new_msg = None
552
  else:
553
  new_msg = f"{product_info[2]} {product_info[1][0]}"
554
 
555
  # Token limiti için mesaj boyutunu kontrol et
556
+ if new_msg and len(new_msg) < 5000: # Maksimum 5000 karakter
557
  system_messages.append({"role": "system", "content": new_msg})
558
  added_products_count += 1
559
  break