SamiKoen commited on
Commit
dbcd8a8
·
verified ·
1 Parent(s): 56aafb6

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +60 -31
  2. enhanced_features.py +52 -43
app.py CHANGED
@@ -318,27 +318,35 @@ def chatbot_fn(user_message, history, image=None):
318
  if history is None:
319
  history = []
320
 
321
- # Enhanced features - Görsel işleme
322
- if image is not None:
323
- user_message = process_image_message(image, user_message)
324
-
325
- # Enhanced features - Karşılaştırma kontrolü
326
- comparison_result = handle_comparison_request(user_message)
327
- if comparison_result:
328
- yield comparison_result
329
- return
330
-
331
- # Enhanced features - Kişisel öneriler
332
- user_id = "default_user" # Gerçek uygulamada session ID kullanılır
333
- recommendation_result = get_user_recommendations(user_id, user_message)
334
- if recommendation_result:
335
- # Kullanıcı etkileşimini kaydet
336
- profile_manager.add_interaction(user_id, "recommendation_request", {
337
- "message": user_message,
338
- "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
339
- })
340
- yield recommendation_result
341
- return
 
 
 
 
 
 
 
 
342
 
343
  # Log: Kullanıcı mesajını ekle
344
  try:
@@ -651,17 +659,38 @@ with gr.Blocks(css=custom_css, theme="soft", title="Trek Asistanı") as demo:
651
  submit_btn = gr.Button("Gönder", scale=1)
652
 
653
  def respond(message, chat_history):
654
- # Enhanced chatbot fonksiyonunu çağır (image=None)
655
- response_generator = chatbot_fn(message, chat_history, None)
656
-
657
- # Generator'dan son cevabı al
658
- response = ""
659
- for partial in response_generator:
660
- response = partial
 
 
661
 
662
- # Chat history güncelle
663
- chat_history.append((message, response))
664
- return "", chat_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
665
 
666
  submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
667
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
318
  if history is None:
319
  history = []
320
 
321
+ try:
322
+ # Enhanced features - Görsel işleme
323
+ if image is not None:
324
+ user_message = process_image_message(image, user_message)
325
+
326
+ # Enhanced features - Karşılaştırma kontrolü
327
+ comparison_result = handle_comparison_request(user_message)
328
+ if comparison_result:
329
+ yield comparison_result
330
+ return
331
+
332
+ # Enhanced features - Kişisel öneriler
333
+ user_id = "default_user" # Gerçek uygulamada session ID kullanılır
334
+ recommendation_result = get_user_recommendations(user_id, user_message)
335
+ if recommendation_result:
336
+ # Kullanıcı etkileşimini kaydet
337
+ try:
338
+ profile_manager.add_interaction(user_id, "recommendation_request", {
339
+ "message": user_message,
340
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
341
+ })
342
+ except Exception as e:
343
+ print(f"Profile save error: {e}")
344
+ yield recommendation_result
345
+ return
346
+
347
+ except Exception as e:
348
+ print(f"Enhanced features error: {e}")
349
+ # Enhanced features hata verirse normal chatbot'a devam et
350
 
351
  # Log: Kullanıcı mesajını ekle
352
  try:
 
659
  submit_btn = gr.Button("Gönder", scale=1)
660
 
661
  def respond(message, chat_history):
662
+ if not message.strip():
663
+ return "", chat_history
664
+
665
+ # Chat history'yi chatbot_fn için uygun formata çevir
666
+ formatted_history = []
667
+ if chat_history:
668
+ for user_msg, bot_msg in chat_history:
669
+ formatted_history.append({"role": "user", "content": user_msg})
670
+ formatted_history.append({"role": "assistant", "content": bot_msg})
671
 
672
+ try:
673
+ # Enhanced chatbot fonksiyonunu çağır (image=None)
674
+ response_generator = chatbot_fn(message, formatted_history, None)
675
+
676
+ # Generator'dan son cevabı al
677
+ response = ""
678
+ for partial in response_generator:
679
+ response = partial
680
+
681
+ # Chat history güncelle
682
+ if chat_history is None:
683
+ chat_history = []
684
+ chat_history.append((message, response))
685
+ return "", chat_history
686
+
687
+ except Exception as e:
688
+ error_msg = f"Üzgünüm, bir hata oluştu: {str(e)}"
689
+ print(f"Chat error: {e}")
690
+ if chat_history is None:
691
+ chat_history = []
692
+ chat_history.append((message, error_msg))
693
+ return "", chat_history
694
 
695
  submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
696
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
enhanced_features.py CHANGED
@@ -334,50 +334,59 @@ def process_image_message(image_path, user_message):
334
 
335
  def handle_comparison_request(user_message):
336
  """Karşılaştırma talebini işle"""
337
- if "karşılaştır" in user_message.lower() or "compare" in user_message.lower():
338
- # Ürün isimlerini çıkarmaya çalış
339
- words = user_message.lower().split()
340
- potential_products = []
341
-
342
- # Bilinen model isimlerini ara
343
- known_models = ["émonda", "madone", "domane", "marlin", "fuel", "powerfly", "fx"]
344
- for word in words:
345
- for model in known_models:
346
- if model in word:
347
- potential_products.append(model)
 
 
 
 
 
348
 
349
- if len(potential_products) >= 2:
350
- comparison_table = product_comparison.create_comparison_table(potential_products)
351
- return f"Ürün Karşılaştırması:\n\n{comparison_table}"
352
-
353
- return None
354
 
355
  def get_user_recommendations(user_id, user_message):
356
  """Kullanıcıya özel öneriler al"""
357
- # Bütçe sorgusu varsa
358
- if "bütçe" in user_message.lower() or "budget" in user_message.lower():
359
- # Rakamları çıkarmaya çalış
360
- import re
361
- numbers = re.findall(r'\d+', user_message)
362
- if len(numbers) >= 2:
363
- budget_min = int(numbers[0]) * 1000 # K TL formatı için
364
- budget_max = int(numbers[1]) * 1000
365
- recommendations = personalized_recommendations.get_budget_recommendations(
366
- user_id, budget_min, budget_max
367
- )
368
-
369
- if recommendations:
370
- rec_text = "Bütçenize uygun öneriler:\n\n"
371
- for product in recommendations[:3]:
372
- rec_text += f"• {product[2]} - {product[1][1]} TL\n"
373
- return rec_text
374
-
375
- # Genel kişisel öneriler
376
- suggestions = personalized_recommendations.get_personalized_suggestions(user_id)
377
- if suggestions:
378
- sug_text = "Size özel önerilerimiz:\n\n"
379
- for product in suggestions:
380
- sug_text += f" {product[2]} - {product[1][1]} TL\n"
381
- return sug_text
382
-
383
- return None
 
 
 
 
 
 
334
 
335
  def handle_comparison_request(user_message):
336
  """Karşılaştırma talebini işle"""
337
+ try:
338
+ if "karşılaştır" in user_message.lower() or "compare" in user_message.lower():
339
+ # Ürün isimlerini çıkarmaya çalış
340
+ words = user_message.lower().split()
341
+ potential_products = []
342
+
343
+ # Bilinen model isimlerini ara
344
+ known_models = ["émonda", "madone", "domane", "marlin", "fuel", "powerfly", "fx"]
345
+ for word in words:
346
+ for model in known_models:
347
+ if model in word:
348
+ potential_products.append(model)
349
+
350
+ if len(potential_products) >= 2 and product_comparison:
351
+ comparison_table = product_comparison.create_comparison_table(potential_products)
352
+ return f"Ürün Karşılaştırması:\n\n{comparison_table}"
353
 
354
+ return None
355
+ except Exception as e:
356
+ print(f"Comparison error: {e}")
357
+ return None
 
358
 
359
  def get_user_recommendations(user_id, user_message):
360
  """Kullanıcıya özel öneriler al"""
361
+ try:
362
+ # Bütçe sorgusu varsa
363
+ if "bütçe" in user_message.lower() or "budget" in user_message.lower():
364
+ # Rakamları çıkarmaya çalış
365
+ import re
366
+ numbers = re.findall(r'\d+', user_message)
367
+ if len(numbers) >= 2 and personalized_recommendations:
368
+ budget_min = int(numbers[0]) * 1000 # K TL formatı için
369
+ budget_max = int(numbers[1]) * 1000
370
+ recommendations = personalized_recommendations.get_budget_recommendations(
371
+ user_id, budget_min, budget_max
372
+ )
373
+
374
+ if recommendations:
375
+ rec_text = "Bütçenize uygun öneriler:\n\n"
376
+ for product in recommendations[:3]:
377
+ rec_text += f"• {product[2]} - {product[1][1]} TL\n"
378
+ return rec_text
379
+
380
+ # Genel kişisel öneriler
381
+ if personalized_recommendations:
382
+ suggestions = personalized_recommendations.get_personalized_suggestions(user_id)
383
+ if suggestions:
384
+ sug_text = "Size özel önerilerimiz:\n\n"
385
+ for product in suggestions:
386
+ sug_text += f"• {product[2]} - {product[1][1]} TL\n"
387
+ return sug_text
388
+
389
+ return None
390
+ except Exception as e:
391
+ print(f"Recommendations error: {e}")
392
+ return None