Upload 2 files
Browse files- app.py +60 -31
- 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 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
#
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
|
|
|
|
|
|
| 661 |
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
for
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
return None
|
| 354 |
|
| 355 |
def get_user_recommendations(user_id, user_message):
|
| 356 |
"""Kullanıcıya özel öneriler al"""
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|